LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I need to parse a word: awk or sed? (https://www.linuxquestions.org/questions/programming-9/i-need-to-parse-a-word-awk-or-sed-210302/)

mehesque 07-27-2004 03:57 PM

I need to parse a word: awk or sed?
 
I just need a one-liner. I search this directory for a file named finished.[then a number] "finished.3". It will never go over 9. I just want to grab the number from the end of the file name to put it in an if statement.

$(ls /dir/number.* | %%%%% )

I'm thinking either awk or sed, but I've never run across anything that parses words. It can be based on the the characters of the line too...

Thoughts?

druuna 07-27-2004 04:16 PM

Grepping the number-part from a string (3 if finished.3)

Using sed:
ls finished.[0-9] | sed 's/finished\.\([0-9]\)/\1/'

Using awk:
ls finished.[0-9] | awk -F"." '{ print $2 }'

There are probably many more ways to do this.

Hope this helps.

Bebo 07-27-2004 04:26 PM

You can also use cut:
Code:

ls finished.[0-9] | rev | cut -c1
bash has some nice string handling; try this for instance:
Code:

for FILE in `ls finished.[0-9]` ; do echo ${FILE##*\.} ; done

bruce ford 07-27-2004 04:36 PM

Quote:

ls finished.[0-9] | rev | cut -c1
great bebo! This is really 'Zen or the art of shell programming'! always appreciate short and simple but spectacular solutions! Keep it up!

bruce

mehesque 07-27-2004 04:48 PM

Thanks a lot guys.

I ended up going with awk and changing the field separator.

$(ls /vmware/number.* | awk 'BEGIN { FS = "." } ; { print $2 }' )

Bebo 07-27-2004 05:23 PM

Spectacular? What?! ;):D shell-jutsu, but still not at dan grade :p:)


All times are GMT -5. The time now is 12:03 AM.