0

I want to append the below string at the end of all lines in a file. I tried to search on web but could not get anything relevant. I used the below command too

sed -i -e "s?$?\$dest \$port \$fgt --pin '\$limit' --nogui;"? ipv42

Can someone please help me I want to append the below string in end of all lines in a file

$dest $port $fgt --pin '\$limit' --nogui;
4
  • Replace it with what? Or do you mean to append that text at the end of each line?
    – seshoumara
    Aug 17, 2022 at 7:48
  • Sorry I mean to append the text at the end of each line
    – ph3ro
    Aug 17, 2022 at 8:04
  • @seshoumara I have edited the post
    – ph3ro
    Aug 17, 2022 at 8:05
  • Is it \$limit or $limit you want in the replacement? Aug 17, 2022 at 8:11

2 Answers 2

2

One way can be using the heredoc, backslashed to inhibit variable expansion. What we are doing is providing a fully quoted string(multiline) heredoc.

sed -f - <<\_SED_ inputfile 
s?$?$dest $port $fgt --pin '$limit' --nogui;?
_SED_
1

Your sed script is almost working, except:

  1. the last delimiter of the s command, ?, should be inside the quotes
  2. you need to escape $ in the search part, because shell interprets $? as being the exit status of the last executed command.

Correct command:

sed -i -e "s?\$?\$dest \$port \$fgt --pin '\$limit' --nogui;?" ipv42
3
  • 1
    Or use single quotes (except for the literal single quotes) for quoting: sed -i 's/$/$dest $port $fgt --pin '\''$limit'\'' --nogui;/' ipv42 Aug 17, 2022 at 8:13
  • Good point. With single quotes you get rid of those pesky bash interpretations of the command before it reaches sed, I'm surprised the OP used ? as the delimiter of the search command in the first place.
    – seshoumara
    Aug 17, 2022 at 8:17
  • In this case, there is no real reason to use ? as the delimiter for the s command. Note too the requested escaping of the last $.
    – Kusalananda
    Aug 17, 2022 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .