Append tabs at the end of each line in NAWK -- varying fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append tabs at the end of each line in NAWK -- varying fields
# 1  
Old 07-11-2006
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
# 2  
Old 07-11-2006
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  
Old 07-11-2006
Code:
awk -v x=6 '{ for (;x > NF; x-- ){ sub ("$", "\t") } print }'

x is the same as in the post above.
# 4  
Old 07-12-2006
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...
# 5  
Old 07-12-2006
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...
# 6  
Old 07-12-2006
I apologize....I figured it out! Thank you very much....
# 7  
Old 07-12-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

2. Shell Programming and Scripting

Append Pipes at the end of each line

Hi , I have pipe delimited file containing 12 columns having below data in AIX Input : A|B|C|D|E|F|G|H|I|J|K|L^M A1|B1|C1|D1|E1^M A2|B2|C2^M So in first row i have 11 pipes which is fine. In second row I have 4 pipes, so additional 7 pipes should get append In third row I have 2... (3 Replies)
Discussion started by: sonu_pal
3 Replies

3. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

4. Shell Programming and Scripting

find a certain line and append text to the end of the line

After I create printer queues in AIX, I have to append a filter file location within that printers custom file. within lets say test_queue.txt I need to find the row that starts with :699 and then I need to append on the end the string /usr/local/bin/k_portrait.sh. Now I've gotten the sed... (2 Replies)
Discussion started by: peachclift
2 Replies

5. Shell Programming and Scripting

Append text to end of every line

I've scoured the internet with mixed results. As an amateur I turn to the great minds here. I have a text file of 80 or so lines. I want to add ".pdf" to the end of each line. (For now that's it) Most of the internet points toward using "sed". I don't know coding but can figure things out... (4 Replies)
Discussion started by: spacebase
4 Replies

6. Shell Programming and Scripting

Append text to end of line on all lines

Hi, I've spent some time researching for this but can't seem to find a solution. I have a file like this 1234|Test|20101111|18:00|19:00There will be multiple lines in the file with the same kind of format. For every line I need to make it this 1234|Test|20101111|18:00|19:00||create... (5 Replies)
Discussion started by: giles.cardew
5 Replies

7. UNIX for Dummies Questions & Answers

Append characters to end of line

Coding in unix shell script. Hi All, Please help Thanks (6 Replies)
Discussion started by: sam12345
6 Replies

8. Shell Programming and Scripting

append end of line with 8 spaces

child_amt=$amount prev_line="$prev_line $child_amt" i am getting the result like this 21234567890001343 000001004OLFXXX029100020091112 0000060 but i want 8 spaces between the eg: 21234567890001343 000001004OLFXXX029100020091112 0000060 how can i do this in .ksh (1 Reply)
Discussion started by: kshuser
1 Replies

9. Shell Programming and Scripting

how to append the pattern at the end of the line

-Hi I have multiple files which contain a line with the word "exec". I need to add the following pattern " -cmode -ccheap" on the same line where "exec" is at the end. Any idea? Thanks a lot in advance to everybody... -A (2 Replies)
Discussion started by: aoussenko
2 Replies

10. Shell Programming and Scripting

How to append words at the end of first line

Hi Unix gurus This is my first post here. I have a file which looks like this: string1,string2 ,string3 ,string4 ... ... I need to append all words below first line to the first line, ie string1,string2,string3,string4,... Bear in mind that it is not known how many lines follow... (11 Replies)
Discussion started by: lmatlebyane
11 Replies
Login or Register to Ask a Question