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 06-05-2013, 02:48 AM   #1
rahulsh
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Rep: Reputation: Disabled
What does -f- option do for awk command...?


I have a strange command option -f- used in the code. Here is a snippet of the code

/bin/awk -f- -v "quiet=$QUIET" -v "mapper=$MAPPER" -v "mpt=$MOUNTPOINT" \
<(/bin/cat /etc/fstab 2>/dev/null) \
<<AWKEOF
#then there is a begin block, action block and END block.
END block ends with AWKEOF like this
END {
.............some processing
}
AWKEOF

Tried googling it, but didnt get any pointer

I am not able to understand three things in the program 1
1) -f- option of awk
2) what is AWKEOF means
3) Meaning of < and << in awk context

Could anyone shed some light on this
 
Old 06-05-2013, 03:13 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Awk's -f option normally takes a command file (the awk commands, not the input file(s)) as input. You can however tell it to use the terminal as input (the - after the -f).

the <<AWKEOF and AWKEOF are here-document markers (begin and end).

In your case: All in between <<AWKEOF and AWKEOF is given to awk, awk accepts this due to the -f- option.
 
1 members found this post helpful.
Old 06-05-2013, 03:44 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
FWIW ... that is a shocking piece of code. It is obviously part of a larger shell script, so my changes would be:

1. Test for the existence of /etc/fstab earlier in the script so redirection of cat errors into /dev/null shouldn't be required

2. Remove the useless use of cat as awk reads files

3. The use of -f- and a here document seems pointless when you can simply enclose the awk part in single quotes and be done with it
 
Old 06-06-2013, 01:52 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Actually, I'd say that's a fairly clean and clever way to set up a long awk script without having to worry about weird quoting or readability issues. But it's probably not safe to use unless the heredoc delimiter is also quoted, to disable parameter expansions inside it.

I wouldn't use it unless the script were very long though. And in such cases I'd first try to see if I couldn't do the whole thing in awk, or set up that part as a stand-alone awk script and call that, or else just switch to another language like perl.


By the way, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.
 
  


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] run ps|grep command by script/command line ... ERROR: Unsupported option (BSD syntax) masuch Programming 4 05-23-2012 04:13 AM
[SOLVED] Awk command help Guilty1682 Linux - Newbie 10 02-21-2011 08:24 PM
field separation not using awk -f option sagar.tv Linux - Newbie 1 09-15-2008 01:28 PM
awk command toaravind Linux - General 5 07-02-2008 01:25 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:37 AM.

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