Working with AWK with fixed with file.

Posted: March 4, 2015 in UNIX

One might observed when working with fixed width files to get substring using awk, may got weird  results.

Ex:-  echo “My project      has lots of issues” | awk ‘{print substr($0,1,5),substr($0,17,3)}’ OFS=”|”
Output:- My pr|has

Ex2:- echo My project       has lots of issues | awk ‘{print substr($0,1,5),substr($0,17,3)}’ OFS=”|”
Output:- My pr|ots
 

What is the difference why the out put is changing?

The issue is with the echo statment not with qwk command. So when every you want to do some operation on a line/string use quotes. If we don’t use the quotes unix will remove space between the strings before sending it to awk.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s