Append file name to the beginning of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append file name to the beginning of each line
# 1  
Old 11-02-2012
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
Code:
abc.txt.gz 123|654|987
bcd.txt.gz 876|trf|kjh

I want a single output file with below format
Code:
abc.txt.gz|123|654|987
bcd.txt.gz|876|trf|kjh


This one is working but only with unzip files,need to have something like this working with zip files as i cannot unzip the files ...


Code:
awk '{print FILENAME ?"|" $0}' ${file}


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 11-02-2012 at 06:19 PM.. Reason: code tags
# 2  
Old 11-02-2012
gzip applies data compression algorithm to compress the file and it is not readable unless your decompress it. How do you expect to read the content of a gzip file without gunzip-ing it?

So you have to gunzip it and then apply your awk code.

---------- Post updated at 16:51 ---------- Previous update was at 16:21 ----------

If you don't want to unzip your file and achieve this, then you can use gzcat command:-

Code:
gzcat abc.txt.gz | awk -v fname="abc.txt.gz" '{print fname "|" $0}'

# 3  
Old 11-02-2012
If you just want to replace the space with a "|" in the example file you posted:
Code:
$ cat t
abc.txt.gz 123|654|987
bcd.txt.gz 876|trf|kjh

$ sed 's/ /|/' t
abc.txt.gz|123|654|987
bcd.txt.gz|876|trf|kjh

# 4  
Old 11-02-2012
Thanks bipinajith ,your code solved my requirement.

Code:
zcat ${file} | awk -v fname=${file} '{print fname "|" $0}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to append in the beginning not at the end?

Hi, I now that >> will append text to the end of the text that is already inside the file. How to append the new text infront of the text that is already in the file. Thanks for any input. Regards, Chandu (3 Replies)
Discussion started by: chandrakanth
3 Replies

2. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

3. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

4. Shell Programming and Scripting

Append Multiple files with file name in the beginning of line

Hi, I have multiple files having many lines like as bvelow: file Name a.txt abc def def xyz 123 5678 file Name b.txt abc def def xyz 123 5678 I would like to append files in the below format to a new file: file Name c.txt (7 Replies)
Discussion started by: rramkrishnas
7 Replies

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

Adding one string at the beginning of each line in a file

Hi, I have file a.txt as below. I want to add one string root beginning of each line. Sample file a.txt aaa bbb ccc Sample output Root aaa Root bbb Root ccc Can any one help me on this? (6 Replies)
Discussion started by: siba.s.nayak
6 Replies

8. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 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