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 -a command name
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
It prints the content of the file.
syntax : cat filename
It is used for matching the pattern in each line.
syntax : grep pattern filename
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 ,
It finds and Replaces text, database sort/validate/index
syntax : awk pattern filename
- 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 :
- To cover sed to replace all occurrence of pattern use
- sed command also helps to save the output into another file without changing existing file.
sed ‘s/search pattern/replacement pattern/flag’ filename > new-filename
- To create a back up copy of file we can use
sed -i.bak 's/search pattern/replacement pattern’ filename . It creates a new backup file with name
- 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
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
- is represented by
- It is used to send the result of the previous command to sed command
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. ❤❤❤❤