Linux: search and replace commands

Linux: search and replace commands

Linux: search and replace commands 150 150 Roderick Derks

Search and replace one file with the same name in several directories
# find . -name "favicon.ico" -exec cp /username/images/favicon.ico {} \;
# find . -name "favicon.ico" -exec ls -al {} \;

Search and replace files with the same name or the same part of the name
# find . -name file.cfg.old -exec rename .cfg.old .cfg {} \;

Find files containing a certain string
# find . -type f -exec egrep -i "search_this_string" /dev/null {} \;

Search .c and .h for "search string" in this dir and below
# find -name "*.[ch]" | xargs grep -E "search string"

Finds all files created in the last day that are larger than 150GB.
# find / -mtime -1 -size +150000000 -print

Search and replace text in several files:
# perl -pi -e "s/search/replace/g;" *.txt

recursively:
# find ./ -exec perl -p -i -e ’s/search/replace/g’ {} \;
# find . -name '*.cfgl' -print0 | xargs -0 perl -pi -e 's/search/replace/g'

Add text from a file to al files in a directory:
for i in `ls -b .`; do cat ../add >> $i; done

Replace a certain string in a file by anther string from the same file using a variable:
for file in $(grep -l "vervang" *); do host_name=$(cat $file|grep host_name|grep -v verv|awk 'BEGIN {FS = "host_name"} {print $2}' |awk '{print $1}'|awk '{print $0}'); echo hostname is $host_name ; sed "s/vervang/${host_name}/g" $file >/tmp/$$ && mv /tmp/$$ $file;donefor file in $(grep -l "##HOSTNAME##" *); do host_name=$(cat $file|grep " host_name"|grep -v HOSTNAME|awk 'BEGIN {FS = "host_name"} {print $2}' |awk '{print $1}'|awk '{print $0}'); echo hostname is $host_name ; sed "s/##HOSTNAME##/${host_name}/g" $file >/tmp/$$ && mv -f /tmp/$$ $file;done

Search files that are:
newer than today 00:00 hr:
touch -t `date +%m%d0000` /tmp/$$| find . -type f -newer /tmp/$$

newer then today 14:00 hr:
touch -t `date +%m%d1400` /tmp/$$| find . -type f -newer /tmp/$$

recently new/updated files
find . -type f -printf "%TY-%Tm-%Td %TT %p\n" | sort | less

Search and remove a piece of text in a file
sed '/replace_this_piece_of_text/{
:loop
N
/till_this_piece_of_text/d
b loop
}' input_file.txt > output_file.txt

Or when using this in a script and using the same file for input and output (-i option for sed):

#! /bin/bash
function remove_it () {
sed -i ‘/Remove from/{
:loop
N
/Remove upto here/d
b loop
}’ $1
}
remove_it $1

Remove all lines starting with a hash

To remove all lines starting with hast sign (#) from a file, try this:
sed /^#/d filename

The above command will remove all lines starting with # from its output. If you want to write the output to another file, use o/p redirection >
sed /^#/d filename > newfile

But, if you want to over write the file with new o/p, use -i option with sed.
sed -i /^#/d filename

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Your Name (required)

    Your Email (required)

    Subject

    Your Message

      Your Name (required)

      Your Email (required)

      Subject

      Your Message