Question

I have a file which looks like that:

sudo apt-get install rar
sudo apt-get install gimp
sudo apt-get install gnome-tweak-tool
sudo apt-get install unity-tweak-tool
sudo apt-get install pidgin

I want to somehow add "-y" in the end of each line, how is it done? Thanks

Was it helpful?

Solution

sed -i 's:$: -y:' YOURFILE

Will do it for you.

  1. -i does the modification "in place", so no new file created (actully there's a tmp file)
  2. s substitute
  3. :delimiter
  4. $ end of line
  5. see the 3. point
  6. -y replacement

OTHER TIPS

Assuming you want to add -y (change it as you deem appropriate) at the end of each line, you can use sed by saying

$ cat file
sudo apt-get install rar
sudo apt-get install gimp
sudo apt-get install gnome-tweak-tool
sudo apt-get install unity-tweak-tool
sudo apt-get install pidgin

$ sed 's/$/ -y/' file
sudo apt-get install rar -y
sudo apt-get install gimp -y
sudo apt-get install gnome-tweak-tool -y
sudo apt-get install unity-tweak-tool -y
sudo apt-get install pidgin -y

This prints on standard out. If you wish to make in-place changes inside the file, you can use -i option of sed by saying

sed -i 's/$/ -y/' file  

or redirect the output to another file by doing

sed 's/$/ -y/' file > newfile

If you are vi mode you can try

:%s/$/text_to_be_added/g and press "Enter"

If you are bash mode you can try

sed 's/$/text_to_be_added/g' filename
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top