Merge files and add file name to the end of each line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Merge files and add file name to the end of each line
# 1  
Old 12-15-2010
Merge files and add file name to the end of each line

Hello everybody,

I'm trying to merge a lot of files, but I want to include the filename to the end of each line. I've tried to use cat, but I got stuck.

My files are for example:

file01.001

123456 aaa ddd ee
458741 eee fff ee

file02.003

478596 uuu ddd ee
145269 ttt fff ee

What I want is:

123456 aaa ddd ee file01.001
458741 eee fff ee file01.001
478596 uuu ddd ee file02.003
145269 ttt fff ee file02.003


Any suggestion?
# 2  
Old 12-15-2010
Code:
for f in file*.*
do
 sed 's/$/ '"$f"'/' $f
done > output

# 3  
Old 12-15-2010
Thanks a lot!!! It worked perfectly
# 4  
Old 12-15-2010
This puts the file name on the front with a colon:
Code:
grep '.*' file*.*

With line numbers:
Code:
grep -n '.*' file*.*

This User Gave Thanks to DGPickett For This Post:
# 5  
Old 12-15-2010
Thanks!!! It's simpler but effective. Thanks to both of you
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add a line to the end of a set of files without using sed command?

I understand that the SED command reads all the lines in the file before adding a required line to the end of the file. Is there another command that adds a line to the end of files without reading the entire file.... SED is increasing the processing time as the number of lines in each of the... (1 Reply)
Discussion started by: Kanch
1 Replies

2. Shell Programming and Scripting

Add name at the end of line in one file

Hi Folks, I want to add one name at the end of one file. Below line i have to add end of line some name... Search_masterlogin=`grep -i $masterlogin passwd.master|awk -F: '{print $1}'` $ grep -i susan passwd.master |awk -F: '{print $1}' susan $ I want to insert one name called... (10 Replies)
Discussion started by: susindram
10 Replies

3. Shell Programming and Scripting

Add sth to end of each line, but only for specific files

Hi. I have a list with files, and I would like to add a variable to the end of each line of each file in this list. The files are in a folder together with a large number of other files which I don't want to change. My code is: for file in 'cat ../list' do sed 's/$/\/R:_0/' $file >>... (4 Replies)
Discussion started by: Bloomy
4 Replies

4. UNIX for Dummies Questions & Answers

Add blank line to end of file

Alright, so I was looking around a bit on the forum before posting and still don't really understand so I figured I'd post my own question. I am appending two files using cat file_1.txt >> file_2.txt The problem is that I need a blank line in between the two different text files and it does... (2 Replies)
Discussion started by: cgagnon
2 Replies

5. Shell Programming and Scripting

add character to every end-of line in file

Hi All I have a file which conatins record.the length of every records is 47. problem : in the end of record i don't have a "\015" character. i want to add this "\015" charcter in the end of every record. the file contains something like 700 records. i've tried with sed command - nothing. ... (8 Replies)
Discussion started by: naamas03
8 Replies

6. Shell Programming and Scripting

add character to the end of each line in file

hi all i have 32 lines in file. the length of each line is 82 , i want that in the end of each line , means in postion 83-84 to put two characters 0d(=\015), 0a(=\012) i want that the 0d will be in postion 83 and the 0a will be in postion 84 in each line of the file how shall i do it ? ... (7 Replies)
Discussion started by: naamas03
7 Replies

7. Shell Programming and Scripting

Removing end of line to merge multiple lines

I'm sure this will be an easy question for you experts out there, but I have been searching the forum and working on this for a couple hours now and can't get it right. I have a very messy data file that I am trying to tidy up - one of the issues is some records are split into multiple lines: ... (4 Replies)
Discussion started by: tink
4 Replies

8. Shell Programming and Scripting

MERGE 13 files and add the file name at the end of each record

Hi Gurus, I have 13 comma(,) seperated files that i have to merge and create a single file which has file name attached at th end of each record in the out put file. Can any one please help me with writing a unix script with this issue? test1.dat BIG ID,Local ID,Bond... (2 Replies)
Discussion started by: vkr
2 Replies

9. Shell Programming and Scripting

How to add filename to the end of each line in file

Hi, I'm reading data from comma separated files to DB. Now there is a need to have the name of the input file in each row of that file. How can I do this in unix script? Example: $cat file1 value11,value12, value,13 value21,value22, value,23 value31,value32, value,33 And the result... (2 Replies)
Discussion started by: tmikahan
2 Replies
Login or Register to Ask a Question