user248237
user248237

Reputation:

piping files in unix for "wc" while retaining filename

I have a bunch of files of the form myfile[somenumber] that are in nested directories.

I want to generate a line count on each of the files, and output that count to a file.

These files are binary and so they have to be piped through an additional script open_file before they can be counted by "wc". I do:

ls ~/mydir/*/*/other_dir/myfile* | while read x; do open_file $x | wc -l; done > stats

this works, but the problem is that it outputs the line counts to the file stats without saying the original filename. for example, it outputs:

100
150

instead of:

/mydir/...pathhere.../myfile1: 100
/mydir/...pathhere.../myfile2: 150

Second question:

What if I wanted to divide the number of wc -l by a constant, e.g. dividing it by 4, before outputting it to the file?

I know that the number of lines is a multiple of 4 so the result should be in an integer. Not sure how to do that from the above script.

how can I make it put the original filename and the wc -l result in the output file?

thank you.

Upvotes: 1

Views: 1633

Answers (2)

sarnold
sarnold

Reputation: 104080

Try this:

while read x; do echo -n "$x: " ; s=$(open_file $x | wc -l); echo $(($s / 4));

You've thrown away the filename by the time you get to wc(1) -- all it ever sees is a pipe(7) -- but you can echo the filename yourself before opening the file. If open_file fails, this will leave you with an ugly output file, but it might be a suitable tradeoff.

The $((...)) uses bash(1) arithmetic expansion. It might not work on your shell.

Upvotes: 3

Ray
Ray

Reputation: 1869

You can output the file name before counting the lines: echo -n "$x: " ; open_file $x | wc -l. The -n parameter to echo omits the trailing newline in the output.

To divide integers, you can use expr, e.g., expr $(open_file $x | wc -l) / 4.

So, the complete while loop will look as follows:

while read x; do echo -n "$x: " ; expr $(open_file $x | wc -l) / 4 ; done

Upvotes: 5

Related Questions