# Selecting text on multiple lines, then removing a beginning and end patterns

**URL:** <https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198>\
**Category:** Shell Programming and Scripting\
**Created:** [February 8, 2020, 3:43pm UTC](https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198 "2020-02-08T15:43:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danette](https://community.unix.com/letter_avatar/danette/32/6_22734026db12803ebb3e059622ac3534.png) [@Danette](https://community.unix.com/u/Danette)\
**Post date:** [February 8, 2020, 3:43pm UTC](https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198/1 "2020-02-08T15:43:21Z")

</div>

I have a file similar to the below. I am selecting only the paragraphs with `@inlineifset` .

I am using the following command

```nohighlight
sed '/@inlineifset/,/^ *$/!d;
  s/@inlineifset{mrg, @btpar{@//' $flnm >> $ofln

```

This produces

```nohighlight
@section Correlations between
seismograms,,,,}}

@inlineifset{mrg, @ptpar{Most common use of ambient noise is subsurface
tomography [extract traveltimes of Rayleigh Wavetrains].,,,,}}

@inlineifset{mrg, @ctpar{(1) cross correlation of waveforms;, (2)
computation of group and phase velocities at different wave frequencies
(periods),,,}}

@inlineifset{mrg, @ptpar{?,,,,}}

```

However I want to end up with the following. As seen the `....}}` is removed and seismograms but on previous line

so the texinfo command defining a section is on one line.

```nohighlight
@section Correlations between seismograms

@inlineifset{mrg, @ptpar{Most common use of ambient noise is subsurface
tomography [extract traveltimes of Rayleigh Wavetrains].,,,,}}

@inlineifset{mrg, @ctpar{(1) cross correlation of waveforms;, (2)
computation of group and phase velocities at different wave frequencies
(periods),,,}}

@inlineifset{mrg, @ptpar{?,,,,}}

```

The input file is the following

```nohighlight
cat file

@inlineifset{mrg, @btpar{@@section Correlations between
Seismograms,,,,}}

@inlineifset{mrg, @ptpar{Most common use of ambient noise is subsurface
tomography [extract traveltimes of Rayleigh Wavetrains].,,,,}}

@inlineifset{mrg, @ctpar{(1) cross correlation of waveforms;, (2)
computation of group and phase velocities at different wave frequencies
(periods),,,}}

The most common approach in the ambient noise technique is to extract
the traveltimes of background Rayleigh Wavetrains using the cross
correlation of waveforms, followed by the computation of group and phase
velocities at different wave frequencies (periods)
@ref{bibl--Ermert--et--Villasenor--et--Fichtner--2016,, Ermert@comma{}
Villase@~nor & Fichtner (2016)}. The geophysical basis is that when
diffuse wavefields are produced by diffuse sources or by multiple
scatterers, although the station instruments detect random signals, when
the same seismic wavetrain passes through two distinct stations, the
recorded seismograms are weakly correlated. This emerged from one of
the most well known conjectures in seismology @minus @ @dfn{The
Claerbout Cross Correlation Conjecture}. It was first stated by
American Geophysicist Jon F.@: Claerbout in 1968, after showing that the
reflectivity response of a one dimensional (1D) layered structure from a
surface source and a surface receiver is one side of the autocorrelation
of the seismogram recorded at the same surface receiver but transmitted
from a source at depth.

@inlineifset{mrg, @ptpar{?,,,,}}

It was specialist discipline of @dfn{Helioseismology}, developed in
recent decades that validated the long standing geophysical cross
correlation approach that generalised Claerbout's Conjecture to Three
Dimensions (3-D). Essentially, helioseismology studies the surface
oscillations of the sun in a timescale of minutes and hours as seen from
solar spectra (spectral lines). The fundamental physics for working
with the surface undulations of the sun focuses on the simulation
(modelling) of acoustic wave propagation stochastically excited and
damped by the convection of solar plasma (e.g., using P-Modes).

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_22734026db12803ebb3e059622ac3534.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 8, 2020, 5:26pm UTC](https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198/2 "2020-02-08T17:26:04Z")

</div>

Try

```nohighlight
awk '
/inlineifset/ {$1 = $1; 
                 if (sub (/@inlineifset{mrg, @btpar{@/, "")) sub (/,,,,}}/, "")
                 print
                }
 ' RS= ORS="\n\n" FS="\n" file
@section Correlations between Seismograms

@inlineifset{mrg, @ptpar{Most common use of ambient noise is subsurface tomography [extract traveltimes of Rayleigh Wavetrains].,,,,}}

@inlineifset{mrg, @ctpar{(1) cross correlation of waveforms;, (2) computation of group and phase velocities at different wave frequencies (periods),,,}}

@inlineifset{mrg, @ptpar{?,,,,}}

```

---

<div class="post-metadata">

**Author:** ![Danette](https://community.unix.com/letter_avatar/danette/32/6_22734026db12803ebb3e059622ac3534.png) [@Danette](https://community.unix.com/u/Danette)\
**Post date:** [February 8, 2020, 7:12pm UTC](https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198/3 "2020-02-08T19:12:47Z")

</div>

I want to avoid changing the other lines with `@ptpar` and `@ctpar` .

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [February 9, 2020, 1:46am UTC](https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198/4 "2020-02-09T01:46:26Z")

</div>

Try this adaptation of RudiC's suggestion:

```nohighlight
awk '
  /@inlineifset/ {
    if(/@btpar/) {
      $1 = $1
      gsub(/@inlineifset{mrg, @btpar{@|,,,,}}/, "")
    }
    print
  }
' RS= ORS="\n\n" FS="\n" file

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [February 9, 2020, 4:39am UTC](https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198/5 "2020-02-09T04:39:14Z")

</div>

You can boost your sed attempt

```nohighlight
sed '
  /@inlineifset/,/^ *$/!d
  /@inlineifset{mrg, @btpar{@/{
# Delete the previous search pattern
    s///
    :Loop
# Delete the end pattern; on success go to the end (next cycle)
    s/,,*}}//; t
# Emergency exit
    $b
# Append the next input line; go to :Loop
    N; bLoop
  }
' $flnm >> $ofln

```

Edit: this does not work!  
My mistake: the success status of the `s///` is not changed by the following unsuccessful `s/,,*}}//` so the `t` command always branches to the end (next cycle).  
Here is a fix:

```nohighlight
sed '
  /@inlineifset/,/^ *$/!d
  /@inlineifset{mrg, @btpar{@/{
# Delete the previous search pattern
    s///
    :Loop
    /,,*}}/{
# Delete the previous search pattern; go to the end (next cycle)
      s///; b
    }
# Emergency exit
    $b
# Append the next input line; go to :Loop
    N; bLoop
  }
' $flnm >> $ofln

```

---

<div class="post-metadata">

**Author:** ![Danette](https://community.unix.com/letter_avatar/danette/32/6_22734026db12803ebb3e059622ac3534.png) [@Danette](https://community.unix.com/u/Danette)\
**Post date:** [February 9, 2020, 6:41am UTC](https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198/6 "2020-02-09T06:41:33Z")

</div>

That should be it.  
I also used the following to take care of long lines

```nohighlight
fold -s temp.txt > temp.fld
fmt -w 72 temp.fld >> $ofln

```

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [August 10, 2020, 7:30pm UTC](https://community.unix.com/t/selecting-text-on-multiple-lines-then-removing-a-beginning-and-end-patterns/377198/7 "2020-08-10T19:30:26Z")

</div>

Automatically bumped on August 10, 2020, 7:30pm UTC


