Sort a text file by line length including spaces

后端 未结 11 2131
故里飘歌
故里飘歌 2020-11-27 11:21

I have a CSV file that looks like this

AS2345,ASDF1232, Mr. Plain Example, 110 Binary ave.,Atlantis,RI,12345,(999)123-5555,1.56
AS2345,ASDF1232, Mrs. Plain Exampl         


        
11条回答
  •  失恋的感觉
    2020-11-27 11:24

    1) pure awk solution. Let's suppose that line length cannot be more > 1024 then

    cat filename | awk 'BEGIN {min = 1024; s = "";} {l = length($0); if (l < min) {min = l; s = $0;}} END {print s}'

    2) one liner bash solution assuming all lines have just 1 word, but can reworked for any case where all lines have same number of words:

    LINES=$(cat filename); for k in $LINES; do printf "$k "; echo $k | wc -L; done | sort -k2 | head -n 1 | cut -d " " -f1

提交回复
热议问题