Find the answer to your Linux question:
Results 1 to 4 of 4
I'm currently using bash and would like to search a file for Start and clip all of the data from start to end and pass it to another file or ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    3

    searching a file and passing information to another file

    I'm currently using bash and would like to search a file for Start and clip all of the data from start to end and pass it to another file or display it on the screen. Example:
    12
    12
    12
    12

    Start
    1
    1
    1
    1
    1
    End


    123
    123
    123
    123


    Thanks

  2. #2
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    You probably want to use the 'grep' tool, check its man page. You do know that posting homework questions is against the forum rules, don't you?
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Just Joined!
    Join Date
    Jan 2010
    Posts
    3
    I'm not a student. I've been asked to manage a linux box at work

  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, surfin.

    Welcome to the forum.

    For simply structured blocks like this amended version of your data file, either sed or awk could be used:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate extraction of block of data.
    
    echo
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed awk
    set -o nounset
    echo
    
    FILE=${1-data1}
    
    echo " Data file $FILE:"
    cat $FILE
    
    echo
    echo " Results, sed:"
    sed -n '/^Start/,/^End/p' $FILE
    
    echo
    echo " Results, awk:"
    awk '/^Start/,/^End/ { print }' $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 sed version 4.1.5
    GNU Awk 3.1.5
    
     Data file data1:
    12
    12
    
    Start
    1
    End
    
    
    123
    123
    Start
    2
    End
    456
    Start
    3
    
     Results, sed:
    Start
    1
    End
    Start
    2
    End
    Start
    3
    
     Results, awk:
    Start
    1
    End
    Start
    2
    End
    Start
    3
    If your file has an unterminated block, then everything from the final "Start" until the end of the file will be extracted. There are ways that you could guard against that, but it would take more than the simple solutions provided here. Of these two tools, I think awk is the better one to learn for most people. I find that sed is quite capable, but it's like texting using a simple keypad, rather than a keyboard.

    When you are asking questions that involve data transformations, it is best if you also provide the output you want. If you have a problem with an existing solution, provide the incorrect output that you got.

    Most responders like to see that that you have tried to solve the problem yourself.

    Note the use of mark-up tags to make data and code easier to read -- to get that, select the text, click the # above the editing window, and tags will be inserted for you.

    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
  •  
...