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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-22-2006, 11:34 AM
|
#1
|
Member
Registered: Apr 2002
Location: Clevedon, UK
Distribution: SUSE 8.2, 9.2, 10.0 OSS
Posts: 57
Rep:
|
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
|
|
|
08-22-2006, 11:59 AM
|
#2
|
Moderator
Registered: May 2001
Posts: 29,417
|
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
|
|
|
08-22-2006, 12:09 PM
|
#3
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
|
|
08-22-2006, 12:17 PM
|
#4
|
Moderator
Registered: May 2001
Posts: 29,417
|
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...
|
|
|
08-22-2006, 12:42 PM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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 
|
|
|
08-22-2006, 12:52 PM
|
#6
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
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.
|
|
|
08-28-2006, 04:39 AM
|
#7
|
Moderator
Registered: May 2001
Posts: 29,417
|
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.
|
|
|
All times are GMT -5. The time now is 09:24 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|