|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
If with sed loop question
Hi, $list_A contains a list of user ID's. $pwdfile_new is the unix password file. $useralias is a report file I'm trying to search through the password file with the list in list_A and dumping what ever name it matches into the useralias file. But I want to the code to put "Unable to resolve user" into the report if it cannot find the ID in the password file.. I was trying the below but its not working.. Can anyone suggest where I'm going wrong Code:
for i in $(cat $list_A)
do
to_replace=$(grep -w $i ${pwdfile_new} | cut -f2 -d"=")
matchuser=$(grep -wc $i ${pwdfile_new})
echo $matchuser $i
if [[ "${matchuser}" != "1" ]]
then
sed "s/$i/$i =Unable to resolve user}/g" ${useralias} > $file_A
mv $file_A ${useralias}
else
sed "s/${i}/${to_replace}/g" ${useralias} > $file_A
mv $file_A ${useralias}
fi
doneEDIT*** Found the issue... Eventually... Last edited by Jazmania; 06-26-2012 at 11:15 AM.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Useless Use of Cat
Useless Use of grep | awk Also, this loop looks very inefficient. If you show the input you have and the output you want, there's probably much better ways to do it. |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
I'll admit it aint the prettiest piece of code ever but it half works and I'm under pressure to get this script finished for the business.. I've 4 files I'm working with here.. ${grouplist}_temp2 Contains a list of the groups in the report. E.g. OCC root admin printgroup $list_C contains a list of all the secondary user ID's belonging to the Groups in the ${grouplist}_temp2 E.g. x111222333 x111333444 x224422333 $pwdfile_new contains user ID's and the names associated with them ${grouplist}_2 is a file which contains the group names and all the xid's asociated with them. E.g. OCC=x1332223, x4324523, w2324233, x12451213 admin=x2136423, x1332223, w2324233, x4444444, x1212323 printgroup=x12451213 I need to replace the group ID's in the ${grouplist}_2 file with their matched username E.g. x111222333=Joe Bloggs But I also need it to return Name_Not_Found if the xid is not contained in that $pwdfile_new file.. E.g. x224422333=Name_Not_Found Code:
for y in $(cat $list_C)
do
to_replace9=$(grep -w ${y} ${pwdfile_new} | cut -f2 -d"=")
matchgrpusr=$(grep -wc ${y} ${pwdfile_new})
if [[ "${matchgrpusr}" != "1" ]]
then
sed "s/$y/${y}=Name_Not_Found/g" ${grouplist}_2 > $file_B
mv $file_B ${grouplist}_2
else
#### Making sure to replace the second instance of $y if the xid is the same name as the user
matchfirstinst=$(grep -wc $y ${grouplist}_temp2)
if [[ "${matchfirstinst}" == "1" ]]
then
sed "s/$y/${y}=${to_replace9} /2" ${grouplist}_2 > $file_B
mv $file_B ${grouplist}_2
else
sed "s/$y/${y}=${to_replace9} /g" ${grouplist}_2 > $file_B
mv $file_B ${grouplist}_2
fi
fi
doneIf someone can suggest a better way that would be much appreciated... EDIT**** Ok I've changed it around a bit but when it finds a match it should be echoing out $useridvar $to_replace9 but it seems to be echoing out echo $useridvar $matchgrpusr instead... Been looking at this so long now nothings making sense Code:
while read useridvar
do
#to_replace9=$(grep -w ${useridvar} ${pwdfile_new} | cut -f2 -d"=")
matchgrpusr=$(grep -wc ${useridvar} ${pwdfile_new})
if [[ "{$matchgrpusr}" == 1 ]]
then
to_replace9=$(grep -w ${useridvar} ${pwdfile_new} | cut -f2 -d"=")
echo $useridvar $to_replace9
sed "s/${useridvar}/${useridvar}=${to_replace9} /g" ${grouplist}_2 > $file_B
mv $file_B ${grouplist}_2
else
echo $useridvar $matchgrpusr
sed "s/$useridvar/${useridvar}=Name_Not_Found/g" ${grouplist}_2 > $file_B
mv $file_B ${grouplist}_2
fi
done < $list_CLast edited by Jazmania; 06-27-2012 at 07:32 AM.. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| For Loop Question | captaindoogles | Shell Programming and Scripting | 6 | 03-21-2012 07:30 AM |
| For Loop Question | ironhalo | Shell Programming and Scripting | 2 | 09-21-2010 12:18 PM |
| BASH loop inside a loop question | rethink | Shell Programming and Scripting | 4 | 09-15-2010 07:58 AM |
| loop question | aliahsan81 | Shell Programming and Scripting | 5 | 11-21-2008 03:46 PM |
| For Loop Question | msb65 | Shell Programming and Scripting | 4 | 08-27-2008 03:36 AM |
|
|