3

I would like to be able to cut line X and insert the line X on position y (top). This would help me to be able to reorganize some config files.

Text example line number 1 <--- Append line 10 above line 1
Text example line number 2
Text example line number 3
Text example line number 4
Text example line number 5
Text example line number 6
Text example line number 7
Text example line number 8
Text example line number 9
Text example line number 10 <--- Cut or hold in buffer
Text example line number 11
Text example line number 12
Text example line number 13

so far the only thing I have found is:

# Cuts line 10 and adds it to the end of file    
sed -n -e '10h;10!p;${g;p;}' file-01.txt

But I don't know how to place the hold content on another line than the last.

Unfortunately I'm not so familiar with sed. There are similar questions on stackoverflow and here but I was not able to modify them to meet my needs.

I would prefer a solution with sed but if it can be achieved easier with other tools like awk than this solutions are also welcome.

  • See Need to move the last line of the file to second line of the same file. I recommend ed for this job. – don_crissti Nov 22 '15 at 18:58
  • 1
    sed -n '10p' file > newfile; sed '10d' file >> newfile – Cyrus Nov 22 '15 at 19:08
  • @Cyrus: Like your solution. To do it inplace, we can do this after your command. ; mv newfilw file-01.txt – CHID Nov 23 '15 at 7:37
  • @don_crissti Thanks for the hint. Surprisingly I never stumbled across this thread. – caracal Nov 23 '15 at 12:39
  • @Cyrus Its a nice and easy solution and with the last step mentioned by CHID it solves my issue completely. Thanks guys! – caracal Nov 23 '15 at 12:45
2

Using sed

Let's create a test file:

$ seq 10 >file

To move, say, line 5 to the top of a file:

$ sed '1,4{H;1h;d}; 5{p;x}' file
5
1
2
3
4
6
7
8
9
10

How it works

  • 1,4{H;1h;d}

    This saves lines 1 through 4 in the hold space.

    In more detail, H appends the current line to the hold space after appending a newline to the hold space. For line 1, we don't want that newline, so we issue the command 1h which sends line 1 to the hold space with no newline. The d command deletes the current line from the pattern space so that it doesn't get printed yet.

  • 5{p;x}

    This prints line 5 and then prints the hold space. p tells sed to print the pattern space (which contains line 5). x tells sed to put to swap the pattern and hold space so that what was in the hold space will now be printed.

Lines after line 5 are passed through unchanged.

Using awk

$ awk 'NR<5{s=s ORS $0;next} NR==5{print $0 s;next} 1' file
5
1
2
3
4
6
7
8
9
10

The logic here is quite similar to that for sed. We save all lines before line 5 in the variable s. When we reach line 5, we print it followed by s. The remaining lines are passed through unchanged.

  • Thanks for your answer. Your solution is my favorite. I like the sed solution and thank you for the explanation, it helps me a lot to understand sed better. The awk solution is as well very handy. awk is going to be my next learning step... – caracal Nov 23 '15 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.