Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. Given an input file , reverse every two lines of the file and print the output.
  2.  
  3. Ex:-
  4. Input file content --
  5. $ cat st.txt
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 6
  12. 7
  13. 8
  14. 9
  15. 10
  16.  
  17. Output --
  18. 2
  19. 1
  20. 4
  21. 3
  22. 6
  23. 5
  24. 8
  25. 7
  26. 10
  27. 9
  28. -----------------------------------------------------------
  29. Solution 1:- Using awk command
  30. awk '{if(NR%2==1) { line=$0 } else { print $0"\n"line }}' st.txt
  31.  
  32. Solution 2:- Using sed command (looks elegant but complicated until you get your head around it)
  33. sed -n 'x;N;G;h;$p' st.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement