awk print question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print question
# 1  
Old 06-22-2012
awk print question

Hello, I have an input file like
Code:
123,456,789,321,654,987,IN,OUT,2012,000075,CF34EQ

It has 11 fields separated by comma. I am trying to do this
Code:
while read string
do
  var=$(echo $string | awk -F"," '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}') 
  set -- $var
echo $1
...
...
echo $10
echo $11
done < $tmpdir/appended.dat

Everything goes well, the output is OK until it reach $10 and $11. Then the output is 010 and 011 instead of 000075 and CF34EQ. Can you help, please?

Thanks!

Last edited by Scrutinizer; 06-23-2012 at 04:04 AM.. Reason: code tags instead of quote tags
# 2  
Old 06-22-2012
You don't need to use awk here, the special IFS variable controls variable splitting, just use it.

Code:
OLDIFS="$IFS"
IFS=","

while read LINE
do
        set -- $LINE # Splits on ,
done < inputfile
IFS="$OLDIFS"

Using pure builtins should make the code literally hundreds of times faster, too.

You might need to use ${10} and ${11}

Last edited by Corona688; 06-22-2012 at 12:06 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-23-2012
Thanks a lot!
# 4  
Old 06-23-2012
You can also set IFS local to the read command, then you do no need to record and reinstate the old IFS value:
Code:
while IFS=, read string
do
  set -- $string
done < inputfile

You need to use curly brackets ( ${10} ) for elements higher then nine other $10, otherwise it get interpreted as $1 with a 0 appended..

Last edited by Scrutinizer; 06-23-2012 at 03:59 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Print NF Question

Petro_Project/Regional Work/Charge Access/Studies_PG2014/P_Tables A_394857_figure.pdf First I apologize for my novice question. I'm still learning sed & awk. I have a file with the following data format: Petro_Project/Regional Work/Charge Access/Studies_PG2014/P_Tables/A_394857_figure.pdf ... (7 Replies)
Discussion started by: leepet
7 Replies

2. UNIX for Dummies Questions & Answers

awk question - print columns with names

I found this command and would like to know what it means: gawk 'NR==1{for(i=1;i<=NF;i++)if($i~/FE/)f=i}{for(i=0;i<n;i++)printf"%s%s",i?" ":"",$f;print""}' It seems to mean if the row =1 assign i to that row, and then if FE is in the top row /column then increment and print the row. I am... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

awk question: How to print condition of NR & NF together.

Experts: LINE1 :This is line one The FIRST line of the file. LINE2 :This is line two LINE3 :This is line three with 8 fileds LINE4 :This is line four LINE5 :This is line five LINE6 :This is line six with 8 fileds I want to delete line 1, and then process the file and want to print lines... (2 Replies)
Discussion started by: rveri
2 Replies

6. Shell Programming and Scripting

echo/print variable question

while read filer ; do echo $filer $1 $2; ssh $filer vfiler status -r | awk '/running/{host=$1}/Path:/{path=$2;print host,path}'; done < filers.list this will print node1 vfiler0 / vfiler2 /vol/vfiler2_vol0 vfilert /vol/vfiler_vol vfilert /vol/virt_vol where node1 = $filer. however how... (1 Reply)
Discussion started by: riegersteve
1 Replies

7. Shell Programming and Scripting

Perl question - print last element in a hash?

I am modifying someone else's code. There is a foreach statement printing the contents of a hash. Can someone give me an example of printing the last element in a hash? The output currently is A B C D E I want the output to be E (1 Reply)
Discussion started by: streetfighter2
1 Replies

8. Shell Programming and Scripting

Question about AWK and print

Hello friends, can any one tell what is meaning of this awk '{print NR ". " $0}' phonebook i just want to know what is ". " and $0 mean in this. thanks (2 Replies)
Discussion started by: tarunsavalia
2 Replies

9. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

10. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question