ASIC/FPGA Design and Verification Out Source Services
sed to add a line to a file after a string match
A simple script to find a pattern in a file and than add a line to the file.
- When I needed to add a new button to all pages in this site, I used this script.
- The requirement is simple: per a given file, check if an input string is found in the file. If it does, add the new line after the line with the match.
- I do it on all files by the following command:
find . -name "*.html" | xargs -n1 | xargs -i= ~/bin/add_html_button.unx =
- The script is shown below:
#!/bin/bash
sed '/match string/ a new string for new added line ' $1 > Tmp
\mv -f Tmp $1
-
Recently I saw that this can be done in one step:
In this example I put perl line in remark by adding # at the beginning of the line.
sed -i "s/^\(\&ads_0001;\)/#\1/" Home_2/udp_code.pl
-
If any of the strings contains forward slash, one can use another character as
the replace separator.
sed -i "s,^\(\&ads_0001;\),#\1/," Home_2/udp_code.pl
sed -i "s;OLD_SWITCH;$HTML_SWITCH;" Home_2/udp_code.pl
Please let me know what you think on my site.
Contact me now at: |