Concise and portable “join” on the Unix command-line

前端 未结 9 1931
囚心锁ツ
囚心锁ツ 2020-11-29 00:53

How can I join multiple lines into one line, with a separator where the new-line characters were, and avoiding a trailing separator and, optionally, ignoring empty lines?

9条回答
  •  眼角桃花
    2020-11-29 01:02

    I had a log file where some data was broken into multiple lines. When this occurred, the last character of the first line was the semi-colon (;). I joined these lines by using the following commands:

    for LINE in 'cat $FILE | tr -s " " "|"'
    do
        if [ $(echo $LINE | egrep ";$") ]
        then
            echo "$LINE\c" | tr -s "|" " " >> $MYFILE
        else
            echo "$LINE" | tr -s "|" " " >> $MYFILE
        fi
    done
    

    The result is a file where lines that were split in the log file were one line in my new file.

提交回复
热议问题