Setting the FS in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting the FS in awk
# 1  
Old 11-03-2006
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?
# 2  
Old 11-03-2006
try this

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

--Manish
# 3  
Old 11-03-2006
Code:
awk -F "$FSEP" '{ print $3 }'`

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 11-03-2006
awk with the -v worked. Thank you very much.
# 5  
Old 11-03-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Setting the number of fields using awk

Hi Guys, I've obviously had a senior moment here, what I'm trying to do is set the number of fields to 35 in a csv these should be appended to the end of the line. But what I'm getting is:- Source Data $ head out_file_01.txt N1000,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX... (10 Replies)
Discussion started by: gull04
10 Replies

2. Shell Programming and Scripting

awk - Removing extra character when setting variable

I have a data file d0 that looks like this: $cat d0 server1 running -n-cv- 8G 3.1% 1435d 15h server2 running -n---- 8G 39% 660d 22h server3 running -n--v- 8G 2.5% 1173d 6h server4 running -n---- 8G 1.1% 1048d 20h... (2 Replies)
Discussion started by: jake0391S
2 Replies

3. Shell Programming and Scripting

Setting Varible with AWK in KSH

I am trying to set a variable from this AWK command in KSH but I keep getting an error that says my variable cannot be found. LOADNO = $(awk -F"|" 'NR==1{print $2}' file.txt) If I just run awk -F"|" 'NR==1{print $2}' file.txt I get the right value but as soon as I try to assign this... (1 Reply)
Discussion started by: cvigeant
1 Replies

4. Shell Programming and Scripting

setting up a flag inside awk

Hi unix gurus.... i need to set a flag inside awk. i tried many methods for that but couldn't succeed :( like echo "flag= " $flag echo "flag= " ${flag} and so...but not able to show the flag. help me out. i'm using a if condition inside awk... help me out with correct syntax to set a... (7 Replies)
Discussion started by: sukhdip
7 Replies

5. Shell Programming and Scripting

setting a shell script variable in awk

The following is part of a larger shell script grep -v "Col1" my_test.log | grep -v "-" | awk '$5 == "Y" {print $1}' instead of printing, can I set set $1 to a variable that the rest of the shell script can read? if $5 == Y, I want to call another shell script and pass $1 as a... (2 Replies)
Discussion started by: guessingo
2 Replies

6. Shell Programming and Scripting

awk - setting fs to equal any single character

Hi Does anyone know how to set any character as the field separator with awk/nawk on a solaris 10 box. I have tried using /./ regex but this doesnt work either and im out of ideas. thanks (7 Replies)
Discussion started by: chronics
7 Replies

7. Shell Programming and Scripting

awk: setting decimal

hello, when I type the following command awk -v varc="$i" '{ new=($1*varc)} { print new }'it gives outputs in 5 decimal. How can I set my outputs to 9 decimal by using awk. thanks (5 Replies)
Discussion started by: rpf
5 Replies

8. Shell Programming and Scripting

awk: Eliminating white space while setting variable

Hi, I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like # Row 03: Customer / field position 3059 +20 WOFABNAM=substr( $0, 3059, 20 ); and deleting the trailing whitespaces before and after with that sub( /^ +/, "",... (4 Replies)
Discussion started by: Celald
4 Replies

9. Shell Programming and Scripting

setting a variable, using SSH and awk?

hi there I am trying to get a value from a remote machine into a local variable. To get this value i want to use awk but im having trouble getting it to run, am i escaping in the right places here and using the right quotes (i must have tried a million combinations :() # VAR=`ssh server1... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

10. UNIX for Dummies Questions & Answers

setting environment variable in awk

Dear all, I have a data sample... Dose: Summed ROI: Bladder ************************** Bin Dose Volume 001 0.700 100.000 002 0.715 99.998 168 3.142 0.368 169 3.157 0.338 170 3.171 0.292 Dose: Summed ROI:... (2 Replies)
Discussion started by: tintin72
2 Replies
Login or Register to Ask a Question