grep, sed, awk: string search: Linux basic ( part 1 )

HackPeas Freelancers
4 min readJul 1, 2022

In this blog post, I describe three Linux commands grep, sed, and awk, and their use for grabbing string characters.

grep

grep command is used to find out all the pattern matches for the provided string from the given file/directory/subfiles in the directory.

grep => getting string from a file

say we have a file.txt and we need to search word “Hackpeas” in it, we can search for the string using the below command

$ cat file.txt | grep “Hackpeas”

as you can see, grep command only give result => “Hackpeas” , but in the file.txt “hackpeas” is also there. means grep command is case sensitive.

grep => getting string from all the files and directories

for this we can use the below command

$ grep -iR “hackpeas” 2>/dev/null

here 2>/dev/null is to ignore any error and to stop putting error to output.

in the above result for “hackpeas” , “hackpeas” and “Hackpeas” both are coming because we included

  • -i flag which means => ignore-case
  • -R flag which means => recursive is a directory instead of file

grep => to print a few extra lines before and after the match

$ cat file.txt | grep -A 3 -B 2 “hackpeas” 
  • -A flag => is used to define a number of lines we want to print after the match
  • -B flag => is used to define a number of lines we want to print before the match

grep => to print everything except the line in which the given string is present

$ cat file.txt | grep -v “hackpeas”

sed

Sed command is used to print the modified results or to modify the content of files,

it is generally used to replace some matching pattern with other patterns

sed => replacing the content of file and printing output [ without editing the file ]

$ sed ‘s/testing/hackpeas/g’ file.txt

Note: above command won’t change anything in the file, it just replaces the string and prints the new output to the terminal.

sed => [ editing the file ] replace all matching strings

to replace the content of the file [ edit ], so we have to use -i ( — in-place ) flag.

$ sed -i ‘s/testing/hackpeas/g’ file.txt

sed => editing files and subfiles in sub directories

there is no recursive feature ( flag ) in sed binary, so we can integrate sed command with find command to get the work done

$ find . -type f -exec sed -i ‘s/hackpeas/testing/g’ {} +

AWK

awk search for the given pattern or string or specified position, then it will perform the mentioned action on it. it is a very powerful tool

usage => awk ACTION file_name

printing only the first character of each line

$awk ‘{print $1}’ file.txt

printing only second character of each line

$ awk ‘{print $2}’ file.txt

printing non empty line only =>

$ awk 'NF > 0' file.txt

now we can use this command to create a new file, which has no empty lines

$ aws 'NF > 0' file.txt 1>output.txt

we can use awk with other command to print some part of output, which we want

for example

$ ls -l 

the command is giving much information, but we want only the owner's name

$ ls -l | awk '{print $3}'

You can find out much more useful information about awk on this tutorials point page: https://www.tutorialspoint.com/unix_commands/awk.htm#

If you like the blog, please give a clap and follow me for more content.

For more CTFs and bug bounty writeup, or content related to ethical hacking, android penetration testing. follow me on:

Medium: https://hackpeas.medium.com

Youtube: https://www.youtube.com/channel/UC17W_Ircv7EmIIdbJeOQ_BQ
Instagram: https://www.instagram.com/hackpeas/
Linkedin: https://www.linkedin.com/in/viraj-vaishnav-19b0a61aa/
Twitter: https://twitter.com/VirajVaishnav16

Thank You..

--

--

HackPeas Freelancers

We provide the best technical services on a reasonable budget