Solved

using sed - how do I delete lines above and below a match?

Posted on 2007-04-05
9
4,980 Views
Last Modified: 2012-08-13
using sed - I need to delete one line above and one line below any line with "ldots" on it.  

In the example below all of the lines with "\backslash" and all the lines with "\end_layout" have to be deleted.   There are other  lines with  "\end_layout" that need to stay so that's why I can't do something like:
sed -e 's/\\end_layout//'

Example:
$ grep -A1 -B1 ldots sampleDoc.lyx
\backslash
ldotsye
\end_layout
--
\backslash
ldotsforeordained
\end_layout
--
\backslash
ldotsAnd
\end_layout
--
\backslash
ldotsfor
\end_layout
--
\backslash
ldotsthat
\end_layout
--
\backslash
ldotsI
\end_layout
--
\backslash
ldotsboth
\end_layout
--
\backslash
ldotsleaving
\end_layout
--
\backslash
ldotsthat
\end_layout
--
\backslash
ldotsas
\end_layout

An explanation of the arguments would be appreciated.  I've been trying to make sense of these sed one-liners to apply to my problem.

# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (similar to "grep -A1 -B1")
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

# print the line immediately before a regexp, but not the line
 # containing the regexp
 sed -n '/regexp/{g;1!p;};h'

 # print the line immediately after a regexp, but not the line
 # containing the regexp
 sed -n '/regexp/{n;p;}'

Thanks,
Frank
0
Comment
Question by:ibanja
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18860010
do you want
sed -n '/ldot/p'
0
 

Author Comment

by:ibanja
ID: 18860341
No, because I need the rest of the file.  I only want to delete the lines above and the lines below a match.

Example: I want to take this:
line one
line two

\backslash
ldotsleaving
\end_layout

last line

and turn it into this:
line one
line two

ldotsleaving

last line

Thanks
0
 
LVL 6

Expert Comment

by:DocGyver
ID: 18867245
try

sed -e 'g/ldots/-1,+1d'
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:ahoffmann
ID: 18876427
I'd use perl or awk instead:
awk '(s==1){s=0;next}{x++}/ldots/{x--;s=1}{a[x]=$0}END{for(x in a){print a[x]}}' yourfile
0
 
LVL 6

Accepted Solution

by:
DocGyver earned 500 total points
ID: 18885705
Sorry I missed the fact that you want the line inbetween to stay.  Sounds like you are cleaning out formatting tags from around matching lines.  Mine would take the "ldots" line too.  Here's the correction and, as you asked, an explanation also.

This is a bit of an extreme example for sed (I would use either ed or ex myself) since they are not stream oriented and can back track in the file.  That's why my earlier example didn't work.  I was thinking in 'ed".  All the same here is your result with an explanation:

sed -n -e '1h' -e '1!H' -e '/ldots/{p;n;n;h;d}' -e 1b -e 'x;P' < yourfile

-n                          "do not print unless I tell you to"
-e '1h'                    "if this is the first line initialize the hold buffer with the line
-e '1!H'                   "if this is not the first line then ADD the line to the hold buffer
-e '/ldots/{...}'         "if the current line has /ldots/ in it then p-print, get the next line, another next line
                               reinitialize the hold buffer, and delete the pattern buffer"  The delete is so we cycle
                               to the top
-e 1b                       "if we are on the first line then jump to the end"  need this to handle the special case
                                 of the first line containing your search string (ldots)
-e 'x;P'                   "otherwise exchange the current line with the hold buffer and 1 line of the hold buffer

Only reason this works is you only want to get a single line above and below.  If you wanted to do more lines you would have to resort to using 'ed' or some other scripting language.

Doc..
0
 

Author Comment

by:ibanja
ID: 18891369
ahoffmann,

thanks for the input but I could not get this to work.  It was scrambling the document somehow.  I created a short sedTest doc by running "grep -A1 -B1 ldots sampleDoc.lyx > sedTest" which produces the exact same output as in my original question - dumped to the file named sedTest. I then ran line that you gave and got the following:

$ awk '(s==1){s=0;next}{x++}/ldots/{x--;s=1}{a[x]=$0}END{for(x in a){print a[x]}}' sedTest
ldotsthat
--
--
ldotsAnd
ldotsas
--
ldotsfor
--
ldotsthat
--
ldotsI
--
ldotsboth
--
ldotsye
ldotsleaving
--
--
ldotsforeordained
0
 

Author Comment

by:ibanja
ID: 18891427
DocGyver,

That is perfect.  It works and thanks so much for the explanation.  That clearifies a few questions I have had about how to use some of the options in sed, especially the h and the n.  I could never get them to work and didn't have any good examples to compare to.

Much appreciated,
Frank
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18892639
you got a solution, perfect.
According my suggestion, the result you posted is exactly what it should do as I understand it from your description.
0
 

Author Comment

by:ibanja
ID: 18892758
ahoffmann,

"According my suggestion, the result you posted is exactly what it should do as I understand it from your description."

Sorry, I might have to be more clear.  Thanks though.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question