Register
It is currently Sun Apr 22, 2018 9:57 pm

Delete characters n to m in each line


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Tue May 24, 2016 4:31 am   

Joined: Tue May 24, 2016 4:25 am
Posts: 1
Hi everybody,

I want to remove certain characters in a file, for example all characters between the third and tenth position in each line. I've found examples on how to delete the first n characters, or the last n but not really something I can use for deleting something in between. I am not sure if the sed command is the right answer for this problem, but I want it to be.
It would be nice if I could specify to do this only for line 5-10, but that is not really necessary. Thank you very much for your help in advance, I am looking forward to improve my skills with the powerful bash tools.

Greetings,
bwp


Top
 Profile  
 PostPosted: Tue May 24, 2016 7:53 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
datafile=somedatafile

export from_line=1
export to_line=\$

export from_char=2
export to_char=4

awk 'NR==ENVIRON["from_line"],NR==ENVIRON["to_line"]{
        print substr($0,1,ENVIRON["from_char"]) substr($0,ENVIRON["to_char"]+1, length($0))
        }
    ' $datafile
First few "export" lines set needed vars. As we need 'em in the environment we have to use the keyword "export". Read help export

awk can read all environment vars. They are accessible in an assoc array named "ENVIRON".
ENVIRON["PATH"] would give the PATH from calling environment.

The awk program is just one conditional block. The condition is a range expression.
awk ' <range_start> , <range_end> { print }'
NR is a builtin var of awk. It is the Number of Records (i.e. lines) already read.
Hence the condition is the range from $from_line , $to_line.
For all tools like awk, sed, grep and so on a $ sign denotes the end of line or the end of file depending on context.
So these vars tell awk to print all lines. i.e. from line one to last line.
Of course the $ sign must be protected by a backslash to avoid being interpreted by the bash itself.

$0 is a builtin var denoting the entire line read.
substr(var,start,length) is a builtin function too. It returns length chars of var from start
So just one print statement gets executed:
print substr(line, 1, from_char) substr( line, from_char +1 , length($0))
( the length($0) at the end can be ommited, as per default awk would return the rest of the string, if length is ommited)


Top
 Profile  
 PostPosted: Thu May 26, 2016 9:29 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 642
hi,

doesn't every awk versions have the -v option to set "external" variables ?


Top
 Profile  
 PostPosted: Fri May 27, 2016 1:06 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
yes.

There are three major ways to access Envrionment vars.

The third is by quoting.


Top
 Profile  
 PostPosted: Sat May 28, 2016 8:26 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 642
Quote:
The third is by quoting.
what do you mean ?
Code:
var=anything
awk 'FNR==1{print '"$var"'}'
?
then another way is to set variables before giving filenames
Code:
awk 'FNR==1{print myVar}' myVar="anything" /path/to/file

man gawk says:
Quote:
If a filename on the command line has the form var=val it is treated as a variable assignment.


Top
 Profile  
 PostPosted: Sat May 28, 2016 8:38 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Yes, i did mean that by quoting is another way to do so.
There are some more ways to achieve this.
Finally you can pipe in the values from within an awk script too.

What's the point of this discussion?
Can't get that.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP