Search compressed files with awk and get FILENAME


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search compressed files with awk and get FILENAME
# 1  
Old 02-27-2013
Search compressed files with awk and get FILENAME

I have many compressed files I want to search using awk and want to print some file contents along with the filename it came from on each output record (I simplified awk command).

Here are the results with the files uncompressed:
Code:
awk '{print FILENAME, $0}' test*.txt
test1.txt from test1 file
test2.txt from test2 file
test3.txt from test3 file

If I compress the files, I use find and zcat to pass to awk but the filename does not print:
Code:
find . -name "test*.txt.Z" -exec zcat {} \; | awk '{print FILENAME, $0}' 
- from test1 file
- from test2 file
- from test3 file

How can I use awk to search compressed files and display the filename with each output line?
# 2  
Old 02-27-2013
Try:

Code:
find . -name "test*.txt.Z" -print | awk '{F=$0; while(("zcat "F| getline) > 0) print F, $0; close("zcat "F)}'

or

Code:
find . -name "test*.txt.Z" -print | while read FILE
do
    zcat "$FILE" | awk -v F="$FILE" '{ print F, $0 }'
done


Last edited by Chubler_XL; 02-27-2013 at 02:42 PM.. Reason: Alternate solution
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 02-27-2013
Another approach:
Code:
find . -name "test*.txt.Z" -exec zcat -v {} \; 1>&1 2>&1 | awk ' {
        sub(/[ \t]+[0-9]+\.[0-9]%/,x,$0)
        r = match($0, /\..*:/)
        if(r) {
                f = substr($0,RSTART,RLENGTH-1)
                print "Filename: " f
                $0 = substr($0,RSTART+RLENGTH)
                sub(/^[ \t]+/,x,$0)
        }
        print $0
} '

This User Gave Thanks to Yoda For This Post:
# 4  
Old 02-28-2013
Chubler_XL,
Your one line solution, which I was looking for, worked perfectly! Thank you.

bipinajith,
Thank you as well, however, I will need to study your solution to determine what it's doing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Validate compressed files

Hi All, I have zip file that needs to be validated and checked for 5 times with sleep of 60 seconds. Some thing like below #!/bin/bash counter=1 while do curl -i -k -X GET `strings tmp.txt |grep Location| cut -f2 -d" "` -H "Authorization: Token $TOKEN" -o $zip_file ## this is... (6 Replies)
Discussion started by: Master_Mind
6 Replies

2. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

3. UNIX for Dummies Questions & Answers

Reading compressed files during a grep search

All, The bottom line is that im reading a file, storing it as variables, recursively grep searching it, and then piping it to allow word counts as well. I am unsure on how to open any .zip .tar and .gzip, search for keywords and return results. Any help would be much appreciated! Thanks (6 Replies)
Discussion started by: ryan.lee
6 Replies

4. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

5. UNIX for Dummies Questions & Answers

To view compressed files

Hello All I compressed a file hello by using compress command compress hello ( enter ) i got the file as hello.z 1. My question is how can i see the file hello.z 2. How can i uncompress it back to change it to filename hello thanks (4 Replies)
Discussion started by: supercops
4 Replies

6. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

7. UNIX for Advanced & Expert Users

How to search for text within compressed file

I was wondering if there's a way to search within a file that's been compressed. i.e. if file a is inside file a.zip or a.gz, is there a a command that will retrieve the string of data I'm looking for in file a, and list which compressed file it found it in? Please help! Thanks. (8 Replies)
Discussion started by: HLee1981
8 Replies

8. UNIX for Advanced & Expert Users

Search first line of compressed file

I want to read a directory full of compressed files and move the file to another directory if it meets certain criteria. I only want to look at the first line of the compressed file and if I find the string, do the move. I am currently using the following: zgrep -R -L... (1 Reply)
Discussion started by: cbreiner
1 Replies

9. UNIX for Dummies Questions & Answers

renaming a compressed file to filename without .Z

In a shell script I would like to use a compressed file name, i.e. with suffix of .Z, as a file input $1. After the file in uncompressed, I would like to use the file name without the .Z . How do I do this? Thank you. (8 Replies)
Discussion started by: bruceps
8 Replies

10. UNIX for Dummies Questions & Answers

import compressed files using pipe

I am trying to import compressed files using a pipe on a server, IBM AIX UNIX 3.4, with very little disk space The command is: nohup cat xaa xab xac xad xae xaf xag | uncompress - > imp_pip & Then the imp_pip file is used in the import statement, files=imp_pip Does this statement... (0 Replies)
Discussion started by: pengwyn
0 Replies
Login or Register to Ask a Question