944,016 Members | Top Members by Rank

Ad:
Dec 29th, 2004
0

derive full name of user and grep search count

Expand Post »
I hope somebody could help me with a couple of things...

First, I would like to search the /etc/passwd file for the full name of a user given his/her username. i've tried the following command:

cat etc/passwd | grep -w '`whoami`' | cut -f 5 -d ':'

of course that didn't work as grep interprets whoami as a string, not as a command becuase of the ' '.

So does anyone know how i can get over this problem?

Second, i have a text file that contains a names of items, the price, and the date it was purchased. The content looks something like this:

t-shirt|20|-
socks|10|2004-12-10
shoes|40|2004-12-10

the item name, price and date are separated by '|' and the '-' denotes that the item wasnt purchased yet

So what i need to do here is to echo the unpurchased items and then the purchased items, i used grep to do this and it worked.

But to add, what i needed was to add a number before each item as it is displayed on the screen, something like this:

UNpurchased items:

1. tshirt 20

purchased items:

1. socks 10
2. shoes 40

they also need to be aligned as shown, can anyone show me how to do this? Help would be very mush appreciated.

Sincerely,
marco83
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marco83 is offline Offline
4 posts
since Dec 2004
Dec 29th, 2004
0

Re: derive full name of user and grep search count

I would address your second problem first. This little/simple script would do your job nicely. Its simple, so you can enhance it as per your need

#!/bin/ksh
count=1
echo "UNPurchased Items"
echo
for name in `grep "\-$" tmp11 | awk -F"|" '{print $1}'`
do
echo "$count. $name"
count=`expr $count + 1`
done
count=1
echo
echo "Purchased Items"
echo
for name in `grep -v "\-$" tmp11 | awk -F"|" '{print $1}'`
do
echo "$count. $name"
count=`expr $count + 1`
done

NOTE: this is the KSH version, if you are a perl guy then you must be knowing ( may be not able to recollect on the top of your head ) that you have the env variable "$." that contains the line number of the input file being read.....just a hint.

Now your first problem. I don't know what prompted you to use those " ' " ( single quotes) in grep.
I don't have a linux box here, i tried on a Sun Box.
cat /etc/passwd | grep ` who am i |awk -F " | " '{ print $1 }'` | cut ------

( you can specify the arguments in 'cut' according to the format of etc/passwd on your machine )

I hope it helps

----------------------------------------------------------------------

Quote originally posted by marco83 ...
I hope somebody could help me with a couple of things...

First, I would like to search the /etc/passwd file for the full name of a user given his/her username. i've tried the following command:

cat etc/passwd | grep -w '`whoami`' | cut -f 5 -d ':'

of course that didn't work as grep interprets whoami as a string, not as a command becuase of the ' '.

So does anyone know how i can get over this problem?

Second, i have a text file that contains a names of items, the price, and the date it was purchased. The content looks something like this:

t-shirt|20|-
socks|10|2004-12-10
shoes|40|2004-12-10

the item name, price and date are separated by '|' and the '-' denotes that the item wasnt purchased yet

So what i need to do here is to echo the unpurchased items and then the purchased items, i used grep to do this and it worked.

But to add, what i needed was to add a number before each item as it is displayed on the screen, something like this:

UNpurchased items:

1. tshirt 20

purchased items:

1. socks 10
2. shoes 40

they also need to be aligned as shown, can anyone show me how to do this? Help would be very mush appreciated.

Sincerely,
marco83
Reputation Points: 10
Solved Threads: 0
Newbie Poster
plavania is offline Offline
2 posts
since Dec 2004
Dec 29th, 2004
0

Re: derive full name of user and grep search count

thanks plavania, im just a newbie in unix so will this script also work in bash?

marco83
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marco83 is offline Offline
4 posts
since Dec 2004
Dec 29th, 2004
0

Re: derive full name of user and grep search count

Ya , it works. Not sure if you make any changes/enhancements to it. give a try, and if you have any problem please do report. May be i will be of help
Thanks

Quote originally posted by marco83 ...
thanks plavania, im just a newbie in unix so will this script also work in bash?

marco83
Reputation Points: 10
Solved Threads: 0
Newbie Poster
plavania is offline Offline
2 posts
since Dec 2004
Dec 31st, 2004
0

Re: derive full name of user and grep search count

ive got new problems, the loop prints one word per line like

1. bond
2. paper
3. (short)
4. brown
5. envelope
6. (long)

when it should print:
1. bond paper (short)
2. brown envelope (long)

i used the following loop for this:

count1=1
user=`whoami`
for name in `grep "-" .ushop.$user | cut -f 1 -d '|'`
do
echo "$count1. $name "
count1=`expr $count1 + 1`
done

the .ushop.$user file has the following text in it:

Pencil|10|-
Bond paper (short)|150|2004-4-7
Bond paper (long)|160|-
Brown envelope (short)|3|-
Brown envelope (long)|4|2004-2-12
Ballpen (black)|9|-
Ballpen (blue)|9|2004-11-22
Ballpen (red)|9|-
Paper clips|20|-
Index cards (3x5)|20|-

Another problem: i don't know how to print the item name with the second part of the line (which should be the price)

it should something like:

1. Bond paper (short) 150
2. Bond paper (long) 160
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marco83 is offline Offline
4 posts
since Dec 2004
Nov 12th, 2010
0
Re: derive full name of user and grep search count
#!/bin/bash
clear
echo ""
grep $(whoami) /etc/passwd > allf
username=`awk -F ":" '{print $1}' allf`
echo "nombre de usuario: $username"
echo ""
fullname=`awk -F ":" '{print $5}' allf`
echo "nombre completo: $fullname"
echo ""
homeadress=`awk -F ":" '{print $6}' allf`
echo "ruta del home: $homeadress"
echo ""
shellkind=`awk -F ":" '{print $7}' allf`
echo "tipo de shell: $shellkind"
echo ""
exit 0

it's a simple shape for do this excercise, with ot if, else, while, o for,
i hope it could help you.
SUCCES AND GOOD LOOK !!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jhonvyx is offline Offline
1 posts
since Nov 2010
Nov 18th, 2010
0
Re: derive full name of user and grep search count
Such a this code is use for my new project and its very powerful so thanks....
Reputation Points: 8
Solved Threads: 0
Newbie Poster
JamesWhite.. is offline Offline
6 posts
since Nov 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Background Process Print To Console
Next Thread in Shell Scripting Forum Timeline: Shell scripting help required urgently





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC