Guest User

Untitled

a guest
Jan 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Check if first and second parameters exist
  4. if [ ! -z "$2" ]; then
  5. STRING=$(cat $1)
  6. # Check if the supplied file exist
  7. if [ -e $2 ]; then
  8. sed -i -e "2i$STRING" $2
  9. echo "The string "$STRING" has been successfully inserted."
  10. else
  11. echo "The file does not exist."
  12. fi
  13. else
  14. echo "Error: both parameters must be given."
  15. fi
  16.  
  17. first_line
  18. second_line
  19.  
  20. REAL_FIRST_LINE
  21. REAL_SECOND_LINE
  22.  
  23. sed: -e expression #1, char 24: unterminated `s' command
  24. The string "first_line
  25. second_line" has been successfully inserted.
  26.  
  27. REAL_FIRST_LINE
  28. first_line
  29. second_line
  30. REAL_SECOND_LINE
  31.  
  32. sed "1r $1" "$2"
  33.  
  34. cat "$1" | sed '2r /dev/stdin' "$2"
  35.  
  36. r filename
  37. As a GNU extension, this command accepts two addresses.
  38.  
  39. Queue the contents of filename to be read and inserted into the output stream
  40. at the end of the current cycle, or when the next input line is read. Note that
  41. if filename cannot be read, it is treated as if it were an empty file, without
  42. any error indication.
  43.  
  44. As a GNU sed extension, the special value /dev/stdin is supported for the file
  45. name, which reads the contents of the standard input.
  46.  
  47. $ sed '1r content.txt' example.txt
  48. REAL_FIRST_LINE
  49. first_line
  50. second_line
  51. REAL_SECOND_LINE
Add Comment
Please, Sign In to add comment