I read through the forum for clues how to solve my problem, but none of the related threads are useable for me, with limited programming knowledge, to apply to my specific problem.
My problem is this: I need to get rid of garbage lines that are clustered throughout my file, but are in between clusters of useable lines. I searched the sed manual and other informative sources about deleting ranges that match patterns, but they only mention to delete UNTIL match pattern, not TILL.
Now I would like to specify a range for which sed deletes lines starting from the first line that matches the pattern line till the line that matches the other pattern. Furthermore, sed needs to recognize the patterns that exists at the end of the lines.
For example:
line 1
blah blah 1
blah blah 2
blah blah 3
blah blah 4
line 2
line 3
Result needs to be:
line 1
blah blah 1
line 2
line 3
Please note the multiple lines between line and and line 2. While blah blah 1 needs to stay, the other 3 need to be deleted.
Thanks!
Delete a range of lines Press the Esc key to go to normal mode. Type :3,5d and hit Enter to delete the lines.
To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.
To Remove the lines from the source file itself, use the -i option with sed command. If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.
Deleting Multiple Lines in Vi and VimInstead of simply pressing dd, you can specify the number of lines you want to delete. For example, typing 3dd will delete the next three lines from the file. You can also use wildcard characters in the aforementioned command.
Try this
sed -n '/line 1/{;p;n;p;};/line 2/,$p' sedTest1.txt
#output
line 1
blah blah 1
line 2
line 3
Sed deconstructed :
sed -n '/line 1/{;p;n;p;};/line 2/,$p' sedTest1.txt
| | | | |||-> print the range
| | | | ||-> til end of file (the '$' char)
| | | | |-> range operator (i.e. start,end)
| | | |-> beginning of range to watch for and print
| | |-> now print line, get 'n'ext, print that line
| |-> match the line with text 'line 1'
|-> don't print every line, only ones flagged with 'p'
Read this from the bottom up.
Also, as your data is a sample, AND you refer to it as garbage lines, it may not be this simple. You'll need to look through sed tutorials to get up to speed.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With