Sort a text file by line length including spaces

后端 未结 11 2130
故里飘歌
故里飘歌 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:29

    The length() function does include spaces. I would make just minor adjustments to your pipeline (including avoiding UUOC).

    awk '{ printf "%d:%s\n", length($0), $0;}' "$@" | sort -n | sed 's/^[0-9]*://'
    

    The sed command directly removes the digits and colon added by the awk command. Alternatively, keeping your formatting from awk:

    awk '{ print length($0), $0;}' "$@" | sort -n | sed 's/^[0-9]* //'
    

提交回复
热议问题