Results 1 to 4 of 4

Thread: Using SED

  1. #1
    Join Date
    Jan 2012
    Posts
    2

    Using SED

    I have a shell script that is outputting from a database. What is coming from the DB is this:

    -----------
    50

    (1 rows affected)

    The issue that I have is there is an if statement in my script that looks at the first line and reads the number there. Unfortunately because of how this comes out it reads either the blank line or the hyphen as an invalid character and throws an error. I know you can use SED to remove unwanted text and spaces however I'm not sure exactly how to put it together in this instance. I do have an SED command in my script:

    Code:
    ${SED} 's/  */ /g' <extract_dt.txt > temp.txt
    From what I have been told this just replaces any time there is two spaces in a row with one space but I need it to do a bit more than that for my purposes. Ideally I'd want to trim everything that's not the number but if I can at least get the number on the top row that would be just fine. Thanks.

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,427
    It might be simpler to accomplish this with grep:

    Code:
    grep -E "^[ \t]*[0-9]+$" extract_dt.txt > temp.txt
    but you can use the same patter in sed I think.

  3. #3
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,001
    Just add a read statement to your script to consume the first line of the output (the ------)

    Code:
    read discard
    read number
    echo $number

  4. #4
    Join Date
    Feb 2006
    Posts
    172
    This will work for you:
    Code:
    cat test1.txt
    
    -----------
    50
    
    (1 rows affected)
    
    cat test1.txt | sed -e '/^$/d' -e '/^\-/d' -e '/^(1 rows affected)$/d' > /tmp/test2.txt
    
    cat test2.txt
    50

Tags for this Thread

Posting Permissions

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