0

I have a use-case to process following file types:

1 - mylog_1.log
2 - mylog_2.log.gz

I have to run two different text processing commands on each of them as follows:

cat mylog_1.log | grep text | sort | uniq -c
zcat mylog_2.log.gz | grep text | sort | uniq -c

(cat, grep, awk and sed are the frequently used commands)

Is there a way to process both file types in a single command without unzipping the file?

0

3 Answers 3

0
(cat mylog_1.log;zcat mylog_2.log.gz) | grep text | sort | uniq -c
0

zgrep will uncompress the given files, if necessary, then pass the results on to grep:

$ echo text one > log_1.log
$ echo text two > log_2.log
$ gzip log_2.log
$ zgrep text log_* | sort | uniq -c
  1 log_1.log:text one
  1 log_2.log.gz:text two
0

If your question is specifically how to process a text file and a gunzipped text file in single command, see the other question.  But if your question is, in general, how to extract text from different types of files, using different tools, and then process them the same way,

for file in mylog_1.log mylog_2.log.gz …
do
    if [[ "$file" == *.gz ]]
    then
        zcat "$file"
    else
        cat "$file"
    fi | grep text | sort | uniq -c
done

This will process each file separately.  To combine (concatenate) them and process the aggregate text as one entity, just move the pipe:

for file in mylog_1.log mylog_2.log.gz …
do
    if [[ "$file" == *.gz ]]
    then
        zcat "$file"
    else
        cat "$file"
    fi
done | grep text | sort | uniq -c

Not the answer you're looking for? Browse other questions tagged .