LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Need to strip words from front of line. sed/awk/grep? (http://www.linuxquestions.org/questions/linux-software-2/need-to-strip-words-from-front-of-line-sed-awk-grep-476271/)

joadoor 08-22-2006 11:34 AM

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

unSpawn 08-22-2006 11:59 AM

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-);

druuna 08-22-2006 12:09 PM

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.

unSpawn 08-22-2006 12:17 PM

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...

druuna 08-22-2006 12:42 PM

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 ;)

spirit receiver 08-22-2006 12:52 PM

:D
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?

unSpawn 08-28-2006 04:39 AM

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 08:06 AM.