Wednesday, April 27, 2016

Linux sed command example

Linux sed command example

Now we are going to try some examples. All examples are also considered as tutorials. When needed we have explained the  command of given examples.

Sed basic syntax

  1. First command used very simply. To search we just need to give it an string.
  2. -e options can be used for multiple pattern
  3. -n Does not output in the terminal. Wait for print command.

First Example of sed

This command finding pcspeech word and replacing with pc speech.
  1. sed (the built-in command).
  2. The pattern enclosed within single quote.
  3. s for substitute the line.
  4. Delimited the substitution with slash.
  5. The target word or characters or even sentence.
  6. Delimited the searching word again.
  7. The word to be replaced and delimited.
  8. The target file.

Replacing Globally

Sed find the pattern for once in every line. But it does not replace the every found string(see previous example). In this case we need to give it address range. For example, Globally to replace all found strings.

Delete the lines using sed command

If the pattern found then delete the lines(goes to next line automatically. We can do this with d ).

Multiple Commands in sed

We can run multiple commands at a time in two way. 1) Terminating the command with semicolon 2)With option -e.

Only look at specific lines

First command only search pattern at line 3.
Second Command the search start from line 3 and end at line 10 then replace the string.

Removing blank lines with sed

If we have unnecessary blank lines then we can remove these lines easily with sed. Here is an example

Don’t print if found

This is called negating(!) command in sed. Normally sed print all lines but when we negate the sed command this will ignore the line.
If the pcspeech found in any line of the text file reg.txt then the line would not printed as we have negated it with !p.

Sed command Branching



sed Recalling

This command matching anything before before the dot. This will match aaas,aod,aood etc. Also we are recalling the expression with &.

Saving the modified data to a file

This command replacing the word globally(g) and w used to tell that we want to save it toreg1.txt file.

Using different delimiter

We mostly seen the delimiter is forward slash. Sometime forward slash make the pattern hard to read the command if we escaped any other special characters using backslash. The easiest solutions for this :
So  if we want to do something with /etc/passwd, for example changing the default shell then it would be like this
[padding right=”0%” left=”5%”]

How it is working

  1. s for substitution.
  2. ! used for alternative delimiter whereas mostly used forward slash as delimiter. We used ! to make it more readable since backslash with forward slash will make it harder to read.
  3. The default shell we want to replace is /bin/bash
  4. Delimited with ! .
  5. The replaced string(shell) is /seen/ash
  6. Last delimitation with ! .
  7. The target file /etc/passwd
[/padding]
If we want to do same thing in hard way , then it will be look like this:
This command performing same task but it looks more horrible!

More delete with Linux sed command

Delete the first line

Delete  line 1-3 of lines

Delete the line if found the search pattern

Append/Insert/Read with sed command

Append a line at the end

Usually append command append a line after the data stream if we don’t specify an address. For example

We can also append a line after specific line number

Insert command insert the line before all line. For example

Better idea is to specify an address

Read from file

See the Last line

What if you want to print only last line?

Cleaning up html tags from a file

No comments:

Post a Comment