LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-16-2009, 08:08 PM   #1
SI_TW
LQ Newbie
 
Registered: Dec 2007
Posts: 17

Rep: Reputation: 0
Question parsing with sed


I'm trying to extract a value from a file. The line I'm trying to read looks like:

05:01.0 Bridge: Device 80cc:11dd (rev 0b)

I know that 80cc: will always appear the same but the 11dd value may change. I was trying to use 'sed' to extract the 11dd value to a variable but I can't figure out how to do it. Is this the correct command to be using? Anyone know how to do this?
 
Old 07-16-2009, 08:16 PM   #2
karamarisan
Member
 
Registered: Jul 2009
Location: Illinois, US
Distribution: Fedora 11
Posts: 374

Rep: Reputation: 55
I don't know how to do it with sed, but if you've got Perl, I think this should work:

Code:
perl -ne "/80cc:([0-9a-f]{4})/; print $1;"
You can pipe something to it, or you can pass it a file as another arg. Note that this is just that part of the string; no newline or anything. Add print '\n'; into the double quotes at the end if you want that.
 
Old 07-16-2009, 08:21 PM   #3
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
It can be done with sed, for example:

sed 's/.*://;s/ .*//'

but you could also use a couple cut commands:

cut -f 4 -d : | cut -f 1 -d ' '
 
Old 07-16-2009, 08:33 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Or different sed variant:
Code:
echo "05:01.0 Bridge: Device 80cc:11dd (rev 0b)" | sed -r 's/.*80cc:([^ ]+).*/\1/'
11dd

Cheers,
Tink
 
Old 07-16-2009, 08:36 PM   #5
SI_TW
LQ Newbie
 
Registered: Dec 2007
Posts: 17

Original Poster
Rep: Reputation: 0
progress

I was able to get the sed command to work. The perl command just seemed to print the entire file. Now I just need to figure out how to use that cut command to remove the whitespace. All I really need to do is assign the value to a variable. Not sure if I need to remove the whitespace first.
 
Old 07-16-2009, 08:42 PM   #6
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
There shouldn't be any whitespaces left in the sed output as it is...? You can dump the output of a command into a variable like this:

VARIABLE="$(sed 'whichevermethodyoulike' [input file])"

Last edited by alienDog; 07-16-2009 at 08:48 PM.
 
Old 07-16-2009, 08:53 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
VARIABLE=$( sed -r '/80cc:/ s/.*80cc:([^ ]+).*/\1/' file )
P.S.: Beat by the edit =)
 
Old 07-16-2009, 09:04 PM   #8
SI_TW
LQ Newbie
 
Registered: Dec 2007
Posts: 17

Original Poster
Rep: Reputation: 0
Yeah that worked but now I can't concatenate the value with another. I want to set the variable so that it is preceded by 0x.
if I get

echo $temp
11dd

Using var=0x$11dd
gets me
0x 11dd

and trying to assign
var=0x`sed .....`
gets me
0x 11dd
as well
 
Old 07-16-2009, 09:10 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
VARIABLE="Ox$(sed -r '/80cc:/ s/.*80cc:([^ ]+).*/\1/' file)"                                                        
echo $VARIABLE
Ox11dd
 
Old 07-16-2009, 09:15 PM   #10
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
I don't get any extra spaces either:

Code:
VARIABLE="0x$(sed 's/.*://;s/ .*//' file)"
echo $VARIABLE
0x11dd

Last edited by alienDog; 07-16-2009 at 09:19 PM.
 
Old 07-16-2009, 09:32 PM   #11
SI_TW
LQ Newbie
 
Registered: Dec 2007
Posts: 17

Original Poster
Rep: Reputation: 0
Using this:

VARIABLE="Ox$(sed -r '/80cc:/ s/.*80cc[^ ]+).*/\1/' file)"
echo $VARIABLE
"Entire file Contents"
0x 11dd

OR
this:

VARIABLE="0x$(sed 's/.*://;s/ .*//' file)"
echo $VARIABLE
0x 11dd

I also tried "

VARIABLE="0x`sed 's/.*://;s/ .*//' tmp`"
echo $VARIABLE
0x 11dd

The same each time.
 
Old 07-16-2009, 09:34 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
How about you show us a good part of the file?

And where does it come from, is it a windows/DOS file?
 
Old 07-16-2009, 09:35 PM   #13
SI_TW
LQ Newbie
 
Registered: Dec 2007
Posts: 17

Original Poster
Rep: Reputation: 0
And I guess the first one is just some dump from the file and not actually in the variable. It is always just "0x 11dd"
 
Old 07-16-2009, 09:45 PM   #14
SI_TW
LQ Newbie
 
Registered: Dec 2007
Posts: 17

Original Poster
Rep: Reputation: 0
Here is the econd half of the file created using:
lspci > tmp

00:1d.1 USB Controller: Intel Corporation 82801JI (ICH10 Family) USB UHCI Controller #2
00:1d.2 USB Controller: Intel Corporation 82801JI (ICH10 Family) USB UHCI Controller #3
00:1d.7 USB Controller: Intel Corporation 82801JI (ICH10 Family) USB2 EHCI Controller #1
00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev 90)
00:1f.0 ISA bridge: Intel Corporation 82801JIB (ICH10) LPC Interface Controller
00:1f.2 IDE interface: Intel Corporation 82801JI (ICH10 Family) 4 port SATA IDE Controller
00:1f.3 SMBus: Intel Corporation 82801JI (ICH10 Family) SMBus Controller
00:1f.5 IDE interface: Intel Corporation 82801JI (ICH10 Family) 2 port SATA IDE Controller
01:00.0 VGA compatible controller: nVidia Corporation GeForce 8400 GS (rev a1)
03:00.0 IDE interface: JMicron Technologies, Inc. JMB368 IDE controller
04:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller (rev 02)
05:01.0 Bridge: Device 80cc:11dd (rev 0b)

VARIABLE="0x`sed 's/.*://;s/ .*//' tmp`"
echo $VARIABLE
0x 11dd
 
Old 07-16-2009, 10:01 PM   #15
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
You don't really need to dump lspci to a file before sed if you don't need the output otherwise, you can just do:

VARIABLE=0x$(lspci | sed '/80cc/!d;s/.*://;s/ .*//')

The above line should also fix your whitespace problem.

Last edited by alienDog; 07-16-2009 at 10:12 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help w/ sed parsing special characters clem_c_rock General 8 08-31-2007 04:06 PM
Sed or Awk question, looking for parsing help rwartell Linux - Software 2 05-17-2006 11:59 PM
Sed or Awk question, looking for parsing help rwartell Programming 1 05-17-2006 04:42 PM
Whitespace parsing sed? carl.waldbieser Programming 1 12-12-2005 04:24 PM
sed parsing question ncblues Linux - Newbie 5 01-03-2005 06:36 AM


All times are GMT -5. The time now is 12:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration