extract string from filename
Ben Scott
dragonhawk at gmail.com
Thu Jan 12 20:26:02 EST 2006
On 1/12/06, Zhao Peng <greenmt at gmail.com> wrote:
> I'm back, with another "extract string" question. //grin
It sounds like you could use a tutorial on Unix text processing and
command line tools, specifically, one which addresses pipes and
redirection, as well as the standard text tools (grep, cut, sed, awk,
etc.). While Paul's recommendation about the O'Reilly regular
expressions book is valid, I suspect it might be a little too focused
on regex's and not cover some of the *other* elements you seem to be
needing.
It's been forever for me, but I seem to recall that _Unix Power
Tools_, also published by O'Reilly, covers all of the above and much,
much more. If others on this list second my suggestion, you might
want to obtain a copy. Alternatively, maybe list members can suggest
alternatives?
There are also a number of free guides at the Linux Documentation
Project. See:
http://www.tldp.org/guides.html
Look for anything mentioning "bash" (the Bourne-again shell) or
scripting. I can't speak as to how good they are, but you can't beat
the price.
Anyway, on to your question...
> I tried to use "cut" commands, but can't even figure out how to use the
> filenames as input. Anyone care to offer me a hint?
You'll want to pipe the output of "ls" to cut. This should get you started:
ls -1 | cut -d _ -f 2
The "-1" switch to ls(1) tells it to output a single column of file
names. Some versions of "ls" do this automagically when using
redirection, but it is best to be sure. The "-d _" switch to cut(1)
tells cut to split fields on the underscore. The "-f 2" selects the
second field.
See also: sort(1), uniq(1)
Hope this helps!
-- Ben "Unix plumber" Scott
More information about the gnhlug-discuss
mailing list