SED : SED (Stream Editor) - Interview Question

 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SED (Stream Editor) is a powerful command-line utility in Linux used for performing various text manipulation tasks such as search and replace, filtering, and transformation. Here are some commonly used SED commands in Linux:

cat -n filename  // displays output using line numbers

Print a particular line multiple time.

sed '2p' filename // This print 2nd lines along with other but 2nd line printed twice


sed -n '16p' data.txt   // This print the 16th line alone

$ sed -n '3{p;p;p}' data.txt

Sreejith        7898980090      sreejith.sbk@gmail.com  London
Sreejith        7898980090      sreejith.sbk@gmail.com  London
Sreejith        7898980090      sreejith.sbk@gmail.com  London

$ for ((i=1; i<=3; i++)); do sed -n '29p' data.txt; done

Ravana  9309080497      ravana@gmail.com        Newzealand
Ravana  9309080497      ravana@gmail.com        Newzealand
Ravana  9309080497      ravana@gmail.com        Newzealand


Display last line and range of lines

$ sed '$p' filename

$ sed -n '$p' filename   // $p means last line

Display only specific range of lines

$ sed -n '2,6p' filename

$ sed -n '10,20p' data.txt 

not to display a few lines


$ sed -n '2,40!p' data.txt

Sreejith        7898980090      sreejith.sbk@gmail.com  London
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandru

Skipping lines / do not display specific lines

$ sed -n '2!p' filename

Context Addressing:

Display Lines having specfic lines 

Display Lines having specfic word (Ignoting Case)

$ sed -n '/Divya Unnus/p' data.txt 

Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum
Divya Unnus     7989398938      divyaunoos@gmail.com    Trivandrum

$ sed -n '/[Aa]mith/p' filename

Search a line and store it in the file

sed -n '/A/p' filename

Writing the output to a file

$ sed -n '/[Aa]/w rest.1' filename

Subsutitue Data in a file



Print out specific word 

sed -n 's/.*\b\(sreejith\)\b.*/\1/p' data.txt

change multiple values in a file use -e option -e stands for extended operation

$ sed -e 's/Sreejith/Sreejith Balakrishnan/' -e 's/DonKing/Agasthya/' data.txt

Name    Phone   Email   City

firstman    9800909000  firstman@gmail.com
Sreejith Balakrishnan   7898980090      sreejith.sbk@gmail.com  London
Sreejith Balakrishnan   7898980090      sreejith.sbk@gmail.com  London
Sreejith Balakrishnan   7898980090      sreejith.sbk@gmail.com  London
Sreejith Balakrishnan   7898980090      sreejith.sbk@gmail.com  London
Sreejith Balakrishnan   7898980090      sreejith.sbk@gmail.com  Londo

We want to replace only of the line has the work Johnny in it 


$ sed  '/johny/s/10000/15000/' filename

Deleting lines which has Amit

$sed 's/[Aa]mit/d' filename

Inserting data into the file

sed '1i welcome to data file' filename

sed '40i welcome to data file' filename  //The text will be inserted in line number 40


Cut Command

 cut -c5-12 data.txt






Search and replace:

> Replace the first occurrence of 'old_text' with 'new_text' on each line of a file:

$ sed 's/old_text/new_text/' filename

 > Replace all occurrences of 'old_text' with 'new_text' on each line of a file:

sed 's/old_text/new_text/g' filename

 > Replace the nth occurrence of 'old_text' with 'new_text' on each line of a file:

sed 's/old_text/new_text/n' filename

 

Delete lines:

 >  Delete the lines that match a specific pattern in a file:

sed '/pattern/d' filename

>  Delete the first n lines of a file:

 sed '1, n d' filename

 explained

In the sed '1, n d' filename command, n is a number that specifies how many lines should be deleted, starting from the first line. So, for example, if n is 5, then the command will delete the first 5 lines of the file.

The d in the command is the sed command that specifies what action should be taken on the specified lines. In this case, d stands for "delete". So, when sed encounters the lines specified by the 1, n range, it will delete them from the file.

 > Delete the last n lines of a file:

 sed -n -e :a -e '1,n!{P;N;D;};N;ba' filename

Here's what each part of the command does:

  • sed is the command that invokes the sed tool.
  • -n option tells sed not to print the input lines by default.
  • -e option specifies that the following argument is an editing command.
  • :a is a label that we can use to jump to later in the command.
  • '1,n!{P;N;D;}; is a command that specifies how lines should be processed. This command consists of three parts:
    • 1,n! means lines not in the range of 1 to n, where n is a number specified by the user, should be processed as follows.
    • {P;N;D;} is a command group that specifies what should be done to these lines:
      • P prints the first line of the group.
      • N appends the next line of input to the pattern space, separating the two lines with a newline character.
      • D deletes the first line of the pattern space up to the first newline character, allowing the next line to become the first line in the pattern space.
  • N is a command that appends the next line of input to the pattern space, separating the two lines with a newline character.
  • ba is a command that jumps back to the label :a and repeats the processing for the remaining lines of the file.

