Sigularity Sigularity - 4 months ago 5
Linux Question

How can I parse many files at the same time with one line commands(Linux/Unix)

There are many logs files I need to parse, and below is the sample structures.
I need to view debug.log files contents under each directory at the same time with one line of commands(Linux/Unix). Probably with awk/sed/cat etc.

1/logs/debug.log
2/logs/debug.log
3/logs/debug.log
4/logs/debug.log
5/logs/debug.log
6/logs/debug.log


If each debug.log contents are below:

In 1/logs/debug.log: finished.
Please go ahead next step.
In 2/logs/debug.log: failed.
In 3/logs/debug.log: finished.
Please go ahead next step.
In 4/logs/debug.log: finished.
Please go ahead next step.
In 5/logs/debug.log: Error.
In 6/logs/debug.log: finished.
Please go ahead next step.


Expected outputs:

1: finished.
1: Please go ahead next step.
2: failed.
3: finished.
3: Please go ahead next step.
4: finished.
4: Please go ahead next step.
5: Error.
6: finished.
6: Please go ahead next step.


Sorry for very complex question but would be appreciated if you could let me know the direction to the solution.

I got an Answer



Need to use 'Loop' to traverse all sub directories.

Answer

Assuming that the directory containing the directory containing the logs is, for instance, /var/log/debug, containing the following structure:

/var/log/debug/1/logs/debug.log
/var/log/debug/2/logs/debug.log
/var/log/debug/3/logs/debug.log

You can retrieve the contents with the desired output of all files [in order], with:

cd /var/log/debug
for dir in *; do
    cat ${dir}/logs/debug.log | awk '{print "'${dir}': " $0}';
done