Unix Programming - writing a script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2007 > writing a script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author writing a script
srikanth.bgn@gmail.com

2007-02-27, 1:19 pm

I have a file in the format given below, the first field is user name,
the second field is id and the third field is user DOB. the DOB format
is YYYYMMDD, for some users it is only MMDD.
I need to remove yyyy from the DOB field, there are around 52,000
entries in the file,i need to write a script.

any one please help

pbarbier,1062563,19590607
cromeo,1038485,19620131
smiskovi,1057401,19790825
mmckeeve,1038620,1015
apolanco,1038627,19710109
kbrownjr,1074774,0911
mwargo,1012103,19500626
rlim,1060115,19730121


srikanth,

Bit Twister

2007-02-27, 1:19 pm

On 27 Feb 2007 07:17:53 -0800, srikanth.bgn@gmail.com wrote:
> I have a file in the format given below, the first field is user name,
> the second field is id and the third field is user DOB. the DOB format
> is YYYYMMDD, for some users it is only MMDD.
> I need to remove yyyy from the DOB field, there are around 52,000
> entries in the file,i need to write a script.


You might want to read http://tldp.org/LDP/abs/html/index.html

Untested snippet follows:

-------------- end snippet ---------------

while read line ; do
set -- $line
echo "<$1> <$2>"
done < /your/file_name/here

_ifs_bkup="${IFS}"
IFS=","

while read line
do
set -- $line
echo "<$1> <$2>"
done < y.txt

IFS=$_ifs_bkup

-------------- end snippet ---------------

Redirection of echo output to a file is left as homework.
Bit Twister

2007-02-27, 1:19 pm

On Tue, 27 Feb 2007 10:19:54 -0600, Bit Twister wrote:


Whoops, double paste problem.
Correction follows:

Untested snippet follows:

-------------- end snippet ---------------

_ifs_bkup="${IFS}"
IFS=","

while read line ; do
set -- $line
echo "<$1> <$2>"
done < /your/file_name/here

_ifs_bkup="${IFS}"
IFS="${_ifs_bkup}"

-------------- end snippet ---------------

Redirection of echo output to a file is left as homework.
Jens Thoms Toerring

2007-02-27, 1:19 pm

srikanth.bgn@gmail.com wrote:
> I have a file in the format given below, the first field is user name,
> the second field is id and the third field is user DOB. the DOB format
> is YYYYMMDD, for some users it is only MMDD.
> I need to remove yyyy from the DOB field, there are around 52,000
> entries in the file,i need to write a script.


What about a PERL one-liner?

perl -F, -ane '$F[2]%=10000;printf "%s,%d,%04d\n",@F' < in_file > out_file

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Chris F.A. Johnson

2007-02-27, 7:16 pm

On 2007-02-27, srikanth.bgn@gmail.com wrote:
> I have a file in the format given below, the first field is user name,
> the second field is id and the third field is user DOB. the DOB format
> is YYYYMMDD, for some users it is only MMDD.
> I need to remove yyyy from the DOB field, there are around 52,000
> entries in the file,i need to write a script.
>
> any one please help
>
> pbarbier,1062563,19590607
> cromeo,1038485,19620131
> smiskovi,1057401,19790825
> mmckeeve,1038620,1015
> apolanco,1038627,19710109
> kbrownjr,1074774,0911
> mwargo,1012103,19500626
> rlim,1060115,19730121


awk -F, -v OFS=, 'length($3) == 8 {$3 = substr( $3, 5)} {print}' FILENAME


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
John W. Krahn

2007-02-27, 7:16 pm

srikanth.bgn@gmail.com wrote:
> I have a file in the format given below, the first field is user name,
> the second field is id and the third field is user DOB. the DOB format
> is YYYYMMDD, for some users it is only MMDD.
> I need to remove yyyy from the DOB field, there are around 52,000
> entries in the file,i need to write a script.
>
> any one please help
>
> pbarbier,1062563,19590607
> cromeo,1038485,19620131
> smiskovi,1057401,19790825
> mmckeeve,1038620,1015
> apolanco,1038627,19710109
> kbrownjr,1074774,0911
> mwargo,1012103,19500626
> rlim,1060115,19730121


