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.
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] 삭제
몇 마디 만하겠습니다