LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help understanding awk (https://www.linuxquestions.org/questions/linux-newbie-8/help-understanding-awk-894509/)

bino25 07-29-2011 12:43 PM

help understanding awk
 
I am trying to debug a script that another person wrote. I am not very strong understanding awk. Can somebody please explain what this line is doing?

cat ${origfile} | awk '{sub("\014","\n\\page\n");print $0 "\par"}' >>${rtftempfile}

I know awk is doing some kind of manipulation of the acsii file ${origfile}, just not sure what.

Thanks

colucix 07-29-2011 12:53 PM

The syntax of the sub function is:
Code:

sub(regexp, replacement [, target])
where regexp is the regular expression to match and replacement is the string that substitutes the matching pattern. The target is optional: if not specified the whole record $0 is used. In practice your statement:
Code:

sub("\014","\n\\page\n")
should substitute every occurrence of the character whose octal code is 014 (form feed in the ASCII table) with a newline, followed by \page and again a newline. Take in mind that in awk regexps, the \NNN is an escape sequence to match ascii characters by means of their octal value. Hope this helps.

igadoter 07-29-2011 04:47 PM

Google "gawk". There is book in postscript format about gawk ("gnu awk"). Awk is almost a programming language - so do not expect reasonably introduction in the two or three posts.

bino25 08-01-2011 06:21 AM

What does the "\par" represent after the record $0?

grail 08-01-2011 06:33 AM

It is a string with those characters in it.

colucix 08-01-2011 06:33 AM

Quote:

Originally Posted by bino25 (Post 4430556)
What does the "\par" represent after the record $0?

Literally "par". The backslash is treated as an escape character (quite not useful here) so that \p is treated as p. If you want to print out a literal "\par" you have to escape the escape character:
Code:

print $0 "\\par"
Regarding the synatx of the print command, you can print out multiple strings (in this case $0 followed by "par"). Just take in mind that if you separate them with a comma, they will be printed out separated by the OFS (Output Field Separator, a blank space by default). If you omit the commas, they will be concatenated together. Example:
Code:

$ cat > file
line1
line2
$ awk '{print $0 "\par"}' file
awk: warning: escape sequence `\p' treated as plain `p'
line1par
line2par
$ awk '{print $0, "\par"}' file
awk: warning: escape sequence `\p' treated as plain `p'
line1 par
line2 par

Hope this helps. If you want to learn more about awk, here are two useful links:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/manual/

bino25 08-02-2011 06:24 AM

You guys rock!!! Thanks for all of the help!!


All times are GMT -5. The time now is 11:22 PM.