[olug] bash help (awk/sed)
Hugh Jarce
hjarce2001 at yahoo.com
Mon Jul 12 00:17:43 UTC 2004
Eric Pierce wrote:
> I'm trying to pull from a file (several files actually) all
> six digit combinations followed by a dash and a letter A-F
> (ex. 123456-D). Then I want to append a comma AND the
> filename that it is pulling from.
>
> Example.
> 123456-D,file01.txt
> 654321-D,file01.txt
Mike Hostetler wrote:
> Or use awk for everything:
>
> for f in `ls -1 *.txt`; do
> awk --assign infile=$f '/[0-9]+?-[A-F]/ {print $0 "," infile}' $f
> done
That $0 prints the whole line not just the matching 123456-D.
This one-liner seems to do the trick:
perl -nle '/(\d{6}-[A-F])/ && print "$1,$ARGV"' *.txt
It also should be a lot faster because it does not fork a new
process for each file.
Hugh
---------------------------------
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
More information about the OLUG
mailing list