0
I have 3 files in a directory: xyz1.txt
, xyz2.txt
, rst_xyz.txt
and I want to work with ONLY the first two (those which start with xyz). The command ls | awk '/xyz/{print $1}'
will return all three of them. What alterations can I make to the command to specify ONLY the xyz* files? I tried ls | awk '/^xyz/{print $1}'
thinking that it would make sure the file started with xyz, but that did not exclude rst_xyz.txt
.
Thanks for the advice not to parse
ls
- that had never occurred to me! The very odd reason is simply so that I can learn exactly how awk works and how to implement regex's in it. I am trying to simply print all files beginning with 'xyz' as you can see from my original question. In your example,awk 'FILENAME ~ /^xyz/ { print $1}'
would be the corollary, but it does not print anything to the screen. – drjrm3 – 2015-01-20T19:04:28.320@Laurbert515 You can
{print FILENAME}
. – Barmar – 2015-01-20T21:39:01.407As it turns out, the problem was, in fact, parsing ls. I am actually using "ls --color" and when I use "ls --color=none | ..." I get the correct thing with my original command. – drjrm3 – 2015-01-21T21:21:11.457
use
--color=auto
. This only colors the output when it's going to a terminal, not when it's redirected to a pipe or file. – Barmar – 2015-01-21T21:27:02.303@Laurbert515, another reason not to parse
ls
– glenn jackman – 2015-01-21T21:55:43.403