awk question 
Author Message
 awk question

Hi

Could any kind souls out there point me in the right direction with an
awk query.

I have an /etc/passwd file on a DEC UNIX box with hundreds of entries. I
would like the GECOS description field  to append a string after the
name, BUT before any possible telephone number etc.

Eg line :

phil:*:195:22:Phil Richardson,,245,39284:/home/phil:/bin/ksh

I'd like to see the string <Tweed> appended to each user's real name
like so:

phil:*:195:22:Phil Richardson <Tweed> ,,245,39284:/home/phil:/bin/ksh

Would awk be the best tool to use, and if so can anyone give me a
starter on this one ?

Much obliged to all,

Phil Richardson



Fri, 17 Jan 2003 03:00:00 GMT  
 awk question
Quote:

> Hi

> Could any kind souls out there point me in the right direction with an
> awk query.

> I have an /etc/passwd file on a DEC UNIX box with hundreds of entries. I
> would like the GECOS description field  to append a string after the
> name, BUT before any possible telephone number etc.

> Eg line :

> phil:*:195:22:Phil Richardson,,245,39284:/home/phil:/bin/ksh

> I'd like to see the string <Tweed> appended to each user's real name
> like so:

> phil:*:195:22:Phil Richardson <Tweed> ,,245,39284:/home/phil:/bin/ksh

> Would awk be the best tool to use, and if so can anyone give me a
> starter on this one ?

> Much obliged to all,

> Phil Richardson

It is basically a one-liner in awk:
   awk -f: '{sub(","," <tweed>,",$5); print $0}' /etc/passwd > newfile

The -f: makes the colon, rather than the default of white space, the
field delimiter. The sub function changes the first comma in the 5th
field (the GECOS field if I remember right) to " <Tweed>,". The print
puts the whole (changed) line to stdout.
--
Bob Stearns
University of Georgia

(706)542-5110



Fri, 17 Jan 2003 03:00:00 GMT  
 awk question

Quote:


> > Hi

> > Could any kind souls out there point me in the right direction with an
> > awk query.

> > I have an /etc/passwd file on a DEC UNIX box with hundreds of entries. I
> > would like the GECOS description field  to append a string after the
> > name, BUT before any possible telephone number etc.

> > Eg line :

> > phil:*:195:22:Phil Richardson,,245,39284:/home/phil:/bin/ksh

> > I'd like to see the string <Tweed> appended to each user's real name
> > like so:

> > phil:*:195:22:Phil Richardson <Tweed> ,,245,39284:/home/phil:/bin/ksh

> > Would awk be the best tool to use, and if so can anyone give me a
> > starter on this one ?

> > Much obliged to all,

> > Phil Richardson
> It is basically a one-liner in awk:
>    awk -f: '{sub(","," <tweed>,",$5); print $0}' /etc/passwd > newfile

> The -f: makes the colon, rather than the default of white space, the
> field delimiter. The sub function changes the first comma in the 5th
> field (the GECOS field if I remember right) to " <Tweed>,". The print
> puts the whole (changed) line to stdout.
> --
> Bob Stearns
> University of Georgia

> (706)542-5110

That is not totally correct.

The option to change the delimeter is "-F" not "-f".

Also the above will replace all of the ":" with a space on the output
unless the output field seperator is also changed to :

Try this:

awk -F: 'BEGIN{OFS=":"}{sub(","," <tweed>,",$5); print $0}'

or

awk 'BEGIN{OFS=FS=":"}{sub(","," <tweed>,",$5); print $0}'

Hope it helps,
Ted

--
Cheers,
Ted J. Loeffelholz
Senior Design Engineer
Caterpillar Inc



Fri, 17 Jan 2003 03:00:00 GMT  
 awk question
Many thanks for your help Bob, my awk days start from here.....

Kind Regards

Phil

Quote:


> > Hi

> > Could any kind souls out there point me in the right direction with an
> > awk query.

> > I have an /etc/passwd file on a DEC UNIX box with hundreds of entries. I
> > would like the GECOS description field  to append a string after the
> > name, BUT before any possible telephone number etc.

> > Eg line :

> > phil:*:195:22:Phil Richardson,,245,39284:/home/phil:/bin/ksh

> > I'd like to see the string <Tweed> appended to each user's real name
> > like so:

> > phil:*:195:22:Phil Richardson <Tweed> ,,245,39284:/home/phil:/bin/ksh

> > Would awk be the best tool to use, and if so can anyone give me a
> > starter on this one ?

> > Much obliged to all,

> > Phil Richardson
> It is basically a one-liner in awk:
>    awk -f: '{sub(","," <tweed>,",$5); print $0}' /etc/passwd > newfile

> The -f: makes the colon, rather than the default of white space, the
> field delimiter. The sub function changes the first comma in the 5th
> field (the GECOS field if I remember right) to " <Tweed>,". The print
> puts the whole (changed) line to stdout.
> --
> Bob Stearns
> University of Georgia

> (706)542-5110



Sat, 18 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Simple awk question

2. A Newbie AWK question

3. awk question

4. awk question about matching a pattern

5. AWK question

6. newbie awk question

7. awk questions !!!

8. Simple awk question

9. Easy Awk Question

10. Awk question.

11. awk Question

12. awk question

 

 
Powered by phpBB® Forum Software