Delete lines and the first pattern between 2 matched patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines and the first pattern between 2 matched patterns
# 1  
Old 07-31-2013
Delete lines and the first pattern between 2 matched patterns

Hi,

i need help to delete all the lines between 2 matched patterns and the first pattern must be deleted too. sample as follows:

inputfile.txt
Code:
>kump_1
...........................
...........................
>start_0124
dgfhghgfh
fgfdgfh
fdgfdh
>kump_2
..........................
..........................
..........................
....................
>start_0012
sdfdsagf
gfhghg
>kump_3
...........................
>start_3254
sdafdsfg.......
fsdf....adfdf
fdsaf...

i want to have the output like this:

Code:
>start_0124
dgfhghgfh
fgfdgfh
fdgfdh
>start_0012
sdfdsagf
gfhghg
>start_3254
sdafdsfg.......
fsdf....adfdf
fdsaf...

i tried using
Code:
sed '/>kump/,/>/{ />/p; d }'

but it still show the ">kump_1" for the first line of the file. Any help is much appreciated. Thanks
# 2  
Old 07-31-2013
Code:
$ awk '/^>/ {P = 0} /^>start/ {P = 1} P' file
>start_0124
dgfhghgfh
fgfdgfh
fdgfdh
>start_0012
sdfdsagf
gfhghg
>start_3254
sdafdsfg.......
fsdf....adfdf
fdsaf...

This User Gave Thanks to Scott For This Post:
# 3  
Old 07-31-2013
Hi Scott,

thanks so much for the code. It works great!!..but i did a very minor change to the code as i have another issue that i didn't show in the sample. actually, not all line use ">start_xxx". So, i change it to ">kump" and add "!" in front of P in your code as follows:

Code:
 $ awk '/^>/ {P = 0} /^>kump/ {P = 1} !P' file

Thanks again for your kind help Smilie

Last edited by redse171; 07-31-2013 at 11:44 AM.. Reason: typo
# 4  
Old 08-01-2013
With positive logic P stands for print
Code:
awk '/^>/ {P = 1} /^>kump/ {P = 0} P' file


Last edited by MadeInGermany; 08-01-2013 at 07:39 AM.. Reason: deleted the space too many
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 08-01-2013
Hello,

Just an another way to get the desired output.
Lets say we have file named check_test as follows.


Code:
 cat check_test
>kump_1
...........................
...........................
>start_0124
dgfhghgfh
fgfdgfh
fdgfdh
>kump_2
..........................
..........................
..........................
....................
>start_0012
sdfdsagf
gfhghg
>kump_3
...........................
>start_3254
sdafdsfg.......
fsdf....adfdf
fdsaf...


Command is as follows to get desired output.


Code:
 
$ sed 's/^\>kump_[0-9]//g; s/\.//g; /^ *$/d' check_test


Output is as follows.

Code:
>start_0124
dgfhghgfh
fgfdgfh
fdgfdh
>start_0012
sdfdsagf
gfhghg
>start_3254
sdafdsfg
fsdfadfdf
fdsaf
$





Thanks,
R. Singh

Last edited by RavinderSingh13; 08-01-2013 at 04:26 AM.. Reason: removing the underline in code to make it clear.
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 08-01-2013
@R.Singh
Must be > not \>. The latter has a special meaning "right word boundary" in many sed versions.
Here is another sed solution (IMHO ugly compared to the awk solution):
Code:
sed -n -e '${x;p;x;p;}' -e '/^>kump/{g;1!p;}' -e 'H;/^>/h' file


Last edited by MadeInGermany; 08-01-2013 at 08:24 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 08-01-2013
Quote:
Originally Posted by RavinderSingh13
Code:
$ sed 's/^\>kump_[0-9]//g; s/\.//g; /^ *$/d' check_test

This is wrong since it modifies line outside of the designated range by indiscriminately deleting dots.

If, as I suspect, the dots in the data are placeholders for irrelevant data, then this solution which depends on literal dots is wrong in a second way.

Regards,
Alister

---------- Post updated at 01:14 PM ---------- Previous update was at 12:27 PM ----------

Quote:
Originally Posted by MadeInGermany
Must be > not \>. The latter has a special meaning "right word boundary" in many sed versions.
I'm curious. Besides GNU (and perhaps Busybox, which emulates GNU whenever their minimalist mission allows), which sed implementations support \>?


Quote:
Originally Posted by MadeInGermany
Here is another sed solution (IMHO ugly compared to the awk solution):
Code:
sed -n -e '${x;p;x;p;}' -e '/^>kump/{g;1!p;}' -e 'H;/^>/h' file

That sed script is not equivalent to the awk solution. If the data ends when in mid-range, it will print (accumulated) lines which the awk alternative would not have.

Not a one-liner, but it's the most straightforward approach which behaves analogously:
Code:
#n

/^>kump/ {
        :top
        n
        /^>/! b top
}
p

Regards,
Alister
These 3 Users Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

awk to delete content before and after a matched pattern

Hello, I have been trying to write a script where I could get awk to delete data before and after a matched pattern. For eg Raw data Start NAME = John Age = 35 Occupation = Programmer City = New York Certification Completed = No Salary = 80000 End Start NAME = Mary Age = 25... (2 Replies)
Discussion started by: sidnow
2 Replies

3. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

4. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

5. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

6. Shell Programming and Scripting

removing lines around a matched pattern

I have an ugly conf file that has the string I'm interested in searching for in the middle of a block of code that's relevant, and I'm trying to find a way to remove that entire block based on the matched line. I've googled for this problem, and most people helping are only interested in... (9 Replies)
Discussion started by: tamale
9 Replies

7. Shell Programming and Scripting

delete two patterns and remove one pattern

Friends, I have .txt file with following format. START ABC|Prashant1|Patel1 ABC|Prashant2|Patel2 ABC|Prashant1|Patel1 ABC|Prashant2|Patel2 END I would like to do: 1) Delete line with START 2) Delete line with END 3) Remove ABC| 4) Delete duplicate records The following command... (7 Replies)
Discussion started by: ppat7046
7 Replies

8. Shell Programming and Scripting

Delete lines between two patterns without deleting the second pattern

I want to delete lines like this sed '/FROM_HERE/,/TO_HERE/d' but I would like to *not* delete the second match, i.e. the TO_HERE line. How can I achieve this? Thank you! (1 Reply)
Discussion started by: Ilja
1 Replies

9. Shell Programming and Scripting

SED: delete and print the only exact matched pattern

I am really need help with the regular expression in SED. From input file, I need to extract lines that have the port number (sport or dport) as defined. The input file is something like this time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23 bytes=2472 proto=6 sport=80 dport=1447... (6 Replies)
Discussion started by: new_buddy
6 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question