Computing.Net > Forums > Unix > Extracting string using regular exp

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Extracting string using regular exp

Reply to Message Icon

Name: toshio
Date: November 13, 2003 at 04:29:32 Pacific
OS: Solaris Cshell
CPU/Ram: 128
Comment:

Hi. I need help to extract a particular string in unix.Here's the text i have.

18713440 0 53 Nov 12 17:43 ./t3ix
2342344 3 446 Nov 14 15:23 ./t4

I need to extract date info, e.g Nov 12
As this is always 3 characters, space then 2 numeric, would using regular expression combined with sed be the answer ? Or are there other ways to extract that string. Thanks for your help




Sponsored Link
Ads by Google

Response Number 1
Name: toshio
Date: November 13, 2003 at 04:31:41 Pacific
Reply:

Oops. The spaces in the text are not fixed. It's always different for diffent rows. Thanks


0

Response Number 2
Name: nails
Date: November 13, 2003 at 08:15:25 Pacific
Reply:

Hi:

Are the fields always the 4th and 5th? If so, awk will work:

awk ' { print $4 $5 } ' data.file

Regards,

Nails


0

Response Number 3
Name: toshio
Date: November 14, 2003 at 03:10:22 Pacific
Reply:

Thanks for your help Nails ! This was exactly what I was looking for. Had difficulty with the 'cut' cmd, but I guess this is good enough. Actually I'm trying to execute this piece of code so I can traverse through the output easily.

set tester_array=(`find . -ls|grep txt| egrep -v "Nov 14|Nov 13|t3st1|t3st2"| awk '{print $8 " " $9 " " $10 " " $11}'`)

However I'm getting this error msg.
Too many words from ``

I'm just puzzled as it seems to work when I type in

find . -ls|grep txt| egrep -v "Nov 14|Nov 13|t3st1|t3st2"| awk '{print $8 " " $9 " " $10 " " $11}'

Any idea's of how to include white space in between my field's ? Thanks.



0

Response Number 4
Name: Nails
Date: November 14, 2003 at 12:47:39 Pacific
Reply:

Hi:

awk has a printf specification similar to the "C" languages printf. That will preserve your whitespace.

I'm not certain what you are doing, but it looks like you only want files that are "txt". Something like this is a little more efficient:

# all on one line
find . -name "*txt*" -print|xargs egrep -v Nov| awk ' { printf("%s %s\n"), $4 ,$5 } '

Regards,

Nails


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Array size in shell scrip... Append to array: c shell



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


Sponsored links

Ads by Google


Results for: Extracting string using regular exp

Extract text using sed www.computing.net/answers/unix/extract-text-using-sed/5169.html

How to use 'sed' on windows XP. www.computing.net/answers/unix/how-to-use-sed-on-windows-xp/6605.html

Replace string using tr command www.computing.net/answers/unix/replace-string-using-tr-command/5645.html