Need to strip words from front of line. sed/awk/grep?
Linux - SoftwareThis 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.
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.
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.
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.