[H-GEN] modifying file format
Jason Henry Parker
jasonp at uq.net.au
Sun Aug 26 20:24:11 EDT 2001
[ Humbug *General* list - semi-serious discussions about Humbug and ]
[ Unix-related topics. Please observe the list's charter. ]
[ Worthwhile understanding: http://www.humbug.org.au/netiquette.html ]
"Kris Amy" <krisamy1 at optushome.com.au> writes:
> I need to modify a file line by line and make the following changes:-
> here is the line format.
>
> <DATA1>,<DATA2>,<DATA3>,<DATA4>
>
> now what i need to be able to do is in DATA4 remove all spaces, ( and
> ).
perl -F, -lane'BEGIN{$"=","} $F[-1] =~ y/ ()//d; print "@F"'
That will read from files named on the command-line, or from standard
input if no filenames are given, and write to standard output.
The dissection:
-F, split on commas
-l chop newlines when reading, add when printing
-a split each line into array @F, using pattern given after -F
-n put a while(<>){ ... } around the script
-e script follows this argument (in ''s to escape from the shell)
[see perldoc perlrun to find out more information about these
command-line switches.]
BEGIN{$"=","}
start by setting $" to comma; this variable is used as an
output separator when an array is interpolated in a
double-quoted string. This happens once only.
[see perldoc perlvar, and search for $", or $LIST_SEPARATOR.]
$F[-1] =~ y/ ()//d;
the last element of @F (where the comma-separated data values
end up) is put through the y/// operator, which looks for
spaces, and brackets, replacing them with nothing; the /d
modifier deletes them, also.
[see perldoc perlop, and search for `Transliterate'.]
print "@F"
print the modified array; the ""s provide a double-quotish
string, and so commas are reinserted; the -l option adds a
newline again.
This should be enough for you to follow what's going on, and fix it if
you need to.
Cheers!
--
||----|---|------------|--|-------|------|-----------|-#---|-|--|------||
| Mmmmm, impaled flesh! jasonp at uq.net.au |
| -- Ann Burlingham #soc.bi on OPN |
||--|--------|--------------|----|-------------|------|---------|-----|-|
--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.
More information about the General
mailing list