LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-29-2011, 12:43 PM   #1
bino25
Member
 
Registered: Dec 2010
Posts: 45

Rep: Reputation: 11
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
 
Old 07-29-2011, 12:53 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 07-29-2011, 04:47 PM   #3
igadoter
Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: slackware 12.2, scientific linux 6.4, knoppix 7.2, salix 14.1
Posts: 910

Rep: Reputation: Disabled
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.
 
Old 08-01-2011, 06:21 AM   #4
bino25
Member
 
Registered: Dec 2010
Posts: 45

Original Poster
Rep: Reputation: 11
What does the "\par" represent after the record $0?
 
Old 08-01-2011, 06:33 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
It is a string with those characters in it.
 
Old 08-01-2011, 06:33 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by bino25 View Post
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/
 
Old 08-02-2011, 06:24 AM   #7
bino25
Member
 
Registered: Dec 2010
Posts: 45

Original Poster
Rep: Reputation: 11
You guys rock!!! Thanks for all of the help!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM


All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration