Grab Portion of Output Text (sed, grep, awk?)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grab Portion of Output Text (sed, grep, awk?)
# 1  
Old 04-27-2010
Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows):

Code:
>> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c
Clouds AND Sun 57/33 - Mostly sunny and cool

I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I want to be able to grab dynamically as everything changes depending on the weather.

My idea was to do something along the lines of

Code:
>> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c | grep [0-9][0-9][/][0-9][0-9]

but this obviously returns the whole line.

Ideally I'd like to display the following (though I'm not sure it can be done without a script):
Quote:
high: 57F
low: 33F
Any help would be great. Thanks!

---------- Post updated at 03:04 PM ---------- Previous update was at 02:45 PM ----------

I ended up just editing the script itself (doh!). I still would be interested to know if there's a way to grab just portions of text though...

Thanks!
# 2  
Old 04-27-2010
Hey,

Please try this..

Code:
/Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c |awk '{print $4}'|awk -F"/" '{print "High="$1 ; print "Low="$2}'

Thanks
Kantha

Last edited by Scott; 04-27-2010 at 05:09 PM.. Reason: Added code tags
# 3  
Old 04-27-2010
Code:
/Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c | cut -d" " -f4 | awk -F"/" '{printf("high:%sF\nlow:%sF\n",$1,$2)}'

# 4  
Old 04-27-2010
Code:
sed 's|.* \([0-9]*\)/\([0-9]*\) .*|High=\1\nLow=\2|'


Last edited by Scrutinizer; 04-27-2010 at 05:34 PM..
# 5  
Old 04-27-2010
Quote:
Originally Posted by Scrutinizer
Code:
sed 's|.* \([0-9]*\)/\([0-9]*\) .*|High=\1\nLow=\2|'

just a small adition. the output format is missing the letter F

Code:
sed 's|.* \([0-9]*\)/\([0-9]*\) .*|High=\1F\nLow=\2F|'

Scrutinizer,

Can you explain the code please

thanks
kamaraj
# 6  
Old 04-27-2010
High kamaraj,

It means save the two patterns of [0-9]* (with a / in between and spaces before and after) so that they can be referenced as \1 and \2 respectively. The patterns are in between \( and \) . Discard the rest. Note that a "|" is used as a separator for subsitition instead of "/ ".

S.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find=grep or maybe sed/awk for multiple lines of text?

Hi, I am running the following: PASS="username/password" sqlplus -s << EOF | grep -v "^$" $PASS set feedback off set heading off set termout off select name from v\$database ; exit EOF Which gives ERROR: ORA-28002: the password will expire within 5 days PSMP1 (1 Reply)
Discussion started by: newbie_01
1 Replies

2. UNIX for Beginners Questions & Answers

How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000. Command: factor/factor 30000000-31000000 Sample output: 30999979 = 30999979 30999980 = 2^2 5 11 140909 30999981 = 3 10333327... (6 Replies)
Discussion started by: steezuschrist96
6 Replies

3. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

4. Shell Programming and Scripting

Grep & sed - Output

Hi All, Facing an issue with grep & sed I have logs as below: gsc_1_20121121.log:2012-11-21 10:09:13,143 INFO - fmsspace.1 ProcessNewOrderSingleRequest: Result - ProcessNewOrderSingleBatchResultDTO - success:true,newOrderSingleBatchResults:ProcessNewOrderSingleResultDTO -... (13 Replies)
Discussion started by: irfanmemon
13 Replies

5. Shell Programming and Scripting

grep output of a text file

Hi. I have a unix script 'if' condition that greps through a file and searches for the strings ERROR and WARNING. if egrep -q 'ERROR|WARNING' myfile.txt; then When this 'if' condition is true then I want to be able to capture 10 lines before the string and 10 lines after the string. So... (6 Replies)
Discussion started by: buechler66
6 Replies

6. Shell Programming and Scripting

Grab characters following grep

Hi all, I'm trying to gather data directly following a keyword in a file but I have no guarantee where it will appear in said file so I can't use cut or anything else that assumes it will be located at a certain position. If anyone has any suggestions I'd be grateful. Here is an example of the... (7 Replies)
Discussion started by: Korn0474
7 Replies

7. Shell Programming and Scripting

Display output as columns using grep/awk/sed

I have several files with say something like cat sparrow I can grep for "cat" and "sparrow" and usually the output is one below the other cat sparrow How can I view these as columns say Pets Birds cat sparrow Would be great if this can be on command line using awk or... (1 Reply)
Discussion started by: gubbu
1 Replies

8. Shell Programming and Scripting

grep reverses # output, but not text?

I have a working script to find out the IP of a hostname like this: #!/bin/bash h="www.google.com" echo $h j=`host $h | grep -o '\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}'` echo "ip: $j" output ip: 74.125.19.99 the strange thing is if I use an IP instead of a hostname for $h, it inverts the... (2 Replies)
Discussion started by: unclecameron
2 Replies

9. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

10. Shell Programming and Scripting

printing portion of the output usind sed/awk

friends, i am a newbie in scripting. could someone help me in selecting only the last column of below ps command output ? mqm 14 16466 0 Sep 15 ? 0:01 /opt/mqm/bin/runmqlsr -r -m QMGR.INBOUNDSSL -t TCP -p 1415 -i 5.1.26.5 mqm 12 16700 0 Sep 15 ? 0:00... (4 Replies)
Discussion started by: unahb1
4 Replies
Login or Register to Ask a Question