pattern matching - grep - sed -awk - find


AWK

Removing Punctuation

awk '{gsub(/[[:punct:]]/, "")} 1' RS='[[:space:]]' novel
cat input_file | tr -d '[:punct:]' > output_file


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

Sort 

Sort IP Addresses in order

cat ip.txt sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 > ip-sorted.txt


tr

Convert Lowercase to uppercase

tr [:lower:] [:upper:] < quotes | tee -a upper



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