86

I am a newbie at shell scripting, and am confused about how to use sed or any other tools to replace the first line in my text file by a string. Here are the text file contents:

/home/snehil/Desktop/j1/movie.MOV
"spome other text lines'

I want to replace the first line (movie file path) with just movie.MOV (could be a variable in the shell script)

Please guide me how to do this. I came across sed in some posts, do I need to use sed here?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Rookie
  • 5,179
  • 13
  • 41
  • 65

2 Answers2

175

sed is the right tool, try doing :

var="movie.MOV"
sed -i "1s/.*/$var/" file.txt

explanations

  • 1 mean first line
  • the rest is the substitution s/// : we substitute everything (.*) by the $var variable
  • the double shell quotation is mandatory here

Learn how to quote properly in shell, it's very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    You could also use `1s:.*/::` to strip everything but the file name, if that's what the asker actually wants. – Dietrich Epp Nov 18 '12 at 06:59
  • 2
    Hello Sputnick, I am trying your solution: but i get the folwing error char 9: unknown option to `s' ............. this is my code.......... #!/bin/sh topDirectoryPath="$1" metaFileName="movie_meta.txt" replaceName="$2" myMetaFilePath="$topDirectoryPath$metaFileName" sed "1s/.*//$replaceName/" /home/snehil/Desktop/j1/Erasure/Coding/movie_meta.txt – Rookie Nov 18 '12 at 07:17
  • I want to replace the first line in myMetaFilePath file to ${2} (which is movie.mOV) ...can u give advice on what could be wrong here? thanks – Rookie Nov 18 '12 at 07:20
  • As I said, there was a typo, copy & paste one more time – Gilles Quénot Nov 18 '12 at 07:27
  • 1
    Thanks! I got the new contents of the file 'echoed' in the terminal... but how do I write this into the same file? i.e write the new contents in the same file? I tried saving the sed in a variable and using > operator.. doesnt wrk :( – Rookie Nov 18 '12 at 07:54
  • 9
    @Rookie you can make `sed` edit the file in place with the `-i` option e.g. `sed -i '.bak' "1s/.*/${var}/" file.txt` `sed` will edit it in-place and create a backup file ( `.bak` ). If you don't need a backup file simply give `sed` a blank parameter for the `-i` option as in: `sed -i '' "1s/.*/${var}/" file.txt` – Sam Figueroa Jan 06 '16 at 06:42
  • It would be great if the answer is updated with the `-i` option as @SamFigueroa suggests in its comment. The OP is asking for this as it is stated in its last comment of this answer. – jchanger Apr 06 '17 at 15:41
  • 1
    what should I do if my new line contains "/"? – Shrikant Shete Mar 18 '18 at 21:51
  • 1
    You can pick one of most of ASCII characters as a delimiter – Gilles Quénot Mar 18 '18 at 21:54
  • Add empty quotes `-i ''` to make the command work on both Linux and Mac. Mac requires the argument for `-i` while in GNU sed it's optional. – Sampo Apr 28 '22 at 19:28
5

You can do this easy with tail:

#I want to replace the first line
cp test.txt test.txt.backup
echo 'mynewfirstline'> test.txt
#add everything from old file starting from second line
cat test.txt.backup |tail -n+2>> test.txt
drvisor
  • 71
  • 1
  • 2