cut, awk, sed and pipe in shell script.

If you want to know basic details on what is shell, shell script and bash then please refer blog here.

To get the type of command use type keyword: type -a command name

CUT:

It prints the selected parts of line.It prints only bytes or characters or word depending on single character delimiter

syntax: cut option [file]

  • -d: it defines the delimiter
  • -f : cut and print the value before delimiter
  • -c: cut and print the character
  • 1: defines prints first value
  • delimiter should be a single character
  • > : creating a text file with data and >> used for appending the content to the text file
cut -c 1: prints o and cut -d ‘/’ -f 1 prints one
Prints first and third word i.e one and tthree
It provides output as first word after delimiter

CAT:

It prints the content of the file.

syntax : cat filename

GREP:

It is used for matching the pattern in each line.

syntax : grep pattern filename

e.g:

1. grep a$ filename: it matches anything end with a

2. grep ^pattern filename: it matches anything beginning with pattern

3. grep pattern filename: it matches pattern anywhere in the line

4. grep ^first,last$ filename: it matches pattern start with first , end with last and in between ,

Search for pattern `hi`

AWK:

It finds and Replaces text, database sort/validate/index

syntax : awk pattern filename

SED:

  • SED stands for stream editor
  • A stream is a data that travel from one process to another through pipe or one file to another as a redirect or one device to another
  • sed perform the text transformations on stream i.e substitution some text from other, remove line, append line after given lines, insert text before certain lines
  • sed is case sensitive by default and replace only first occurrence of search pattern in each line
  • To convert the sed to case insensitive we need to use flag : i/I
  • To cover sed to replace all occurrence of pattern use g as flag
  • sed command also helps to save the output into another file without changing existing file.

syntax: sed ‘s/search pattern/replacement pattern/flag’ filename > new-filename

  • To create a back up copy of file we can use -i command.

syntax : sed -i.bak 's/search pattern/replacement pattern’ filename . It creates a new backup file with name filename.bak

  • We can create a new backup file as sed 's/search pattern/replacement pattern/gw replacementFileName' outputFileName
  • We can use any pattern as a delimiter. example: Instead of / we can use # as sed 's#search pattern#replacement pattern#flag
  • We can use multiple pattern in single sed command as sed 'pattern1 ; pattern2 ; ...' filename
  • Multiple patters of sed command also we can pass through the file. Store multiple command in a file and use the sed command. syntax: sed -f script.sed inputFile

syntax for using flag: sed ‘s/search pattern/replacement pattern/flag’ filename

sed example
sed operation with case insensitive flag and we can change only 2nd occurrence of text using 2 as flag
^ : beginning of line and $: end of line

PIPE:

  • is represented by |
  • It is used to send the result of the previous command to sed command

LAST REMARKS

I hope you find this blog helpful. If you enjoyed it, feel free to hit the clap button below 👏 to help others find it! and follow me on Medium. Thanks for reading. ❤❤❤❤

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store