![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| print pattern line +2 without tabs or brackets | repudi8or | Shell Programming and Scripting | 2 | 04-16-2008 05:39 PM |
| I need help counting the fields and field separators using Nawk | scrappycc | Shell Programming and Scripting | 3 | 02-06-2008 08:47 PM |
| using nawk to concate the fields | raychu65 | UNIX for Dummies Questions & Answers | 2 | 01-25-2008 02:26 AM |
| sh: Inserting tabs and moving text to 1 line | 00000008 | Shell Programming and Scripting | 6 | 07-26-2007 01:58 AM |
| append field to file(nawk) | axl | Shell Programming and Scripting | 4 | 11-14-2004 02:19 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Append tabs at the end of each line in NAWK -- varying fields
Hi,
I need some help in knowing how I can append tabs at the end of each line... The data looks something like this: field1, field2, field3, field4 1 2 3 4 5 I have values in field1 and field 2 in the first row and I would like to append tab on field3 and field4 for the first row..and in the same way append tab on field4 for the second row. Fields can vary in number...Next time the file comes, it might have 10 fields..Can we achieve this in NAWK? Pass field number as the parameter and append tabs to all the fields which doesn't have values.. Please advise... Madhu |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Try this
awk -v x=4 '{printf("%s",$0);if(NF < x) for(i=NF;i<x;i++) printf("%s","\t"); printf("%s","\n")}'
x=4 is the number of fields you want to have. |
|
#3
|
||||
|
||||
|
Code:
awk -v x=6 '{ for (;x > NF; x-- ){ sub ("$", "\t") } print }'
|
|
#4
|
|||
|
|||
|
Thank you Rajeev and Reborg....
I tried both of your suggestions.... Code:
#!/usr/bin/ksh
nawk -v x=4 '{ for (;x > NF; x-- ){ sub ("$", "\t") } print }' filename > filename2
PHP Code:
|
|
#5
|
|||
|
|||
|
Just wanted to kindly ask if anyone has an idea...
Code:
nawk -v x=78 '{ for (;x > NF; x-- ){ sub ("$", "\t") } print }' NarenTest.dat > filename2
Please advise... |
|
#6
|
|||
|
|||
|
I apologize....I figured it out! Thank you very much....
|
|
#7
|
||||
|
||||
|
Code:
nawk -v y=78 '{ for (x=y;x > NF; x-- ){ sub ("$", "\t") } print }' NarenTest.dat > filename2
Sorry you are correct, I forget to reset x. |
||||
| Google The UNIX and Linux Forums |