LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-27-2004, 02:57 PM   #1
mehesque
Member
 
Registered: Oct 2003
Location: TX
Distribution: RH 8.0
Posts: 97

Rep: Reputation: 15
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?
 
Old 07-27-2004, 03:16 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 07-27-2004, 03:26 PM   #3
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
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
 
Old 07-27-2004, 03:36 PM   #4
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
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
 
Old 07-27-2004, 03:48 PM   #5
mehesque
Member
 
Registered: Oct 2003
Location: TX
Distribution: RH 8.0
Posts: 97

Original Poster
Rep: Reputation: 15
Thanks a lot guys.

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

$(ls /vmware/number.* | awk 'BEGIN { FS = "." } ; { print $2 }' )
 
Old 07-27-2004, 04:23 PM   #6
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
Spectacular? What?! shell-jutsu, but still not at dan grade
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Script to parse Word/RTF Documents saravanan1979 Programming 10 02-18-2010 07:25 AM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 09:07 PM
Sed & Awk hinetvenkat Linux - Software 4 05-30-2005 05:10 AM
Parse a filename with awk and sed chrisk5527 Programming 2 06-08-2004 06:13 PM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration