grep, sed, awk

categories:

Let’s continue with the basics… Reviewing only the really useful, and not going into the deep

If you have some useful tips, please share ! :)

Grep

grep : print lines matching a pattern (equals “grep -G” which is the default) egrep : equals grep -E (interpret extended regexp)

grep -n : line numbered grep -i : ignore case grep -c : count matches grep -v : print non-matching lines grep -r : recursivity, read all files under each directory

grep pattern file.txt Will display the lines containing the pattern

grep -c pattern file.txt Will display how many lines contain the pattern

grep -i pattern file.txt Will display the lines containing the pattern regardless of the case

grep -A 1 -B 1 pattern output.txt Will print the one line before (-B) and one line after (-A) the matching pattern

Awk

awk '{ print $0 }' file Output the content of the file

awk '{ print $2 }' file Output the second field of data of the file, space is the default separator

awk -F ':' '{ print $2 }' file Same but separator is “:”

Sed cat file | sed -e 's/old_pattern/new_pattern/g' sed would replace old_pattern by new_pattern in the output

cat file | sed -e '4,10s/old_pattern/new_pattern/g' sed would replace old_pattern by new_pattern in the output between line 4 and 10

cat file | sed '/pattern/d' Delete a pattern

cat file | sed '/pattern/!d' Delete everything but the pattern (this equals grep “string_to_remove”)




Thanks for reading this post!


Did you find an issue in this article?

- click on the following Github link
- log into Github with your account
- click on the line number containing the error
- click on the "..." button
- choose "Reference in new issue"
- add a title and your comment
- click "Submit new issue"

Your feedback is much appreciated! πŸ€œπŸΌπŸ€›πŸΌ

You can also drop me a line below!