Append Multiple files with file name in the beginning of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append Multiple files with file name in the beginning of line
# 1  
Old 12-18-2013
Append Multiple files with file name in the beginning of line

Hi,

I have multiple files having many lines like as bvelow:
Code:
file Name a.txt
abc    def
def    xyz
123   5678

Code:
file Name b.txt
abc def
def xyz
123 5678

I would like to append files in the below format to a new file:
Code:
file Name c.txt
a.txt:abc    def
a.txt:def    xyz
a.txt:123   5678
b.txt:abc def
b.txt:def xyz
b.txt:123 5678

like above input files I have apprx 500 files and each file does contain thousends of records, which need to be murged into single file

Please help
# 2  
Old 12-18-2013
Code:
awk 'BEGIN{print "file name","out.txt"}FNR>1{print FILENAME":"$0}' *.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-18-2013
Code:
grep . {a,b}.txt >c.txt

Or a little more elaborated:
Code:
f="c.txt"; ( echo "file Name $f"; grep -v file {a,b}.txt ) >$f


Last edited by ctsgnb; 12-18-2013 at 10:40 AM..
# 4  
Old 12-18-2013
Quote:
Originally Posted by Yoda
Code:
awk 'BEGIN{print "file name","out.txt"}FNR>1{print FILENAME":"$0}' *.txt

Small change as you miss first line with FNR>1

Code:
$ awk 'BEGIN{print "file name","out.txt"}{$0=FILENAME":"$0}1' *.txt

file name out.txt
a.txt:abc    def
a.txt:def    xyz
a.txt:123   5678
b.txt:abc def
b.txt:def xyz
b.txt:123 5678

# 5  
Old 12-18-2013
The way you provide your requirement was a bit confusing :
I guess he was assuming that the first line was like
Code:
"file Name a.txt"

or
Code:
"file Name b.txt"

That's why he added the FNR>1 to skip those lines.

If the first line of your files are not supposed to contain the filename and must just contain datas, then the suggested

Code:
grep . {a,b}.txt >c.txt

should be sufficient to do what you need :


Code:
# cat a.txt
abc    def
def    xyz
123   5678
# cat b.txt
abc def
def xyz
123 5678
# cat c.txt
cat: c.txt: Aucun fichier ou répertoire de ce type
# grep . {a,b}.txt >c.txt
# cat c.txt
a.txt:abc    def
a.txt:def    xyz
a.txt:123   5678
b.txt:abc def
b.txt:def xyz
b.txt:123 5678
#


Last edited by ctsgnb; 12-18-2013 at 12:38 PM..
# 6  
Old 12-19-2013
Hi,

Thanks for all you support, and regret for the confusion created, actually below code worked the desired output, I was required simplly the file name to be added at the beginning of each line item

Code:
awk 'BEGIN{$0=FILENAME":"$0}1' *.txt

# 7  
Old 12-19-2013
Quote:
Originally Posted by ctsgnb
... ... ...
Code:
grep . {a,b}.txt >c.txt

... ... ...
Note that if an input file contains any empty lines (i.e., just a <newline> character), they will not appear in the output produced by the above command.

However, the command:
Code:
grep '^' *.txt > output

should do what was requested. (Note that if the input files are matched by the pattern *.txt, the output file must not also match that pattern unless it is located in a different directory so the input file pattern won't match the output file's pathname.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Removing characters from beginning of multiple files

Hi, I have been searching how to do this but I can't seem to find how to do it. Hopefully someone can help. I have multiplr files, 100's example 12345-zxys.213423.zyz.txt. I want to be able to take all these files and remove the first '12345-' from each of the files. '12345-' these characters... (5 Replies)
Discussion started by: israr75
5 Replies

2. Shell Programming and Scripting

Append file name to the beginning of each line

I want to append file names at the beginning of a line for each row file content abc.txt.gz 123|654|987 bcd.txt.gz 876|trf|kjh I want a single output file with below format abc.txt.gz|123|654|987 bcd.txt.gz|876|trf|kjh This one is working but only with unzip files,need to have... (3 Replies)
Discussion started by: rakesh5300
3 Replies

3. Shell Programming and Scripting

Read multiple files, parse data and append to a file

Hi..Can anyone suggest a simple way of achieving this. I have several files which ends with extension .vcf . I will give example with two files In the below files, we are interested in File 1: 38 107 C 3 T 6 C/T 38 241 C 4 T 5 C/T 38 247 T 4 C 5 T/C 38 259 T 3 C 6 T/C... (8 Replies)
Discussion started by: empyrean
8 Replies

4. Shell Programming and Scripting

Append variable texts to the beginning of each line in all files in a directory

I am writing a code to append some numbers in the beginning of each line in all the files present in a directory. The number of files are really huge. The files are numbered as 1.sco, 2.sco, 4.sco (Note: 3.sco is missing). The files currently look like this: 1.sco 2 3 5 6 6 7My task is to... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

5. Shell Programming and Scripting

how to append multiple lines to the last line of a file

Hello, This is what I am trying to achieve: file1 a b c d file2 e f g h (8 Replies)
Discussion started by: smarones
8 Replies

6. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

7. Shell Programming and Scripting

how to append blank line in multiple files in unix

Dear All- Please suggest a single unix command which can append blank line in multiple files. I need to achieve this using a single unix command and not a script Please do advice Regards, Suresh (2 Replies)
Discussion started by: sureshg_sampat
2 Replies

8. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies

9. UNIX for Dummies Questions & Answers

Append to beginning of a file

Hi, I have a file x which is being upated continuously. I want to add file y in the file x but at the beginning of file x. file x file y After commands file x eeee aaaa aaaa gggg bbbb bbbb hhhh... (2 Replies)
Discussion started by: baanprog
2 Replies

10. Shell Programming and Scripting

Append (cat) to beginning of file ?

Hi, Fairly new to unix scripting, hoping to get some help. using AIX v5 Basically I have 3 files 1). Header record 2). many detail record 3). Trailer record My desired result is 1 file which contains Heaeder, Detail, Trailer Currenty I am using a series of: ... (8 Replies)
Discussion started by: CBZ
8 Replies
Login or Register to Ask a Question