How to append filename to rows in gzip stdout


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to append filename to rows in gzip stdout
# 1  
Old 04-25-2012
How to append filename to rows in gzip stdout

Assume I have 2 gz files with 2 lines each as below
a.gz
Code:
a1,a
a2,a

b.gz
Code:
b1,b
b2,b

If I issue
Code:
gzip -d -c *.gz

I will get below in the stdout
Code:
a1,a
a2,a
b1,b
b2,b

What I need it the filename appended to each row either at the front or back.
Code:
a.gz,a1,a
a.gz,a2,a
b.gz,b1,b
b.gz,b2,b

Can the above be done with a single command and output to stdout?

Last edited by Franklin52; 04-26-2012 at 03:43 AM.. Reason: Please use code tags, thank you
# 2  
Old 04-26-2012
Anyone got any suggestion?
# 3  
Old 04-26-2012
Code:
for file in *.gz
do
    gzip -d -c $file | awk -v f=$file '{print f ","  $0}'
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append string to filename

Hi, i wrote the script like... v_date=`date "+%Y%m%d"`; v_date1="rejects_"$v_date'.txt'; echo $v_date1; awk -F\| 'NF==4{ print $0>$v_date1;next} NF!=4 {print $0 >"murali.txt";next}' test1.txt while append data into file as coming like file is $v_date1,but i want data into file... (4 Replies)
Discussion started by: bmk
4 Replies

2. Shell Programming and Scripting

StdOut to file --> Append Mode

Hy I've got a little problem: I work with ksh... The application I start should write its output to a log file. It works but the problem is that as I have to run this application few times in a loop it overrites the file every time.:confused: /proj/bre/tools/bin/bretestdriver... (2 Replies)
Discussion started by: lucae
2 Replies

3. UNIX for Advanced & Expert Users

How to use gzip on stdout

Does anyone know how I can use gzip to zip a large log file on the fly. My simulation is currently logging a large file that I need for analysis at a later point. However the files are so huge that I may even run out of disk space. The content is mainly text so when compressed the files are... (2 Replies)
Discussion started by: mitch1710
2 Replies

4. Shell Programming and Scripting

How to append a Value to all the rows in a file

Hi, We have Multiple source files which has some data, I need a K shell script which will append number '1' as the last character to all the lines of the first file and then increments the value and appends '2' to all the lines to the next file and so on. For Example Incoming file 1 ... (2 Replies)
Discussion started by: dsshishya
2 Replies

5. UNIX for Dummies Questions & Answers

gzip filename?

Hi, I want to be able to gzip a file but keep its original name and not give it a .gz extension. And then gunzip the file to the same name as well. Is this possible? Thanks in advance! (1 Reply)
Discussion started by: goochalba
1 Replies

6. UNIX for Dummies Questions & Answers

shortcut for tar cvf - [filename] | gzip > [filename].tar.gz

i'd like to have an alias (or something similar) where i can type a command like "archive" and a filename and have it tar and gzip the file, so... $ archive filename results in filename.tar.gz...do i have to write a script to do this? (4 Replies)
Discussion started by: bcamp1973
4 Replies

7. Shell Programming and Scripting

Append string in all rows

I would like to append a string at the end of each row of a .dat file. please give me some advice? Thanks. e.g. #content in abc.dat aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... #after running the script/command,... (7 Replies)
Discussion started by: Rock
7 Replies

8. UNIX for Dummies Questions & Answers

Append filename to datafile

I am working on an shell script which checks for all the file starting with abc*.* and if file found then the filelines need to append the file name in begining can some one help with the filename appending... for i in `ls $filename*.csv` do echo $i --- NEED to append file name befor... (3 Replies)
Discussion started by: Satyagiri
3 Replies

9. UNIX for Dummies Questions & Answers

Append current date to filename

In C Shell programming I haven't successfully been able to append the date in the format mmddyyyy to a filename. I've tried the following: I can print out the date in the correct format: date +%x | sed ‘s/\///g I can create a variable with the filename: set newfile=changedfiles I can... (3 Replies)
Discussion started by: gigigi
3 Replies

10. UNIX for Dummies Questions & Answers

Append date to filename

What is the easiest way to append the date (year, month, day) to a filename? (5 Replies)
Discussion started by: hshapiro
5 Replies
Login or Register to Ask a Question