How to merge every two lines into one from the command line?

后端 未结 21 2179
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 15:14

I have a text file with the following format. The first line is the \"KEY\" and the second line is the \"VALUE\".

KEY 4048:1736 string
3
KEY 0:1772 string
1         


        
21条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 15:39

    A more-general solution (allows for more than one follow-up line to be joined) as a shell script. This adds a line between each, because I needed visibility, but that is easily remedied. This example is where the "key" line ended in : and no other lines did.

    #!/bin/bash
    #
    # join "The rest of the story" when the first line of each   story
    # matches $PATTERN
    # Nice for looking for specific changes in bart output
    #
    
    PATTERN='*:';
    LINEOUT=""
    while read line; do
        case $line in
            $PATTERN)
                    echo ""
                    echo $LINEOUT
                    LINEOUT="$line"
                            ;;
            "")
                    LINEOUT=""
                    echo ""
                    ;;
    
            *)      LINEOUT="$LINEOUT $line"
                    ;;
        esac        
    done
    

提交回复
热议问题