If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Using SED

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
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.
Reply With Quote
  #2 (permalink)  
Old
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 5,399
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.
Reply With Quote
  #3 (permalink)  
Old
Programming since 1BC
 
Join Date: Sep 2009
Location: Ontario
Posts: 994
Just add a read statement to your script to consume the first line of the output (the ------)

Code:
read discard
read number
echo $number
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
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
Reply With Quote
Reply

Tags
sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On