How to Add a Line to a File in a Shell Script

How can I add a line to a file in a shell script?

This adds custom text at the beginning of your file:

echo 'your_custom_escaped_content' > temp_file.csv
cat testfile.csv >> temp_file.csv
mv temp_file.csv testfile.csv

Adding a line to a file using sed in a shell script

Jonathan already mentioned the potential issues with using sed -i (non-standard, behaves in different ways when supported depending on implementation, etc.). Avoid them by using ed to edit files:

ed -s Test.txt <<EOF
109a
This is the string
.
74d
w
EOF

Note how this appends, and then deletes. Because ed acts on entire files, not a stream of lines, commands to act on specific lines can be in any order.

Bash: Inserting a line in a file at a specific location

If you want to add a line after a specific string match:

$ awk '/master.mplayer.com/ { print; print "new line"; next }1' foo.input
ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900
new line
ServerActors=UWeb.WebServer

Add a new line of text at the top of a file in bash shell

You can use this BSD sed command:

sed -i '' '1i\
one
' file

-i will save changes inline to file.


If you want to add a line at the top if same line is not already there then use BSD sed command:

line='one'

sed -i '' '1{/'"$line"'/!i\
'"$line"'
}' file

How to insert a string into a line in a file after a key word in bash

A pure sed solution:

sed -r "s:(THEKEYWORD):\1 $(sed ':a;N;$!ba;s/\n/ /g' test.txt) :g" insert.txt

Where;

  • test.txt is the value to be inserted, $(sed ':a;N;$!ba;s/\n/ /g' test.txt) removes any newlines from the file, so it can be inserted on the same line
  • insert.txt the text file where THEKEYWORD exists

If you wish to replace the file, use the -i option;

sed -i -r "s:(THEKEYWORD):\1 $(gsed ':a;N;$!ba;s/\n/ /g' test.txt) :g" insert.txt

As @KamilCuk pointed out, using paste -sd ' ' test.txt could be used to remove the newlines, and insert the file;

sed -r "s:(THEKEYWORD):\1 $(paste -sd ' ' test.txt) :g" insert.txt

Terminal output + sed version

Shell script to to check if a line exist in a file

I see 2 issues in your code:

  1. if [[ $isInFile == 0 ]]; --If condition should not terminate with ;. Remove that.
  2. The expression you are checking is always an empty string. Try echo $isInFile. What you are checking is output of the command, not its return value. Instead, you should remove -q from your grep expression and check if the output is empty or not.

Following code should work:

isInFile=$(grep '^export' /etc/bashrc)
if [ -z "$isInFile" ]
then
echo "line is not present";
echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
source /etc/bashrc;
else
echo "line is in the file";
fi

-z check for emptiness of variable.

Insert multiple lines into a file after specified pattern using shell script

Another sed,

sed '/cdef/r add.txt' input.txt

input.txt:

abcd
accd
cdef
line
web

add.txt:

line1
line2
line3
line4

Test:

sat:~# sed '/cdef/r add.txt' input.txt
abcd
accd
cdef
line1
line2
line3
line4
line
web

If you want to apply the changes in input.txt file. Then, use -i with sed.

sed -i '/cdef/r add.txt' input.txt

If you want to use a regex as an expression you have to use the -E tag with sed.

sed -E '/RegexPattern/r add.txt' input.txt

comment a line using search pattern and insert new line shell script

With GNU sed:

search_variable="Dlog4j2.formatMsgNoLookups"
new_variable="wrapper.java.additional.47=-Dlog4j2.formatMsg=false"

sed -i "s/.*${search_variable}.*/#&\n${new_variable}/" testfile.txt

Output to testfile.txt:

#wrapper.java.additional.47=-Dlog4j2.formatMsgNoLookups=true
wrapper.java.additional.47=-Dlog4j2.formatMsg=false

For the meaning of & see using sed with ampersand (&).

The curly brackets can also be omitted in this case.

This can also be helpful: Difference between single and double quotes in bash

insert line into a new file in linux using shell script

echo 'new line' >file_name

Also, you can append to the end without using sed using the >> operator:

echo 'additional line' >>file_name


Related Topics



Leave a reply



Submit