0

I have the following file.txt that follows the same pattern and I want to modify it where this file is by adding an ip:

# gfhfhgfh
gfhfghgfhgfhgfh
MACs 
# access
USER CONSOLA *,!10.249.247.3,!10.249.245.65
/bin/false

I want to add an ip in the end of the line that contains as patron USER CONSOLE:

 USER CONSOLA *,!10.249.247.4,!10.249.245.65,!10.249.245.90,

I only manage to add the ip in the whole document at the moment but not in that particular line the code used is

sed 's/\r\?$/,!10.10.11.1/' file.txt 
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
ortiga
  • 131
  • 7

2 Answers2

2

Match at the start of the line (^ anchor) and substitute the new text at the end of the line ($ anchor):

$ sed '/^USER CONSOLA/ s/$/,!10.10.11.1/' file.txt
# gfhfhgfh
gfhfghgfhgfhgfh
MACs 
# access
USER CONSOLA *,!10.249.247.3,!10.249.245.65,!10.10.11.1
/bin/false

If your file has Windows/DOS style CRLF line endings that you wish to preserve, modify the above to

sed '/^USER CONSOLA/ s/\r$/,!10.10.11.1\r/' file.txt

If you don't wish to preserve the DOS endings, then either remove them first with dos2unix or by adding an additional command to do that in sed:

sed -e 's/\r$//' -e '/^USER CONSOLA/ s/$/,!10.10.11.1/' file.txt
steeldriver
  • 81,074
-1

Used below method to achieve same

sed  "/USER CONSOLA/s/.*/&\,\!10.249.245.90,/g" filename

output

# gfhfhgfh
gfhfghgfhgfhgfh
MACs
# access
USER CONSOLA *,!10.249.247.3,!10.249.245.65,!10.249.245.90,
/bin/false