Find a Line in a File and Add Something to The End of The Line in Bash

Find a line in a file and add something to the end of the line in bash

Given the following:

Textfile:

[root@yourserver ~]# cat text.log 
#This is your hosts file

127.0.0.1 localhost linux
[root@yourserver ~]#

bash script:

[root@yourserver ~]# cat so.sh 
#!/bin/bash

_IP_TO_FIND="$1"

# sysadmin 101 - the sed command below will backup the file just in case you need to revert

_BEST_PATH_LINE_NUMBER=$(grep -n "${_IP_TO_FIND}" text.log | head -1 | cut -d: -f1)
_LINE_TO_EDIT=$(($_BEST_PATH_LINE_NUMBER+2))
_TOTAL_LINES=$( wc -l text.log)
if [[ $_LINE_TO_EDIT -gte $_TOTAL_LINES ]]; then
# if the line we want to add this to is greater than the size of the file, append it
sed -i .bak "a\${_LINE_TO_EDIT}i#This is added automatically\n\n192.168.1.2 domain1. com" text.log
else
# else insert it directly
sed -i .bak "${_LINE_TO_EDIT}i\#This is added automatically\n\n192.168.1.2 domain1. com" text.log
fi

Usage:

bash ./so.sh 127.0.0.1

Simply enter in the ip address you're trying to find and this script matches on the first occurrence of it.

Hope this helps!

How to append a string at end of a specific line in a file in bash

Using sed and the pattern described:

sed '/192.168.1.2/s/$/ myalias/' file

Using sed and a specific line number:

sed '2s/$/ myalias/' file

How to add to the end of lines containing a pattern with sed or awk?

This works for me

sed '/^all:/ s/$/ anotherthing/' file

The first part is a pattern to find and the second part is an ordinary sed's substitution using $ for the end of a line.

If you want to change the file during the process, use -i option

sed -i '/^all:/ s/$/ anotherthing/' file

Or you can redirect it to another file

sed '/^all:/ s/$/ anotherthing/' file > output

Bash how to append word to end of a line?

You can match $ to append to a line, like:

sed -e 's/$/ eth0/'

EDIT:

To loop over the lines, I'd suggest using a while loop, like:

while read line
do
# Do your thing with $line
done < <(grep address file.txt | cut -d'=' -f2 | tr ':' ' ' | sed -e 's/$/ eth0')

How to add text at the end of each line in unix

There are many ways:

sed: replace $ (end of line) with the given text.

$ sed 's/$/ | COUNTRY/' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

awk: print the line plus the given text.

$ awk '{print $0, "| COUNTRY"}' file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

Finally, in pure bash: read line by line and print it together with the given text. Note this is discouraged as explained in Why is using a shell loop to process text considered bad practice?

$ while IFS= read -r line; do echo "$line | COUNTRY"; done < file
india | COUNTRY
sudan | COUNTRY
japan | COUNTRY
france | COUNTRY

In Bash, how do I add a string after each line in a file?

If your sed allows in place editing via the -i parameter:

sed -e 's/$/string after each line/' -i filename

If not, you have to make a temporary file:

typeset TMP_FILE=$( mktemp )

touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename

Add text after end line between N lines in a file with SED

This might work for you (GNU sed):

sed -i '1,15s/$/ cd directory2\//' file

In the range 1 to 15, append cd directory2/ only.

Bash shell script: appending text to a specific line of an existing file without line break

awk '/Options=/ && ! /nodev/ {print $0 ",nodev"; next};1' file

no need to get the line number, just append the ",nodev" to the corresponding line



Related Topics



Leave a reply



Submit