Search code examples
linuxselectsedlinemove

Sed move a line


I just need to move a line up in sed. I can select the line with

sed -i '7s///'

I need to move line 7 up 2 lines so it will be line 5.

I can't find anything on the internet to do this without complicated scripts, I can't find a simple solution of moving a specific line a specific number of times.


Solution

  • seq 10|sed '5{N;h;d};7G'
    

    when up to line 5 append next line(line 6) into pattern space then save them into hold space and delete them from pattern space; up to line 7 then append the hold space content("5\n6") behind the line 7; now, pattern space is "7\n5\n6";finally,sed will print the pattern space at the end of current cycle by default(if no "-n" parameter)