$ echo "pbarbier,1062563,19590607
cromeo,1038485,19620131
smiskovi,1057401,19790825
mmckeeve,1038620,1015
apolanco,1038627,19710109
kbrownjr,1074774,0911
mwargo,1012103,19500626
rlim,1060115,19730121" | PERL -lpe's/,\d{4}(\d{4})/,$1/'
pbarbier,1062563,0607
cromeo,1038485,0131
smiskovi,1057401,0825
mmckeeve,1038620,1015
apolanco,1038627,0109
kbrownjr,1074774,0911
mwargo,1012103,0626
rlim,1060115,0121




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Logan Shaw

2007-02-28, 1:22 am

srikanth.bgn@gmail.com wrote:
> I have a file in the format given below, the first field is user name,
> the second field is id and the third field is user DOB. the DOB format
> is YYYYMMDD, for some users it is only MMDD.
> I need to remove yyyy from the DOB field, there are around 52,000
> entries in the file,i need to write a script.
>
> any one please help
>
> pbarbier,1062563,19590607
> cromeo,1038485,19620131
> smiskovi,1057401,19790825
> mmckeeve,1038620,1015
> apolanco,1038627,19710109
> kbrownjr,1074774,0911
> mwargo,1012103,19500626
> rlim,1060115,19730121


Try this:

sed -e 's/,....\(....\)$/\1/' input-file > output-file

The "...." parts of the expression will each match sequences of
any four characters. The "\(" and the "\)" will tell sed to
remember the enclosed characters that matched. The "$" tells
it to only perform the match against the end of the string.
And finally, the "\1" tells it to replace the entire matched
thing with the remembered thing.

This pattern should only match 8-digit fields because there
are 8 of the "." characters. 4-digit fields at the end will
be left alone since they will not match.

- Logan
Bill Pursell

2007-02-28, 7:17 pm

On Feb 27, 3:17 pm, srikanth....@gmail.com wrote:
> I have a file in the format given below, the first field is user name,
> the second field is id and the third field is user DOB. the DOB format
> is YYYYMMDD, for some users it is only MMDD.
> I need to remove yyyy from the DOB field, there are around 52,000
> entries in the file,i need to write a script.
>
> any one please help
>
> pbarbier,1062563,19590607
> cromeo,1038485,19620131
> smiskovi,1057401,19790825
> mmckeeve,1038620,1015
> apolanco,1038627,19710109
> kbrownjr,1074774,0911
> mwargo,1012103,19500626
> rlim,1060115,19730121
>


Here's a sed one-liner: (YMMV, and greatly
depend on the version of sed you've got.)

sed 's/\(.*,\)[0-9]*\([0-9]\{4\}\)/\1\2/' input

This works with GNU sed version 4.1.2

(Note: I just noticed Logan's sed answer...his
is better, IMO)

shakahshakah@gmail.com

2007-02-28, 7:17 pm

On Feb 27, 10:26 pm, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> srikanth....@gmail.com wrote:
>
>
>
> Try this:
>
> sed -e 's/,....\(....\)$/\1/' input-file > output-file
>
> The "...." parts of the expression will each match sequences of
> any four characters. The "\(" and the "\)" will tell sed to
> remember the enclosed characters that matched. The "$" tells
> it to only perform the match against the end of the string.
> And finally, the "\1" tells it to replace the entire matched
> thing with the remembered thing.
>
> This pattern should only match 8-digit fields because there
> are 8 of the "." characters. 4-digit fields at the end will
> be left alone since they will not match.
>
> - Logan


There still could be some accidental matches there, though, e.g. if
the
input file has a line like:

smiskovi,401,0825

Maybe the following avoids that?
sed -e 's/,[0-9][0-9][0-9][0-9]\([0-9][0-9][0-9][0-9]\)$/\1/' input-
file > output-file

Logan Shaw

2007-03-01, 1:25 am

shakahshakah@gmail.com wrote:
> On Feb 27, 10:26 pm, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
>
> There still could be some accidental matches there, though, e.g. if
> the
> input file has a line like:
>
> smiskovi,401,0825
>
> Maybe the following avoids that?
> sed -e 's/,[0-9][0-9][0-9][0-9]\([0-9][0-9][0-9][0-9]\)$/\1/' input-
> file > output-file


Oops! Good point. I think I might've been assuming that the second
field was also a date, and that therefore could never be shorter
than 4 characters. Regardless of what I was assuming, I came up with
a solution that isn't correct. :-)

- Logan
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com