Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-26-2012
Registered User
 
Join Date: Feb 2007
Location: Ireland
Posts: 111
Thanks: 3
Thanked 0 Times in 0 Posts
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
done


EDIT***

Found the issue... Eventually...

Last edited by Jazmania; 06-26-2012 at 11:15 AM..
Sponsored Links
    #2  
Old 06-26-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,371
Thanks: 490
Thanked 2,534 Times in 2,417 Posts
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  
Old 06-27-2012
Registered User
 
Join Date: Feb 2007
Location: Ireland
Posts: 111
Thanks: 3
Thanked 0 Times in 0 Posts
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
        done

If 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_C


Last edited by Jazmania; 06-27-2012 at 07:32 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 09:49 AM.