LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-10-2012, 02:39 AM   #1
priyankabade
LQ Newbie
 
Registered: May 2012
Posts: 5
Blog Entries: 1

Rep: Reputation: Disabled
Not understand command what is doing


awk -F= '{print $4}'
awk -F\( '{print $1}
what will be the output of this command

actual command is
PI=`grep 'Suspended' $FILE | awk -F= '{print $4}' | awk -F\( '{print $1}'`

for which pattern awk will search

plz reply soon
 
Old 05-10-2012, 02:41 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
that prints the 4th field, where fields are delimited with an "=" sign, and then the 1st field of that output, where fields are delimited with an opening bracket.
 
1 members found this post helpful.
Old 05-10-2012, 02:48 AM   #3
priyankabade
LQ Newbie
 
Registered: May 2012
Posts: 5
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
thanks chris
 
Old 05-10-2012, 06:22 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
grep is superfluous here, by the way. awk can do its own pattern matching.

Also, $(..) is highly recommended over `..`

Code:
PI=$( awk -F= '/Suspended/ {print $4}' "$FILE" | awk -F\( '{print $1}' )
You can also reduce it to a single awk instance by using the split function to divide the desired field into an array instead. Or since we want only the first field, the sub function would also work (to remove everything from the first paren on).

Code:
PI=$( awk -F= '/Suspended/ { split($4,a,"(") ; print a[1] }' "$FILE" )

PI=$( awk -F= '/Suspended/ { sub(/[(].*/,"",$4) ; print $4 }' "$FILE" )
See here for details on awk's string functions:
http://www.gnu.org/software/gawk/man...Functions.html


Be sure to quote all of your variables too, to prevent possible word-splitting.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


As a final bit of advice, since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
 
  


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
Need help to understand a command Mikhus Linux - Newbie 1 02-16-2012 08:38 AM
Not able to understand the output of w command pinga123 Linux - Newbie 3 08-12-2011 01:10 AM
Command understand Problem prasunhowlader Linux - Newbie 2 04-27-2008 10:56 PM
unable to understand last -x command xcr Linux - General 3 03-13-2006 12:48 AM
Dont understand the df command juanb Linux - General 2 01-02-2003 03:22 AM

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

All times are GMT -5. The time now is 06:02 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