Sed , Awk , Cut
Hello Every One , today I would like to explain basics of these 3 Linux commands.
SED
sed command is used to replace one word in a file with any word specified.
Suppose content of file.txt is
cats are nice pets
Suppose we want to replace cats with dogs then sed command is used to help us.
sed 's/cats/dogs/' file.txt
Simple , here s
is used to search the word cats and sed command replaces all the occurrences of cats with dogs, also /
is used as delimiter, works fine with any other symbols as well.
sed 's:cats:dogs:' file.txt # Works same as above
But doing like this doesn't modify the content of the file .
sed -i 's cats dogs ' file.txt # Now space is delimiter
Doing like this replaces content of the file.
AWK
Awk command is used to filter out specific part in a file.
Let us suppose if we want to filter out 3rd word goutham from every file listed in the hoe directory, then awk comes to help.
ls -l | awk '{print $3}'
output of ls command is passed as input to awk command, this command goes through every line and filters out 3rd word and displays. Simple.
By default awk command uses space as delimiter we can mention our own delimiter by using -F .
ls -l | awk -F '-' '{print $3}'
Now it treats -
instead of space as delimiter.
We can even mention file name from which it can filter out.
awk -F ':' '{print $2}' file.txt
We can write our own bash script in place of print
CUT
cut command is also used to filter out the specific words from the file.
There are different options like -b for bytes , -c for characters , -d delimiter, -f for fields.
Suppose we want to list out 3rd character from the ls -l output.
ls -l | cut -b 3 # -c also does the same
-f is for words
ls -l | cut -f 3 # filters 3rd word from outp
ls -l | cut -d '/' -f 3
filters 3rd word from ls-l output treating / as delimiter.
Same as awk we can it can also filters text from file by mentioning name of the file.
awk is advanced version of cut
That's it thanks for reading . Please let me know if you found any thing wrong or anything I missed.
Subscribe to my newsletter
Read articles from KORLA GOUTHAM directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
KORLA GOUTHAM
KORLA GOUTHAM
I am an Undergraduate having having a good Knowledge in Frontend development with React.js,competetive programming with C++, AWS fundementals.