The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
lan setting mightypp.nits UNIX for Dummies Questions & Answers 1 11-11-2007 12:00 PM
ssh setting kwliew999 AIX 11 07-10-2007 08:57 AM
New ip setting kuultak UNIX for Dummies Questions & Answers 6 01-30-2005 03:32 PM
Setting the UID in Mac OS X noahcowan OS X (Apple) 1 06-07-2004 07:41 PM
setting env alsande SUN Solaris 3 03-29-2004 09:24 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2006
Registered User
 

Join Date: Dec 2005
Posts: 12
Setting the FS in awk

I am writing a POSIX script where I need to set the FS equal to a variable that was set prior to executing the awk command. Is there a way to set FS equal to a variable that has already been set in the script?

Example
LINE="FIELD1~FIELD2~FIELD3~FIELD4~FIELD5"
(the separator could be different each time the LINE variable is set by reading a file, which is why I cut the 7th character each time, which would always be a separator character, to know what the separator is before getting to the awk command in the next line)

FSEP=`echo "$LINE" | cut -c 7`
'echo "$LINE" | awk 'FS="$FSEP" { print $3 }'`

However, awk doesn't recognize the $FSEP variable. Any suggestions on how I could set the FS with each iteration of the awk command in a while loop? Could I possible echo the FSEP variable out to a temp file and then read it in as the FS during the execution of the awk command?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-03-2006
Registered User
 

Join Date: Dec 2005
Location: Boston, USA
Posts: 65
try this

echo "$LINE" | awk -v FSEP=$FSEP 'FS=FSEP { print $3 }'

--Manish
Reply With Quote
  #3 (permalink)  
Old 11-03-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,861
Code:
awk -F "$FSEP" '{ print $3 }'`
Reply With Quote
  #4 (permalink)  
Old 11-03-2006
Registered User
 

Join Date: Dec 2005
Posts: 12
awk with the -v worked. Thank you very much.
Reply With Quote
  #5 (permalink)  
Old 11-03-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
You can do all the work in awk (determining field separator and printing field) :
Code:
echo "$LINE" | awk '{FS=substr($0,7,1); $0=$0 ""; print $3 }'
Jean-Pierre.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0