Awk to add columns from a file into an existing file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk to add columns from a file into an existing file
# 1  
Old 04-04-2011
Awk to add columns from a file into an existing file

Hi!
I would need some help to add the last two columns of one file into another file using awk (or something similar).
For example, I have:

file 1: file 2:

car book day root lag
bar look pay boot tag
tar took may moot sag


I want to have: file 2 transformed into:

file 2 (new):

root lag book day
boot tag look pay
moot sag took may


Thanks!!!
# 2  
Old 04-04-2011
man awk with print function.
# 3  
Old 04-04-2011
Try:
Code:
paste -d" " file_2 <(cut -d" " -f2,3 file_1)

# 4  
Old 04-04-2011
cat file1| awk '{print $4,$5,$2,$3}'
# 5  
Old 04-04-2011
Quote:
Originally Posted by Castelior
cat file1| awk '{print $4,$5,$2,$3}'
no need cat.

Code:
awk '{print $4,$5,$2,$3}' file1

# 6  
Old 04-04-2011
Here is a solution using sed

Code:
sed 's/\(.*\) \(.*\) \(.*\) \(.*\) \(.*\)/\4 \5 \2 \3/' file_in
root lag book day
boot tag look pay
moot sag took may

You can use >file_out to write the results back to a file

Code:
sed 's/\(.*\) \(.*\) \(.*\) \(.*\) \(.*\)/\4 \5 \2 \3/' file_in >file_out

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add new columns based on existing columns

Hi all, I am kind of stuck with printing my desired output. Please help me if you know how it can work. My input file(tab separated): NW_0068.1 41,16 100,900 NW_0699.1 4,2,19 200,700,80 My Output file (desired): NW_0068.1 41,16 100,900 100 - 141 NW_0068.1 41,16 100,900 ... (3 Replies)
Discussion started by: sam_2921
3 Replies

2. Shell Programming and Scripting

Add 8 columns at the end of .csv file using awk

Hello all, I have a .csv file of 16 columns consists of bunch of numbers. 6.45E+01 1.17E+01 8.10E+04 8.21E+01 8.50E+00 1.20E+01 1.02E+01 1.88E+01 1.86E+04 3.53E+03 1.09E+07 3.82E+04 2.09E+03 3.57E+03 2.98E+03 3.93E+03 6.34E+01 3.23E+01 9.24E+04 ... (5 Replies)
Discussion started by: Zam_1234
5 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

Add new column from another file in existing file

I have two files which has one column comman in them. The two files has exact same number of rows in the same sequence. I want to add the second column of Users_detail_servicesonly.txt as last column in the existing file. 1) Users_detail_complete.txt V0135 Memb Info ... (4 Replies)
Discussion started by: Sanjeev Yadav
4 Replies

5. UNIX for Dummies Questions & Answers

Add columns to an existing excel sheet

Hi All, i have an excel sheet as below: day ----- monday tuesday wenesday thursday friday i need to append the two more columns in this existing file below: insert date should be todays date--- day insert date insert user ---- ---------- ... (3 Replies)
Discussion started by: arunmanas
3 Replies

6. Shell Programming and Scripting

AWK : Add columns in the end of csv file

Hi everybody, I need some help please I have a csv file named masterFile1.csv header1,header2,header3 value1,value2,value3 value4,value5,value6 I am trying to add new columns in the end of the csv to have a new csv file named masterFile2.csv like this :... (3 Replies)
Discussion started by: villebonnais
3 Replies

7. Shell Programming and Scripting

Adding a new column in a file with other existing columns

Hi All , Kindly help me with this soln awk '{printf "%s %7s \n", $1,$c}' infile where value of variable c I am externally giving input But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c (8 Replies)
Discussion started by: Pratik4891
8 Replies

8. Shell Programming and Scripting

how to add a new column in an existing file

Hi guys, Please help me if u have some solution. I have a file with three columns separated by ':' - INPUT_FILE C416722_2 : calin Dirigent : Dirigent AC4174_6 : Jac : cal_co TC4260_5 : [no : lin kite BC426302_1 : [no : calin Dirigent lin JC426540_3 : lin Pymo_bin : calin TC428_3 : no7... (4 Replies)
Discussion started by: sam_2921
4 Replies

9. Solaris

Add a file to existing zip file

Hi, I was trying to add a file to the existing zip file and i was using the below command but could not able to succeed. Could you please throw some light on this. Thanks for your time. Syntax zip 20081029.zip Siebel_DW_Rep_Dev2_20081016.rep Error message: zip warning: missing end... (2 Replies)
Discussion started by: Ariean
2 Replies

10. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies
Login or Register to Ask a Question