How to switch/rotate every two lines with sed/awk?

前端 未结 4 617
时光取名叫无心
时光取名叫无心 2020-12-15 08:47

I have been doing this by hand and I just can\'t do it anymore-- I have thousands of lines and I think this is a job for sed or awk.

Essentially, we have a file lik

4条回答
  •  死守一世寂寞
    2020-12-15 08:54

    sed 'N; 
    s/\(.*\)\n\(.*\)/\2\
    \1/' infile
    

    N - append the next line of input into the pattern space
    \(.*\)\n\(.*\) - save the matching parts of the pattern space the one before and the one after the newline.
    \2\\ \1 - exchange the two lines (\1 is the first saved part, \2 the second). Use escaped literal newline for portability

    With some sed implementations you could use the escape sequence \n: \2\n\1 instead.

提交回复
热议问题