| Home Profile Fun |
#161 Linux 28.08.2008
Bash scripts for text file manipulationsI used these scripts in an environment which had a lot of text files. Too much to work on them manually. They do some basic tasks like inserting lines, merging text files and so on with all files contained in the directory "files". The first script inserts the content of another text file at the beginning.
#!/bin/bash
for i in ./files/*
do
cat sampletext.txt > ${i}_tmp
cat $i >> ${i}_tmp
mv ${i}_tmp $i
done
exit
Find a certain line with grep and extract the line number. Then delete all lines from this line(including) until line 2000. #!/bin/bash for i in ./files/* do LINENUMBER=`grep -n "some text" $i | sed 's/:.*//'` sed -i -e "$LINENUMBER,2000d" $i done exit Find the html tag <h1> and extract the content of it. Put it into the html tag <title>. #!/bin/bash for i in ./files/* do H1=`grep "<h1>" $i | sed -e 's/^.*<h1>//' | sed -e 's/<\/h1.*$//'` sed -i -e "s/<title>.*<\/title>/<title>$H1<\/title>/" $i done exit Take the first 20 lines, write them into a temporary file. Then append a line at the end and finally take the content from the original file from line 22(including) and append it as well.
#!/bin/bash
for i in ./files/*
do
head -n20 $i > ${i}_tmp
echo -e "some text" >> ${i}_tmp
tail -n +22 $i >> ${i}_tmp
mv ${i}_tmp $i
done
exit
Also very helpful for those operations is the command "tac". It reverses all lines so that the file starts with the last line. |