| | |
derive full name of user and grep search count
![]() |
•
•
Join Date: Dec 2004
Posts: 4
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2004
Posts: 2
Reputation:
Solved Threads: 0
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
----------------------------------------------------------------------
#!/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
----------------------------------------------------------------------
•
•
•
•
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
•
•
Join Date: Dec 2004
Posts: 4
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Problem with PHP Search Code.... (PHP)
- Search and count for string (C)
- it's simple but yet complicated...php and mysql search.. (PHP)
- Program for Grep search (Pascal and Delphi)
Other Threads in the Shell Scripting Forum
- Previous Thread: Run script in certain X Window?
- Next Thread: kwrite and gedit errors
| Thread Tools | Search this Thread |





