3

This question already has an answer here:

I have a text file,of 100 lines,i need to move line 40 to line 39(refer to position) How to do this with sed? I tried

sed '40 m 39' file

and

sed '40,39m' file

but give me error. Thanks

marked as duplicate by don_crissti, jasonwryan, Gilles, Michael Homer, Stephen Kitt Mar 22 '15 at 22:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

With GNU sed:

seq 1 43 | sed -e '39{h;d};40{p;g}' | tail -n 8

Output:

36
37
38
40
39
41
42
43
1

Try:

$ sed -e '39N;s/\(.*\)\n\(.*\)/\2\
\1/' file
  • Sorry but doesn't work – elbarna Mar 22 '15 at 18:11
  • Give error sed: -e expression #1, char 23: invalid reference \2 on `s' command's RHS – elbarna Mar 22 '15 at 18:20
  • @elbarna: sorry, my typo, if you have GNU sed, try sed -e '39N;s/\(.*\)\n\(.*\)/\2\n\1/' file – cuonglm Mar 22 '15 at 18:22

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