LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-22-2006, 11:34 AM   #1
joadoor
Member
 
Registered: Apr 2002
Location: Clevedon, UK
Distribution: SUSE 8.2, 9.2, 10.0 OSS
Posts: 57

Rep: Reputation: 15
Need to strip words from front of line. sed/awk/grep?


Hi all,

I have been googlin' all afternoon and cannot find a solution to my problem.

I have a text file like this:

Address: somebodies address
Name: billy
Surname: bob
Shoe Size: 9
Telephone Number: 01234 123456

and I want to be able to create a script that can extract the details into variables. ie.

------------------------------------
#!/bin/sh
USERADDRESS=cat filename | sed -e 's/searchfor:/print rest of line'
PERSONSNAME=sed ..........
------------------------------------
so that when you execute the script, it puts "somebodies address" into the USERADDRESS variable (hope this makes sense)

How can this be accomplished? I have played around with grep, sed, and awk but couldn't do it.

Many thanks,
Andy
 
Old 08-22-2006, 11:59 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I have been googlin' all afternoon and cannot find a solution to my problem.
I guess it's hard to look for something you don't know *how* to look for. Might I suggest search LQ for the term s bash and tutorial? You'll probably find:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.tldp.org/LDP/abs/html/
of which you could best read the first two.

USERADDRESS=$(grep -i ^address filename|cut -d ":" -f 2-);
SHOESIZE=$(grep -i "^shoe.size" filename|cut -d ":" -f 2-);
TELNO=$(grep -i "^tel.*num" filename|cut -d ":" -f 2-);

Last edited by unSpawn; 08-22-2006 at 12:05 PM. Reason: //add pointers
 
Old 08-22-2006, 12:09 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Beaten again by unSpawn (druuna removes some text..... )

Instead of using 2 commands (grep and cut) you could use one (bit more resource friendly):

USERADDRESS="`awk -F": " '/^Address/ { print $2 }' infile`"
PERSONSNAME="`awk -F": " '/^Name/ { print $2 }' infile`"
PERSONSSURNAME="`awk -F": " '/^Surname/ { print $2 }' infile`"

Hope this helps.
 
Old 08-22-2006, 12:17 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Optimisation. Cool.
Take 2: ;-p
USERADDRESS=$(grep -i ^address filename); USERADDRESS=${USERADDRESS//A*:/}
SHOESIZE=$(grep -i "^shoe.size" filename); SHOESIZE=${SHOESIZE//S*:/}
TELNO=$(grep -i "^tel.*num" filename); TELNO=${TELNO//T*:/}
There: no "cut" anymore ;-p Of course this breaks any backwards compat with Bourne plus you need to check right case on the first char...
 
Old 08-22-2006, 12:42 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Your last sentence is true, but........ I like parameters and this is a creative way of using them

And, a bit to my surprise: Your solution is faster, not much (0.001s over 20 (2x10) testruns). Yeah I know, this is very precise science
 
Old 08-22-2006, 12:52 PM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33

Code:
sed -ne 's/^Address: //p' filename
In view of optimization, I originally wanted to mention that sed is probably much more heavyweight than grep, but then I had to realize:
Code:
ada@barnabas:~/tmp> ls -hl /bin/grep
-rwxr-xr-x 1 root root 133K 2006-04-23 05:28 /bin/grep
ada@barnabas:~/tmp> ls -hl /bin/sed
-rwxr-xr-x 1 root root 46K 2006-04-23 03:49 /bin/sed
Isn't this weird?

Last edited by spirit receiver; 08-22-2006 at 12:54 PM.
 
Old 08-28-2006, 04:39 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Your last sentence is true
I *know* it is. Unfortunately I had to learn that the hard way.


And, a bit to my surprise: Your solution is faster, not much (0.001s over 20 (2x10) testruns).
Neat. BTW, I thought I was the only one doing timings on stuff like that...


In view of optimization, I originally wanted to mention that sed is probably much more heavyweight than grep
IMHO it all depends on using the right tool for the job ("useless use of cat award") and what features you actually use. If you use (g)awk to only print where you could var=$(< source), then awk can be considered "heavyweight", but if you use it to script your way around glueing data together from three different sources with one awk script then it becomes an indispensable tool worth it's weight in gold.

Wrt sed, iIf you can't work around using sed then you could speed up sed by prefixing your search pattern, though you'll probably only a relative performance gain if you have lots of data to wade through.
 
  


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
diffrence between grep, sed, awk and egrep Fond_of_Opensource Linux - Newbie 3 08-18-2006 08:15 AM
Can grep filter out words? extrasolar Linux - General 1 07-20-2006 03:14 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
How can I awk/sed/grep the IPs from the maillog? abefroman Programming 7 03-09-2006 10:22 AM
How sed strip /* comments aaronzh Programming 1 06-05-2003 05:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:37 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