Let’s take an plain text file, input.txt
, that looks like this
PATTERN-1 First line of unimportant text Second line of unimportant text PATTERN-2 Some more texts (may/ mayn't be important!)
We want to delete some of the lines from the file using the command line stream editor, sed.
1. Use the following command to delete the lines lying between PATTERN-1
and PATTERN-2
, excluding the lines containing these patterns:
sed '/PATTERN-1/,/PATTERN-2/{//!d}' input.txt
If you want to modify the file itself, instead of just the file stream, include the “-i” flag after sed.
2. Use the following command to delete the lines lying between PATTERN-1
and PATTERN-2
, including the lines containing these patterns:
sed '/PATTERN-1/,/PATTERN-2/d' input.txt
3. To delete all the lines after PATTERN-2, use this
sed '/PATTERN-1/,$d' input.txt
4. To delete lines, say 2 through 4 (if you know the correct line numbers, of course!), use this
sed '2,4d' input.txt
Here is a good sed reference.
skimeer said:
Good Article
Chris said:
Thanks a lot, this was exactly what I was looking for.
Kenneth Whittaker said:
I notice one problem. If I have a pattern such as the following:
random stuff 1
PATTERN1
random stuff 2
random stuff 3
PATTERN2
random stuff 4
random stuff 5
PATTERN1
random stuff 6
random stuff 7
PATTERN2
random stuff 8
I would want to only remove the items between PATTERN1 and PATTERN2, but not between PATTERN2 and PATTERN1, and not including the next PATTERN1. I.e., I would want the following output:
random stuff 1
random stuff 4
random stuff 5
random stuff 8
With the commands provided above, I got this:
random stuff 1
random stuff 8
Any ideas?
kousik said:
Are you sure about your findings?
Using the input you provided and the command
$ sed ‘/PATTERN1/,/PATTERN2/d’
I got the expected outcome
random stuff 1
random stuff 4
random stuff 5
random stuff 8
Could you double check please?
Smital said:
Hi I am looking for a pattern which will delete only new line between two strings and string may different each time :
Input :
Some text
Some text 1
Some text 2
Some text
Exp Output:
Some text
Some text1 Some text2
Some text
Please help me to resolve this
Smital said:
I dont know y above stuff is wrongly posted. My pattern is not over there!!!!
Some text1 and Some text 2 is heading and trailing by pattern
Muhammad Nouman said:
Really worth reading but a little modification on point 3
Syntax must point to : sed ‘/PATTERN-2/,$d’ input.txt instead sed ‘/PATTERN-1/,$d’ input.txt
Regards
Muhammad Nouman
Pradeep V said:
Hi all,
This is regarding deleting the lines between the pattern excluding the lines with pattern.
If the seconds pattern appears twice or thrice.. i want the lines to be deleted till the last occurrence of the second pattern..
Please help me in writing the regex for this..
Thanks a lot.
Guru said:
Thanks for the concise explanation. I had seen multiple forums including stackoverflow and found no clear answers for deleting lines with sed.
ketrad said:
I have a file containing a target passage which occurs in four separate places within the file. The target passage has content resembling the following (hundreds of rows are omitted here for brevity):
building file list …
0 files…
100 files…
200 files…
181900 files…
182000 files…
182025 files to consider
I’m not sure why, but using GNU sed 4.2.2, the following script leaves the file contents completely unchanged:
sed ‘/^building file list/,/files to consider$/{//!d}’ INFILE > OUTFILE
Any ideas? After reading the GNU sed manual I am getting the impression I will just have to live with the pattern rows also being deleted.
Pingback: bash 正規表示式 | 資訊雜記
fbicknel said:
Also…
If you want to pass lines between two patterns, but not the lines matching the patterns:
sed -n -e ‘/PATTERN1/,/PATTERN2/{//!p}’ INFILE
and similarly, if you _do_ want the matching patterns:
sed -n -e ‘/PATTERN1/,/PATTERN2/p’ INFILE
Anonymous said:
What if I want to pass the lines between the two patterns AND the first pattern (but not the last)?
teo said:
Great. Except if the patterns are not present, the whole file is wiped out!