LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-06-2008, 03:14 PM   #1
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Rep: Reputation: 15
Extract part of a string


I have this string:

umbillingnumber=1112223333,uniqueidentifier=200803251301508111112223333,ou=defaultGroup,ou=Mailboxes ,ou=DC-Mailbox,o=MailServer

I want to extract everything up to the first comma, i.e.

umbillingnumber=1112223333

I can't use cut -f1 -d",". This will be part of a piped series of commands examining a file with several lines, and other lines may have commas in them, but no other lines have umbillingnumber in them.

The file looks like this:
...
dcucpictalternatenumber 4445556666
dcucpictwirelessnumber 5556667777
umbillingnumber=1112223333,uniqueidentifier=200803251301508111112223333,ou=defaultGroup,ou=Mailboxes ,ou=DC-Mailbox,o=MailServer
dcfindmefollowme <fmfmsettings enabled="on"
...

I looked at sed and awk but don't see how to perform this simple extraction.

Thanks in advance...
 
Old 05-06-2008, 03:19 PM   #2
tidww01
LQ Newbie
 
Registered: Dec 2005
Posts: 19

Rep: Reputation: 0
How about

IFS='
'
cat file | while read aline
echo $aline | grep umbillingnumber
if [ $? -q 0 ]
then
VAR=echo $aline | cut -f1 -d,
other code
fi
done
 
Old 05-06-2008, 03:20 PM   #3
tidww01
LQ Newbie
 
Registered: Dec 2005
Posts: 19

Rep: Reputation: 0
should have been -eq on the test
 
Old 05-06-2008, 03:31 PM   #4
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44

Original Poster
Blog Entries: 1

Rep: Reputation: 15
I don't think that will work. What I have so far is:

mlsmailbox --dump --phone 1112223333 | egrep -e 'dcucpictalternatenumber|dcucpictwirelessnumber|umbillingnumber=|dcfindmefollowme' | cut -f1 -d","

I need a single command that I can use instead of the cut -f1 -d","
 
Old 05-06-2008, 05:17 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Does this work?
Code:
sed 's/\(umbillingnumber=[0-9]*\),.*/\1/'
 
Old 05-06-2008, 05:30 PM   #6
beadyallen
Member
 
Registered: Mar 2008
Location: UK
Distribution: Fedora, Gentoo
Posts: 209

Rep: Reputation: 36
How about:
Code:
 cat file.txt | grep -o 'umbillingnumber=[0-9]\+'

Last edited by beadyallen; 05-06-2008 at 05:31 PM.
 
Old 05-06-2008, 05:42 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,417
Blog Entries: 55

Rep: Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628
Code:
mlsmailbox --dump --phone 1112223333 | awk -F',' '/^umbi/ {print $1}'
?
 
Old 05-07-2008, 07:18 AM   #8
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Wink

Thanks to all who replied. The suggestion by Kenhelm:

sed 's/\(umbillingnumber=[0-9]*\),.*/\1/'

worked perfectly!!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extract substring from string in C baddah Programming 6 02-02-2010 04:22 AM
Extract from stdout the string in [ ], in a nicely way frenchn00b Programming 15 11-23-2007 03:01 AM
anyway to extract a version string from a non running vmlinuz ? rcorkum Slackware 8 12-08-2006 02:33 PM
How do I extract only part of a tarball? punt Linux - General 6 01-26-2006 01:57 AM
How to extract a part of a line by sed? J_Szucs Programming 2 02-15-2003 06:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:48 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
Open Source Consulting | Domain Registration