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
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-11-2006
Registered User
 

Join Date: Nov 2005
Posts: 91
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
Reply With Quote
Forum Sponsor
  #2  
Old 07-11-2006
Registered User
 

Join Date: Feb 2006
Posts: 34
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.
Reply With Quote
  #3  
Old 07-11-2006
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,638
Code:
awk -v x=6 '{ for (;x > NF; x-- ){ sub ("$", "\t") } print }'
x is the same as in the post above.
Reply With Quote
  #4  
Old 07-12-2006
Registered User
 

Join Date: Nov 2005
Posts: 91
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
The code is putting tabs only for the first row and not the rows in the rest of the file...

PHP Code:
LAST_BOOKED_DATE
LAST_BOOKED_DATE        D       YYYYMMDD        AIR_LOYALTY_COUNT
LAST_BOOKED_DATE        D
LAST_BOOKED_DATE        D       YYYYMMDD 
Please advise...
Reply With Quote
  #5  
Old 07-12-2006
Registered User
 

Join Date: Nov 2005
Posts: 91
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
The above piece of code suggested before does work only for the top row in the file. There are a couple of hundred rows in this file.

Please advise...
Reply With Quote
  #6  
Old 07-12-2006
Registered User
 

Join Date: Nov 2005
Posts: 91
I apologize....I figured it out! Thank you very much....
Reply With Quote
  #7  
Old 07-12-2006
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,638
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:26 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 Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0