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

4
Contributors
6
Replies
7
Views
12 Years
Discussion Span
Last Post by JamesWhite..
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

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

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

0

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

marco83

0

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

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

marco83

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

0

#!/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 !!!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.