Computing.Net > Forums > Unix > search for partial sting -grep/awk?

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

search for partial sting -grep/awk?

Reply to Message Icon

Original Message
Name: ihsail
Date: August 31, 2005 at 08:03:05 Pacific
Subject: search for partial sting -grep/awk?
OS: Unix
CPU/Ram: Shell
Comment:

Hi,

Please help...

I would like to do the following:

I have a file name num_key.txt, this file only contains one line.
Content of file:
/usr/home/data/abc-212-20050801.txt

Content changes, it can be the following:
aa-313-20050801.txt

I need to extract the "212" from this line and use it as a parameter for something else. The 3-digit number is always between the dashes "-###-".

How do I "extract" this 3-digit number from the file and pass it to a variable?

Thank you very much.


ihsail


Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: August 31, 2005 at 10:53:40 Pacific
Reply:

var=`cut -f 2 -d '-' num_key.txt`
var=`awk -F"-" '{ print $2 }' num_key.txt`


Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: August 31, 2005 at 11:01:07 Pacific
Reply:

There are probably better solutions, but in awk you can change the field separator to a "-" and the second field should be your answer:

#!/bin/ksh

var=$(awk ' BEGIN { FS = "-" } { print $2 } ' num_key.txt)
echo $var


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home



Results for: search for partial sting -grep/awk?

Search for file within shell
    Summary: Hi I have a script that searches for the last file modified & runs two differnt commandeds to apend to another file. I have aproblem with my find as it cannot find the file if I run the script from th...
www.computing.net/answers/unix/search-for-file-within-shell/6982.html

Searching for more then one word
    Summary: Hello all I have a little problem. I want to search for more then one word in a line example below [18:16:15.537744] [1] EVENT: FORCE_STARTJOB JOB: TuitionCalc_6 [18:16:17.078595] [4] EVENT: F...
www.computing.net/answers/unix/searching-for-more-then-one-word/5027.html

Search for words in a text file
    Summary: Hi, I am a unix newbie. I am trying the following to search for a number of words in a text file (@unix command prompt), but the entire file contents are displayed. Can anyone tell me where I am makin...
www.computing.net/answers/unix/search-for-words-in-a-text-file/7915.html








Which MP3 player do you have?

iPod/iPhone
Zune
Something Else
None


View Results

Poll Finishes Today.
Discuss in The Lounge
Poll History






Data Recovery Software