Add a line below each line matching a pattern only if not already present

Mongrel

Can sed add a new line below a specific content, if the input content exist then leave it ?

Current content of the file ssss

Hostname example.com
Os version rhel5.6
apache 4.2

Hostname example2.com
Os version rhel5.6

Desired content of the file ssss

Hostname example.com
Os version rhel5.6
apache 4.2

Hostname example2.com
Os version rhel5.6
apache 4.2

I am able to add the content with the below command

sed -i '/Os version rhel5.6/a apache 4.2' ssss

My Question

I want to add a line below a specified content if the content exist on the file then leave it. If the content doesn't exist then add it.

Rahul

This perl expression will do the trick,

perl -i -ne 'next if /apache 4.2/;s+Os version rhel5.6+Os version rhel5.6\napache 4.2+; print' ssss

Explanation

  • next if /apache 4.2/ skips any lines matching apache 4.2.
  • s+Os version rhel5.6+Os version rhel5.6\napache 4.2+; print search Os version rhel5.6 and replaces line with same with appending apache 4.2 at newline.

Test with your input file

$ cat ssss
Hostname example.com
Os version rhel5.6
apache 4.2

Hostname example2.com
Os version rhel5.6

$ perl -ne 'next if /apache 4.2/;s+Os version rhel5.6+Os version rhel5.6\napache 4.2+; print' ssss
Hostname example.com
Os version rhel5.6
apache 4.2

Hostname example2.com
Os version rhel5.6
apache 4.2

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

add / at the end of each line

분류에서Dev

regular expressions / pattern matching on ubuntu command line

분류에서Dev

Add (prepend) line to file, if parameter is not already existing

분류에서Dev

Delete n lines following a pattern (and the line matching the pattern)

분류에서Dev

Notepad++ Regex find line pattern but only replace one line

분류에서Dev

How to delete a pattern matching and the rest of the line in a file using Perl

분류에서Dev

Add and number blank line above each line in a file

분류에서Dev

Substitution only on last matching line (perl one-liner)

분류에서Dev

Delete line if content is present on the next line too

분류에서Dev

xslt - for each - line-by-line

분류에서Dev

Grok pattern with this log line

분류에서Dev

file.each_line can only be called once

분류에서Dev

C - Using strtok gives me only the first word of each line?

분류에서Dev

How to use Perl one-liner to add line based on first line pattern match?

분류에서Dev

vim - split line by comma and add prefix to each token

분류에서Dev

remove characters from a list and add a line break from each element

분류에서Dev

Add text to beginning and end of each line using batch/cmd

분류에서Dev

PHP Search Not Matching Line in File

분류에서Dev

whose instance is created in below line of code

분류에서Dev

Android: How to remove black line below the GridView

분류에서Dev

Fill missing fields with values from the line below

분류에서Dev

linux - add a line just before ending line

분류에서Dev

add first line to second line but separated with a tab

분류에서Dev

Bash - pair each line of file

분류에서Dev

Matching fixed number of zeroes before new line

분류에서Dev

Delete a line does not start with # and match a pattern

분류에서Dev

Search for pattern and append line to another file

분류에서Dev

Print up to (and including) the nth occurence of a pattern in a line

분류에서Dev

find not recursive when given a pattern on the command line

Related 관련 기사

  1. 1

    add / at the end of each line

  2. 2

    regular expressions / pattern matching on ubuntu command line

  3. 3

    Add (prepend) line to file, if parameter is not already existing

  4. 4

    Delete n lines following a pattern (and the line matching the pattern)

  5. 5

    Notepad++ Regex find line pattern but only replace one line

  6. 6

    How to delete a pattern matching and the rest of the line in a file using Perl

  7. 7

    Add and number blank line above each line in a file

  8. 8

    Substitution only on last matching line (perl one-liner)

  9. 9

    Delete line if content is present on the next line too

  10. 10

    xslt - for each - line-by-line

  11. 11

    Grok pattern with this log line

  12. 12

    file.each_line can only be called once

  13. 13

    C - Using strtok gives me only the first word of each line?

  14. 14

    How to use Perl one-liner to add line based on first line pattern match?

  15. 15

    vim - split line by comma and add prefix to each token

  16. 16

    remove characters from a list and add a line break from each element

  17. 17

    Add text to beginning and end of each line using batch/cmd

  18. 18

    PHP Search Not Matching Line in File

  19. 19

    whose instance is created in below line of code

  20. 20

    Android: How to remove black line below the GridView

  21. 21

    Fill missing fields with values from the line below

  22. 22

    linux - add a line just before ending line

  23. 23

    add first line to second line but separated with a tab

  24. 24

    Bash - pair each line of file

  25. 25

    Matching fixed number of zeroes before new line

  26. 26

    Delete a line does not start with # and match a pattern

  27. 27

    Search for pattern and append line to another file

  28. 28

    Print up to (and including) the nth occurence of a pattern in a line

  29. 29

    find not recursive when given a pattern on the command line

뜨겁다태그

보관