Visit Our UNIX and Linux User Community


extracting matched pattern from a line using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting matched pattern from a line using sed
# 1  
Old 10-04-2009
extracting matched pattern from a line using sed

I am trying to pull certain pieces of data out of a line of a file that matches a certain pattern:

[0-9][0-9][.][0-9]

The three pieces that I want to pull out of this line are the only occurrences of that pattern within the line, but the rest of the line is not consistent in each file. Basically the line is the output of uptime, which could be any of the following:

15:11:53 up 3 days, 23:44, 1 user, load average: 0.00, 0.00, 0.00

15:12:10 up 21:24, 1 user, load average: 0.00, 0.00, 0.00

15:12:04 up 1 day, 10 min, 0 users, load average: 0.00, 0.00, 0.00

Even though those examples are all boring (with 0.00 load on all cases) I need to pull out those numbers and put them into separate variables.

I assumed I could do this with sed (mostly because I know sed better than awk or grep) but I have been struggling for a while and would like some guidance...

I have seen plenty of search results dealing with deleting lines that contained the pattern or ones that didn't contain the patters, but nothing about keeping just parts of a line that match the pattern.

I even tried deleting everything in the line that DID NOT match that patters, but that didn't pan out either.

Any suggestions?
# 2  
Old 10-04-2009
Hi, your pattern does not seem to match load averages:
[0-9][0-9][.][0-9] vs. [0-9].[0-9][0-9]

E.g.
ksh:
Code:
echo $line grep -o "\b[0-9]\.[0-9][0-9]"|xargs -n3|read avgload1min avgload5min avgload15min

Code:
echo $avgload1min $avgload5min $avgload15min
0.00 0.00 0.00

or
Code:
line="15:11:53 up 3 days, 23:44, 1 user, load average: 0.00, 0.00, 0.00"

bash:
Code:
read avgload1min avgload5min avgload15min < <(echo $line grep -o "\b[0-9]\.[0-9][0-9]" |xargs -n3)

Code:
echo $avgload1min $avgload5min $avgload15min
0.00 0.00 0.00

Instead of
Code:
grep -o "\b[0-9]\.[0-9][0-9]"|xargs -n3

you could also use
Code:
sed 's/.*e: //;s/,//g'

so:
Code:
echo $line sed 's/.*e: //;s/,//g'|read avgload1min avgload5min avgload15min


Last edited by Scrutinizer; 10-05-2009 at 03:37 AM..
# 3  
Old 10-05-2009
cat > abc.txt
15:11:53 up 3 days, 23:44, 1 user, load average: 0.00, 0.00, 0.00

15:12:10 up 21:24, 1 user, load average: 0.00, 0.00, 0.00

15:12:04 up 1 day, 10 min, 0 users, load average: 0.00, 0.00, 0.00[/html]

cat abc.txt | perl -e 'while (<>){ chomp; my @cols = split(":");($l1,$l2,$l3) = split(/,/,$cols[-1]); print "$l1\t$l2\t$l3\n";}'
0.00 0.00 0.00

0.00 0.00 0.00

0.00 0.00 0.00

Use the variables which are needed ie l1 or l2 or l3 according to your need

Hope this helps .

Cheers
# 4  
Old 10-05-2009
if you wanna take out last three fields..
Code:
awk '{gsub(",","")}{print $NF" "$(NF-1)" "$(NF-2)}' filename|read $var1 $var2 $var3


Previous Thread | Next Thread
Test Your Knowledge in Computers #730
Difficulty: Medium
The first version BASIC language was released on 1 May 1964.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. UNIX for Dummies Questions & Answers

Extracting the two lines where the first line is matched

Hi, If I have a file of something like @hg19_gold_AL122127.6-131160 GCTTCATCATGCATGGATAGGCTGGCGCCTTTCCTGAGGCCATATGCCGATGGATATG @hg19_gold_AL122127.6-131159 CTTTAATATTTCCGCCACCATCCTGAGTGAATCCCAGCAAGGACAGTCTTTGGGGATT @hg19_gold_AL122127.6-131158... (4 Replies)
Discussion started by: jyu429
4 Replies

3. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

4. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

5. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

6. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

7. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

8. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

9. Shell Programming and Scripting

AWK - Extracting matched line

Hi all, I have one more query related to AWK. I have the following csv data: ,qwertyA, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200 ,,,,,,,,,,,,,,,,,,,300,400 ,qwertyB, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200... (9 Replies)
Discussion started by: not4google
9 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies

Featured Tech Videos