Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with AWK

Status
Not open for further replies.

AlStl

MIS
Oct 2, 2006
83
US
I am new to awk or nawk and inherited a script (add1.awk) , which is as follows:

#!/bin/nawk -f
{
gsub(/\015/, "")
gsub(/^ *$/, "")
gsub(/\012/, "")
gsub(/\010/, "")
}

I am issuing this command at the prompt:
add1.awk file.txt

file.txt is as follows (as you can clearly see it has bunch of blank lines in it):

Jane Doe
123 Main Street
Anywhere, SE 12345-7890

John Smith
456 amit-lined Avenue
Smallville, MW 11111-0000

Can someone explain to me what these two gsub are for i.e.

gsub(/\015/, "")
gsub(/^ *$/, "")
gsub(/\012/, "")
gsub(/\010/, "")

It seems that they are not doing anything even when I redirect it to output.txt file:

add1.awk file.txt > output.txt

output.txt is same as file.txt so first two function are not doing anything to input file and I have no idea what 3 & 4 are for?
Thanks,

Al
 
I presume the code you posted is missing a print statement from the original code?

The first, third and fourth gsub()s are attempting to replace the ASCII characters represented by those octal values with nothing. The characters are carriage return, line feed and backspace respectively.

The first one has the effect of converting DOS format files to Unix format.

The third one has no effect at all because awk treads line feed as a record (or line) separator anyway, so it is not part of the string processed by gsub().

The second one just removes any spaces on an otherwise empty line.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top