Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, I am no SED Guru and therefore I need short help for writing a SED script which searches for a RegExp in a file and copyies from that RegExp ...
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Posts
    4

    SED script for searching text in a file and copying to new file

    Hello,

    I am no SED Guru and therefore I need short help for writing a SED script which searches for a RegExp in a file and copyies from that RegExp to another RegExp content to a new file like:

    Code:
    $sed -n -e /RegExp1/,/RegExp2/p mytextfile
    So now I get the content from RegExp1 to RegExp2 to Stdout which is OK.

    But I don't want to have the lines with RegExp1 and RegExp2 within the output but to start 1 line after RegExp1 to copy and stop 1 line before RegExp2.
    Can anyone tell me what RegExp to use so I get lines RegExp1+1(line) to RegExp2-1(line) to the output?

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    sed '1d;$d' <mytextfile
    the sun is new every day (heraclitus)

  3. #3
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97
    Code:
    sed -n '/regexp1/,/regexp2/w anotherfile' filename
    Refer here for more examples: Unix Sed Tutorial: How To Write to a File Using Sed

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by gessi2000 View Post
    ...Can anyone tell me what RegExp to use so I get lines RegExp1+1(line) to RegExp2-1(line) to the output?
    I would say that it is not the regular expression that can do this, but rather how the RE is combined and used in a command. Here are a few ways:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate range extraction, ed, sed, cgrep, perl.
    
    # Infrastructure details, environment, commands for forum posts. 
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    c=$( ps | grep $$ | awk '{print $NF}' )
    version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
    [ "$c" = "$s" ] && p="$s" || p="$c"
    version >/dev/null 2>&1 && version "=o" $p ed sed cgrep perl
    set -o nounset
    echo
    
    FILE=${1-data1}
    
    echo " Data file $FILE:"
    cat $FILE
    
    line1="qux"
    line2="grault"
    
    rm -f t1
    ed $FILE 2>/dev/null <<EOF 
    /$line1/+1,/$line2/-1w t1
    q
    EOF
    
    echo
    echo " Extracted data from ${line1}+1 to ${line2}-1 with ed:"
    cat t1
    
    rm -f t1
    echo
    echo " Results with sed:"
    # See http://www.faqs.org/faqs/editor-faq/sed/ section 4.15
    # ( "b" means "branch" )
    sed -n "/${line1}/,/${line2}/{
    /${line1}/b
    /${line2}/b
    p
    }" $FILE >t1
    cat t1
    
    rm -f t1
    echo
    echo " Results using 2 sed commands:"
    # First command could also easily be the corresponding awk command.
    sed -n "/${line1}/,/${line2}/p" $FILE |
    sed '1d;$d'
    
    echo
    echo " Windowing copy with cgrep (expecting trailing line):"
    cgrep -D -I2 -w "${line1}" "${line2}" $FILE
    
    echo
    echo " Results with perl, print within a range:"
    perl ./p1 "${line1}" "${line2}" $FILE
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0 
    GNU bash 3.2.39
    GNU Ed 0.7
    GNU sed version 4.1.5
    cgrep - (local: ~/executable/cgrep May 29 2009 )
    perl 5.10.0
    
     Data file data1:
    foo
    bar
    baz
    qux
    quux
    corge
    grault
    garble
    warg
    fred
    plugh
    xyzzy
    thud
    
     Extracted data from qux+1 to grault-1 with ed:
    quux
    corge
    
     Results with sed:
    quux
    corge
    
     Results using 2 sed commands:
    quux
    corge
    
     Windowing copy with cgrep (expecting trailing line):
    quux
    corge
    grault
    
     Results with perl, print within a range:
    quux
    corge
    What we might learn from this is:
    1) it is best to supply a sample of the data, along with expected output. That avoids each responder needing to create a data file and provides a common reference.

    2) tpl's solution is clever, I had not thought about running 2 seds (assuming that is what is being implied).

    3) I could not coerce my often-favorite, cgrep, into doing the right thing with only one call.

    4) the perl code is not shown, but could be provided if needed.

    5) see the URL noted in the script for the sed FAQ that provides many solutions to common problems.

    6) many people find awk easier to use and easier to read compared to sed.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...