Trying to add a char to the end of each line in a given file, how is it done?

StackOverflow https://stackoverflow.com/questions/23603081

  •  20-07-2023
  •  | 
  •  

문제

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

도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top