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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with add existing file name as new data column in new output file
# 1  
Old 01-13-2015
Help with add existing file name as new data column in new output file

Input File 1
Code:
cat S1.txt
MI0043 2731 miR-1

Input File 2
Code:
cat S4.txt
MI006 310 CiR-1
MI057 10 CiR-24
MI750 5 CiR-24

Desired Output File 1
Code:
cat S1.txt.out
MI0043 2731 miR-1 S1.txt

Desired Output File 2
Code:
cat S4.txt.out
MI006 310 CiR-1 S4.txt
MI057 10 CiR-24 S4.txt
MI750 5 CiR-24 S4.txt

Do anybody have idea how to add the existing file name at the last data column in new output file?
# 2  
Old 01-13-2015
Try
Code:
awk '{print $0, FILENAME}' S?.txt

Make very sure your input files don't have DOS <CR> line separators (they do have now!) cause that would overwrite the BOL with the file name.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-13-2015
Hello perl_beginer,

Could you please try following and let us know if this helps.
Code:
awk 'FNR==1{I++} {print $0 OFS FILENAME > "OUTPUT_"I;}' S1.txt  S4.txt

EDIT: Adding one more solution with the requested output file names as follows.
Code:
awk '{print $0 OFS FILENAME >> FILENAME".out"}' S1.txt  S4.txt

This above will create 2 files named S4.txt.out and S1.txt.out.


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-13-2015 at 11:25 AM.. Reason: Added one more solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 01-13-2015
Thanks RudiC.
It worked fine Smilie

---------- Post updated at 09:07 PM ---------- Previous update was at 09:06 PM ----------

HI RavinderSingh13,

Really many thanks Smilie
Both solution looks fine and really thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add current time stamp column in existing csv file

Hi , I want to add a new column 'current_time stamp' in my existing csv file with current time stamp for all the records.I tried something this but this is printing 0 with date & time and printed date one line above header.Please help awk -F "," 'BEGIN{ OFS="," } {$6=system("date... (5 Replies)
Discussion started by: netdbaind
5 Replies

2. Shell Programming and Scripting

Matching column then append to existing File as new column

Good evening I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this. I have file called file1 and file2. I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd... (4 Replies)
Discussion started by: mychbears
4 Replies

3. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 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. Ubuntu

How to add a data column in existing file

Hi All I need to add a column on my existing data file. I know similar posts are there but none of them were meeting my requirement. My input is 1.20 3.44 4.88 5.11 4.99 3.22 1.89 3.89 2.90 Desired output 1 1.20 3.44 4.88 2 5.11 4.99 3.22 3 1.89 3.89 2.90 I will... (2 Replies)
Discussion started by: mahbub03
2 Replies

6. Shell Programming and Scripting

add more data to existing data in a file

Hi all, I need help to add additional data from file2 to existing data in file 1 using awk, sed or perl. the ID in file 1 should match against field $3 in file2 file1 #this is a new game ID HR_1 BASE1 30 BASE2 37 DETAIL No TYPE L @@ ID HR_10 BASE1 6030 BASE2 ... (4 Replies)
Discussion started by: redse171
4 Replies

7. UNIX for Dummies Questions & Answers

Add line with data to existing file

i have a file called motors with 3 columns separated with tabs eg: car make reg i want to create a executable file that will add a line with data eg: car make reg benz s600 t35778 can you please show me how to do this? (7 Replies)
Discussion started by: fletcher
7 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. 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

10. Shell Programming and Scripting

Need to add a line of data to already existing file in Unix..

Hello.. I have a text file with 100 lines of data. I need to add 1 line of data to that already existing file at the first line( beginning of the file) , so that the already existing 100 lines will start from 2 nd line.Now the file will have 101 lines of data. Help me on how to add the line... (4 Replies)
Discussion started by: charan81
4 Replies
Login or Register to Ask a Question