Add a column to a file with nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add a column to a file with nawk
# 1  
Old 06-25-2009
Error Add a column to a file with nawk

Hi all, I'm new at forum, I cant find an answer to my problem so ,

I need a file which has pipe as a file separator and I need to add a column to a file in the third column I write this code but it s not enough ,

cat allproblems | nawk'\

BEGIN { FS:"|" } {print $3 $4 $5, ????} '

-------------------------

This prints the concataneted 3,4 and 5th columns but I want to add it to second field which is already exist so before it was shifted second to third ..

Thank you all your patient.
# 2  
Old 06-25-2009
Can you please explain your requirement with an example.
# 3  
Old 06-25-2009
Quote:
Originally Posted by rakeshawasthi
Can you please explain your requirement with an example.
My file :AAAA|BBBBBBBBBB|CCCCCC|DDDD|EEEEEE 5 field

To : AAAA|BBBBBBBBBB CCCCCC CCCCCC|BBBBBBBBBB|CCCCCC|DDDD|EEEEEE 6 fiekd

Is that clarify a little bit more ?
# 4  
Old 06-25-2009
Quote:
Originally Posted by circuitman06
My file :AAAA|BBBBBBBBBB|CCCCCC|DDDD|EEEEEE 5 field

To : AAAA|BBBBBBBBBB CCCCCC CCCCCC|BBBBBBBBBB|CCCCCC|DDDD|EEEEEE 6 fiekd

Is that clarify a little bit more ?
Should be something like:

Code:
awk -F"|" '{$2="BBBBBBBBBB CCCCCC CCCCCC|" $2}1' OFS="|" allproblems

# 5  
Old 06-25-2009
May be this is what you need...
Code:
awk -F"|" '{OFS="|"}{$2=$2" "$3" "$3;print $0}' inputfile

# 6  
Old 06-25-2009
Quote:
Originally Posted by rakeshawasthi
May be this is what you need...
Code:
awk -F"|" '{OFS="|"}{$2=$2" "$3" "$3;print $0}' inputfile

Thank you all but I'm wondering the function of OFS="|"

My code :

cat all | nawk' BEGIN{ FS="|"} { $5=$3" "$4" "$NR; print $0}' > allnew

There are not any pipes at allnew file.. They were disappeared. ( because OFS ??? )
# 7  
Old 06-25-2009
OFS==Output field separator, use it if you want records to be separated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Add a column to a file

Hi, I have this data file that contains: 1 A 2 B 3 C 4 D 5 E 6 F 7 G 8 H 9 I I want the results to be: 1 A A 2 B A 3 C A 4 D A 5 E A 6 F A 7 G A 8 H A (8 Replies)
Discussion started by: bobo
8 Replies

2. Shell Programming and Scripting

Add a new column to file

I have file like this b,c 10,20 30,40 50,60 Now I want to add a new column a with fixed values for all the rows a,b,c 60,10,20 60,30,40 60,50,60 Please let me know how can we do this in unix. (4 Replies)
Discussion started by: weknowd
4 Replies

3. Shell Programming and Scripting

Help with add existing file name as new data column in new output file

Input File 1 cat S1.txt MI0043 2731 miR-1 Input File 2 cat S4.txt MI006 310 CiR-1 MI057 10 CiR-24 MI750 5 CiR-24 Desired Output File 1 cat S1.txt.out MI0043 2731 miR-1 S1.txt Desired Output File 2 cat S4.txt.out MI006 310 CiR-1 S4.txt (3 Replies)
Discussion started by: perl_beginner
3 Replies

4. Shell Programming and Scripting

Append column using awk/nawk

Is there any way I can achieve this? Considering test1 : a 1 2 3 4 b 2 3 4 5 c 12 1232 14 1 d 10 13 4 5 e 1 5 6 9 1 And test to be some string : qw twe tew we qw I want something like this : a 1 2 qw 4 b 2 3 twe 5 (5 Replies)
Discussion started by: aksijain
5 Replies

5. Shell Programming and Scripting

How to add a column from other file?

Dear all, Lets say, I've a file a.txt containing two columns, like a1 b1 .. .. .. .. and another file b.txt containg two columns, like a1 c1 .. .. .. .. I need to put c1 column from b.txt file to the a.txt file. So, the output should be a1 b1 c1 .. .. .. .. ... (4 Replies)
Discussion started by: mkg
4 Replies

6. Shell Programming and Scripting

Add column to a file

Hola, How can I add a column to a existing file?? PS: The column which should be added need to be the first column and it will be a parameter from the script. Example: 1 name1 2 name2 3 name3 4 name3 Need to add parameter $file as a first column. $file is a file name with time... (6 Replies)
Discussion started by: Olivia
6 Replies

7. Shell Programming and Scripting

Nawk script to compare records of a file based on a particular column.

Hi Gurus, I am struggling with nawk command where i am processing a file based on columns. Here is the sample data file. UM113570248|24-AUG-11|4|man1|RR211 Alert: Master Process failure |24-AUG-11 UM113570624|24-AUG-11|4|man1| Alert: Pattern 'E_DCLeDAOException' found |24-AUG-11... (7 Replies)
Discussion started by: usha rao
7 Replies

8. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

9. Shell Programming and Scripting

Format - Inventory Row data into Column - Awk - Nawk

Hi All, I have the following file that has computer data for various pcs in my network... Snap of the file is as follows ******************************************************************************* Serial 123456 Computer IP Address lo0:... (1 Reply)
Discussion started by: aavam
1 Replies

10. Shell Programming and Scripting

o/p column wise by nawk

hi i have file which hav following entries 1501,AAA,2.00 1525,AAA,2.00 1501,AAA,2.00 1525,AAA,2.00 1501,AAA,3.00 1525,AAA,3.00 1525,AAA,3.00 1501,AAA,3.00 1501,AAA,3.00 i want to have a o/p coloum wise like 1501,AAA,13 1525,AAA,10 here 13 comes as a sum of last colum value... (6 Replies)
Discussion started by: aaysa123
6 Replies
Login or Register to Ask a Question