Skip to content
Advertisement

compare the length of multiple files using awk or sed

I want to compare the number of lines of each file and choose the one that contains the maximum number of lines for example

filename_V1 |wc -l =100
filename_V2 |wc -l =19
filename_V3  |wc -l =10

myFile_V1  |wc -l =1
myFile_V2  |wc -l =10
myFile_V3  |wc -l =15

I will get as result

filename_V1
myFile_V3

Advertisement

Answer

Alternative with lot’s of piping

wc -l *_V* |              # generate the list
sed 's/_V/ _V/;$d' |      # separate baseline from versions, delete total
sort -k 2,2 -k 1,1nr |    # sort by names and size (reverse)
sort -k 2,2 -u |          # get the first by name (max by design)
sed 's/ _V/_V/' |         # reverse baseline name back to original
awk '{print $2}'           # extract the filename

this script assumes the file names are under your control and space or _V won’t appear in the base names. Otherwise check out @Qualia’s version.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement