1

I have a file abc.txt and I want to remove specific portion in a file from a bash script.

#######################Media######################
[Media]
       comment = Media Files
       path = /share22
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################
#######################Add#######################
[Add]
       comment = Media Files
       path = /share33
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################
#######################Added#####################
[Added]
       comment = Media Files
       path = /share44
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################

and remove below portion from file

#######################Add#######################
[Add]
       comment = Media Files
       path = /share33
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################

how can I remove a first portion, second portion or last portion?

Kindly help me!

1

2 Answers 2

2

The given input is fit for an easy solution:

awk '/##Add##/,/##end##/ {next};1'  abc.txt

or

sed '/##Add##/,/##end##/d' abc.txt
Sign up to request clarification or add additional context in comments.

Comments

1

Awk suits best for this kind of text file handling, you may use below one

awk '/^[#]+(Add)[#]+/{f=1}f && /^[#]+(end)[#]+/{f=0; next}!f' file

# OR combined one

awk '/^[#]+(Add|end)[#]+/{if(f){f=0; next}if(/^[#]+(Add)[#]+/)f=1}!f' file 

Test Results:

Input

$ cat file
#######################Media######################
[Media]
       comment = Media Files
       path = /share22
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################
#######################Add#######################
[Add]
       comment = Media Files
       path = /share33
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################
#######################Added#####################
[Added]
       comment = Media Files
       path = /share44
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################

Output - 1 :

$ awk '/^[#]+(Add)[#]+/{f=1}f && /^[#]+(end)[#]+/{f=0; next}!f' file
#######################Media######################
[Media]
       comment = Media Files
       path = /share22
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################
#######################Added#####################
[Added]
       comment = Media Files
       path = /share44
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################

Output - 2 :

$ awk '/^[#]+(Add|end)[#]+/{if(f){f=0; next}if(/^[#]+(Add)[#]+/)f=1}!f' file
#######################Media######################
[Media]
       comment = Media Files
       path = /share22
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################
#######################Added#####################
[Added]
       comment = Media Files
       path = /share44
       browseable = yes
       read only = yes
       guest only = no
#######################end#######################

5 Comments

Thanks a lot my issue is resolved. Can you explain the command because I'm new and it'll very helpful for me.
@ZubairMughal you are welcome, you may read this stackoverflow.com/help/someone-answers
Can you please explain?
@ZubairMughal: awk '/^[#]+(Add)[#]+/{f=1}f && /^[#]+(end)[#]+/{f=0; next}!f' variable f will be 1 when line starts with one or more # (Add) and one or more # , when variable f is set and line starts with one or more # (end) and one more # variable f becomes 0; and next means go to next line, any expression after next keyword will be skipped, }!f remember NOT gate, as long as variable f is zero awk prints line/row/record
@ZubairMughal sed '/##Add##/,/##end##/d' file walter has already given you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.