1.

What is the importance of sed command and awk command?

Answer»
  • sed Command: sed command is an acronym for stream editor. It is used for editing a file without using an editor. The SED command performs a variety of operations on files, such as searching, finding and replacing, inserting and deleting. However, substitution or find and replace are the most commonly used features of SED.
    Syntax: sed options file 
    Example: Execution over Shell Interpreter/Editor
/u/user1/Shell_Scripts_2020> echo "Hi Gourav" | sed 's/Hi/Hello/'

In this case, “s” command in sed replaces the string Hi with “Hello”. 

Output: 

Hello Gourav
  • AWK Command: The command is a scripting language generally used to manipulate data and generate reports. There is no need to compile it and users are ALLOWED to have access to a variable, string functions, numeric functions, and logical operations.
    Syntax: awk options File Name 
    Example: Script/Code
/u/user1/Shell_Scripts_2017> cat > script10 Hello Aisha Hello Rohan Hello Gourav

An awk command/utility assigns variables in following way: 

$0 -> For whole LINE (e.g. Hello Aisha)  

$1 -> For the FIRST FIELD i.e. Hello  

$2 -> For the second field 

Execution over Shell Interpreter/Editor 

awk ‘{print $0}’ script10

The above script prints all the 3 lines completely. 

Output: 

Hello Aisha Hello Rohan Hello Gourav

Execution over Shell Interpreter/Editor 

awk ‘{print $1}’ script10

The above script prints only the first word i.e., Hello from each line. 

Output: 

Hello Hello Hello


Discussion

No Comment Found