pattern matching - grep - sed -awk - find



GREP


Match exactly 8 characters.

grep -x '[_[:alnum:]]\{8\}' file.txt


SED

Change the first letter of each line to uppercase.

sed -e 's/^\(.\)/\U\1/g' words.txt  > words_first_letter_upper.txt

Delete blank lines

sed -i '/^$/d' wordlist.txt

  1. remove leading spaces:
sed -i -e's/^\s*//' breachcompilation.sorted.txt


FIND

Find all files less than 1024 MB and move them to a new directory.

find . -maxdepth 1 -type f -size -1024M -exec mv '{}' '../less-than-1GB/' \;


Find all files modified between certain dates

find / -type f -nerwermt 2017-03-15 ! -newermt 2017-03-20 2>/dev/null


Delete all files with the condition of 'with-count' anywhere in thee filename

find . -type f -name '*with-count*' -exec rm -rf {} \;


Merge larger files into one file efficiently

find . -name "*.txt" | xargs cat >> ./mergedfile.txt


For Loops

Find all filenames with 'wpa' regardless of case, and move to a given directory.

for i in $(ls | egrep -i '*wpa*'); do mv $i ../wireless; done

No comments:

Post a Comment