[olug] bash help (awk/sed)
Vincent.Raffensberger at dtn.com
Vincent.Raffensberger at dtn.com
Fri Jun 18 15:33:46 UTC 2004
I'm sure there are many ways to do it, but here's the easiest way I can
think of (if I understand you correctly):
for filename in `find /your/files`; do
string=`egrep -o [0-9]{6}-[A-F] $filename | awk '{ print $1","}'`
replace $string ${string}${filename} -- $filename
done
'replace' is a utility included in MySQL. It's very handy to have around:
[user at host ~]$ rpm -qf `which replace`
mysql-3.23.56-1.80
[user at host ~]$ man replace
replace(1) MySQL database replace(1)
NAME
replace - A utility program that is used by msql2mysql, but that
has
more general applicability as well. replace changes strings in
place in
files or on the standard input. Uses a finite state machine to
match
longer strings first. Can be used to swap strings.
USAGE
replace [-?svIV] from to from to ... -- [files]
or
replace [-?svIV] from to from to ... < fromfile > tofile
SYNOPSIS
replace [-?|-I] [-s] [-v]
DESCRIPTION
replace
-?|-I info
-s silent
-v verbose
EXTRA INFO
Special characters in from string:
\^ Match start of line.
\$ Match end of line.
\b Match space-character, start of line or end of line. For a
end
\b the next replace starts locking at the end
space-character. A
\b alone in a string matches only a space-character.
EXAMPLE
this command swaps a and b in the given files:
shell> replace a b b a -- file1 file2 ...
SEE ALSO
isamchk (1), isamlog (1), mysqlaccess (1), mysqladmin (1),
mysqlbug
(1), mysqld (1), mysqldump (1), mysqlshow (1), msql2mysql (1),
perror
(1), replace (1), safe_mysqld (1), which1 (1), zap (1),
AUTHOR
Ver 1.0, distribution 3.23.29a Michael (Monty) Widenius
(monty at tcx.se),
TCX Datakonsult AB (http://www.tcx.se). This software comes with
no
warranty. Manual page by L. (Kill-9) Pedersen (kill-9 at kill-9.dk),
Mer-
curmedia Data Model Architect / system developer
(http://www.mercurme-
dia.com)
MySQL 3.23 19 December 2000 replace(1)
Eric Pierce <eric_olug at yahoo.com>
Sent by: olug-bounces at olug.org
06/18/2004 12:37 AM
Please respond to
Omaha Linux User Group <olug at olug.org>
To
olug at olug.org
cc
Subject
[olug] bash help (awk/sed)
Ok, I'm stumped.
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
...
Here's what I have so far:
filename=pg0001.txt && egrep -o [0-9]{6}-[A-F] $filename | awk '{ print $1
","}'
The output so far is 90% there. It looks like this:
300150-D,
300141-C,
300258-D,
300132-A,
...
But I can't figure out how to append the $filename after the comma in each
line. I can't get Awk to resolve the $filename variable. I've tried a
billion
different syntax combinations sticking $filename on the end of the Awk
part,
but to no avail.
I also have a sed version, but the problem is exactly the same.
filename=pg0001.txt && egrep -o [0-9]{6}-[A-F] $filename | sed -n
's/\(.*\)/\1,/p
Any ideas anyone?
Thanks,
Eric Pierce
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
_______________________________________________
OLUG mailing list
OLUG at olug.org
http://lists.olug.org/mailman/listinfo/olug
More information about the OLUG
mailing list