In summary, this sed command reads in a file and processes it line by line. For lines in the range of 1 to n, it skips them and does not process them. For lines outside this range, it prints the first line of the group, appends the next line, deletes the first line, and repeats the process until it has printed all lines in the file. The final result is a printout of all lines in the file, except for the first n lines.


 Add or insert lines:

 > Add a line at the beginning of a file:

sed '1i\new_line' filename

 > Add a line at the end of a file:

 sed '$a\new_line' filename

 > Insert a line before a specific pattern in a file:

 sed '/pattern/i\new_line' filename

> Insert a line after a specific pattern in a file:

sed '/pattern/a\new_line' filename

 Print lines:

Print all lines of a file:

sed -n 'p' filename

> Print a specific line of a file:

 sed -n 'n p' filename

 > Print lines within a range:

 sed -n 'm,n p' filename

Transform text:

 > Convert all text to uppercase:

sed 's/.*/\U&/' filename

 > Convert all text to lowercase:

sed 's/.*/\L&/' filename

 > Reverse the order of characters in each line of a file:

 sed 's/\(.\)/\1\n/g' filename | tac | tr -d '\n'

 

 ----


  1. sed (short for "stream editor") is a powerful command-line tool for text processing and transformation. It is commonly used in Unix-like operating systems to edit text files or data streams in a batch or automated fashion. Here are some basic sed commands to get you started: s/old/new/g: This is the most commonly used sed command, which substitutes all occurrences of the string "old" with "new". The g at the end of the command means that it will perform the substitution globally (i.e., replace all occurrences). For example, sed 's/old/new/g' file.txt will replace all occurrences of "old" with "new" in the file file.txt. p: This command prints the current line of the input file to the standard output. For example, sed '2p' file.txt will print the second line of file.txt twice. d: This command deletes the current line of the input file. For example, sed '3d' file.txt will delete the third line of file.txt. /pattern/d: This command deletes all lines that match the specified pattern. For example, sed (short for "stream editor") is a powerful command-line tool for text processing and transformation. It is commonly used in Unix-like operating systems to edit text files or data streams in a batch or automated fashion. Here are some basic sed commands to get you started: s/old/new/g: This is the most commonly used sed command, which substitutes all occurrences of the string "old" with "new". The g at the end of the command means that it will perform the substitution globally (i.e., replace all occurrences). For example, sed 's/old/new/g' file.txt will replace all occurrences of "old" with "new" in the file file.txt. p: This command prints the current line of the input file to the standard output. For example, sed '2p' file.txt will print the second line of file.txt twice. d: This command deletes the current line of the input file. For example, sed '3d' file.txt will delete the third line of file.txt. /pattern/d: This command deletes all lines that match the specified pattern. For example, sed '/pattern/d' file.txt will delete all lines in file.txt that contain the word "pattern". /pattern1/s/pattern2/replace: This command substitutes "replace" for "pattern2" in the lines that contain "pattern1". For example, sed '/hello/s/world/earth/' file.txt will replace "world" with "earth" in all lines that contain "hello". /pattern1/,/pattern2/d: This command deletes all lines between the lines that match "pattern1" and "pattern2", including the lines that match "pattern1" and "pattern2". For example, sed '/start/,/end/d' file.txt will delete all lines in file.txt between the lines that contain "start" and "end", inclusive. These are just a few basic sed commands, and there are many more advanced features and options available. I recommend consulting the sed documentation or online resources for more information and examples.will delete all lines in file.txt that contain the word "pattern". /pattern1/s/pattern2/replace: This command substitutes "replace" for "pattern2" in the lines that contain "pattern1". For example, sed '/hello/s/world/earth/' file.txt will replace "world" with "earth" in all lines that contain "hello". /pattern1/,/pattern2/d: This command deletes all lines between the lines that match "pattern1" and "pattern2", including the lines that match "pattern1" and "pattern2". For example, sed '/start/,/end/d' file.txt will delete all lines in file.txt between the lines that contain "start" and "end", inclusive. These are just a few basic sed commands, and there are many more advanced features and options available. I recommend consulting the sed documentation or online resources for more information and examples.

Comments

Popular posts from this blog

AWK : Commands

CURL Commands

$$$$ Symbols in shell scripting