using awk on only files which START with 'xyz'

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.

drjrm3

Posted 2015-01-20T18:21:22.993

Reputation: 1 164

Answers

2

Don't parse ls!

You want

awk '...' xyz*

If, for some very odd reason, you want to do this entirely within awk:

awk 'FILENAME ~ /^xyz/ { .... }' *

glenn jackman

Posted 2015-01-20T18:21:22.993

Reputation: 18 546

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.407

As 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

1

no need for awk. just use filename glob:

ls xyz*

most likely you will want to pipe the filenames to work on them? like this maybe?

ls xyz* | some other commands

in that case it is better to use a for loop.

for filename in xyz*
do
  some other commands "$filename"
done

note that in the first variant the filenames will come as a stream to stdin of some other commands while in the for loop the filenames will come one by one as a command line argument for some other command.

lesmana

Posted 2015-01-20T18:21:22.993

Reputation: 14 930

Thank you. There are a variety of ways to do the task in question, but I was specifically asking for the awk method because I am trying to learn awk. – drjrm3 – 2015-01-20T18:37:26.210

ok i understand. in that case you should provide more details about your system in your question because awk '/^xyz/{print $1}' works as expected on my system. also your question is probably better asked on stackoverflow.com as it is a programming question. – lesmana – 2015-01-20T18:43:08.603

Thank you, I will repost on stackoverflow. To be clear though, /^xyz/ performs the desired function on your system? If that is the case, then I will update my question on stackoverflow with more details about my system if I cannot figure out the problem on my own. – drjrm3 – 2015-01-20T18:45:32.683

There's no reason to repost this on SO. It's in the overlap between SU, SO, and unix.SE, and one question should be enough. – Barmar – 2015-01-21T20:14:01.193

1

ls | awk '/^xyz/'

Your original solution was just missing the ^ in the regep, which anchors the match to the beginning of the line. You don't need a print block because the default action when finding a match is to print the line. If you want to be explicit, you could write:

ls | awk '/^xyz/ {print $0}'

You should use $0 rather than $1 in case the filename contains spaces (although if the filename contains newline, this will still treat it as multiple names).

Barmar

Posted 2015-01-20T18:21:22.993

Reputation: 1 913

I actually have the /^xyz/ solution in my original question and it does not work on my system. Is my issue just system dependent you think? – drjrm3 – 2015-01-21T20:00:02.217

I don't think it should be system-dependent. This is an extremely basic command and should work in every version of awk. I just tried it on both OS X and Debian Linux and it worked fine. I suspect you mistyped something when you tried it. – Barmar – 2015-01-21T20:12:41.087

Thanks, see my comment on the selected answer. I was typing it correctly, but I was piping from "ls --color" and not just "ls". – drjrm3 – 2015-01-21T21:21:46.017