Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP WITH AWK - Extracting fields of a comma delimited file

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have a file which is generated daily and each field of the file is separated with a comma. Using awk, how can I print the values of each field, but insert spaces in between the fields? Below is a sample of one line of the file.

test_job,12/16/2002,00:02,12/16/2002,00:04,SU

Using the following command, I can extract the data from the fields, however they appear right next to eachother without any spaces. I would like to have the data separated into columns.

cat report.1216 | awk -F, '{print $1 $2 $3}'

Any help would be greatly appreciated.
 
nawk -f columns.awk file.txt

#----------------- columns.awk
BEGIN {
FS=","
OFS=" "

numCols="2"
}

{
for(i=1; i <= NF; i=i+2)
print $i , $(i+1)
}
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I'm sorry...I meant rows, not columns.

Thanks,

John
 
Hi,
You can use PRINTF in AWK rather than PRINT and then you can do the same thing you could do in C.


printf(&quot;%20s %20s %20s\n&quot;,$1,$2,$3 );

or

printf(&quot;%s\t%s\t%s\n&quot;,$1,$2,$3);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top