Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk for a specific word and only that word 1

Status
Not open for further replies.

sunixadm

Technical User
Sep 20, 2001
73
DE
Hi,

I'm sure that I'm making this harder than it really is and would appreciate any help.

I have written a script that reports the size of home directories and compares the information to previously collected data.

I am using awk to find the user in a list containing the directory size and the users name:

11096 admin
23456 bsmith
27869 jhart
223 bak-admin
7789 kbasmith

So, when I grep (/usr/bin/grep on Solaris 8) for admin I get the output for both bak-admin and admin.

This isn't the actual list of users (directories) but represents our naming convention for user home directories.
Below is an example of what I am doing in my script and what I am getting as out put:

#!/bin/sh
oldlist=list.old
user=`awk '{ print $2 }' $oldlist`
echo $user
oldsize=`for i in $user
do
grep "\<$i\>" $oldlist | awk '{ print \$1 }'
done`
echo $oldsize

The contents list.old is:

11096 admin
23456 bsmith
27869 jhart
223 bak-admin
7789 kbasmith

Running the script results in following output:

admin bsmith jhart bak-admin kbasmith lost+found
11096 223 23456 27869 223 7789 2234

As you can see the script is reporting the "$oldsize" for admin is reported as 11096 and 223.
This is wrong and causes problems with the rest of my script.

I have also tried /usr/xpg4/bin/grep -E and /usr/xpg4/bin/grep -F -x but neither are the correct solution.

Thanks in advance.

-Joe
 
This awk script is untested but may get you started

awk -f sunixadm.awk list.old

#sunixadm.awk
{
printf $2 " "
a[++n] = $1
}
END {
print ""
for (j=1;j<=n;j++) printf a[j] " "
print ""
}

CaKiwi
 
CaKiwi,

Thanks for your quick response. It's exactly what I needed!

-Joe
 
Hello,

The simplest is to add a space before $i in your grep:

=> grep " \<$i\>" instead of grep "\<$i\>" .

It works but isn't too elegant.

Your problem is that grep "admin" returns 2 lines.
grep -w doesn't seem too helpful. Special regexps in egrep should do. But here again nothing seems as staight forward as adding the space. A word of caution: don't forget to add a debug remark in your script saying you added a space in the command. spaces are always pretty tricky when you start debugging.
Cheers,
Sibawe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top