Using awk (or sed) to remove newlines based on first character of next line

前端 未结 5 2049
陌清茗
陌清茗 2021-02-15 14:23

here\'s my situation: I had a big text file that I wanted to pull certain information from. I used sed to pull all the relevant information based on regexp\'s, but each \"piece\

5条回答
  •  盖世英雄少女心
    2021-02-15 14:42

    This might work for you:

    # sed ':a;N;s/\n,/,/;ta;P;D' test.dat | sed 's/,/\n/5;s/\(.*,\).*\n/&\1/'
    92831,499,000,0644321
    79217,999,000,5417178,PK91622
    79217,999,000,5417178,PK90755
    

    Explanation:

    This comes in two parts:

    Append the next line and then if the appended line begins with a , , delete the embedded new line \n and start again. If not print upto the newline and then delete upto the new line. Repeat.

    Replace the 5th , with a new line. Then insert the first four fields inbetween the embedded newline and the sixth field.

提交回复
热议问题