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

前端 未结 5 2051
陌清茗
陌清茗 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 15:04

    Well, guess I should have taken a closer look at using Records in awk when I was trying to figure this out last night... 10 minutes after looking at them I got it working. For anyone interested here's how I did this: In my original sed script I put an extra newline infront of the beginning of each record so there's now a blank line seperating each one. I then use the following awk command:

    awk 'BEGIN {RS = ""; FS = "\n"}
    {
    if (NF >= 3)
    for (i = 3; i <= NF; i++)
    print $1,$2,$i
    }'

    and it works like a charm outputting exactly the way I wanted!

提交回复
热议问题