If with sed loop question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If with sed loop question
# 1  
Old 06-26-2012
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 12:15 PM..
# 2  
Old 06-26-2012
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.
# 3  
Old 06-27-2012
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 08:32 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If loop question

Hi I have script if ; then echo "Found Node" else echo "Sorry this is not a node" I need to add conditions "nn" ,"rm", and "hh" in the same line along with "hd" .Can this be done or do I need to use elif (1 Reply)
Discussion started by: Dublin4320
1 Replies

2. Shell Programming and Scripting

While loop Question

Hi, I have a requirement where I have to check for 10 files in Unix location. If all of the files present in that directory then i have execute another process. can you help me resolving this issue. sample location /home/usr abc.txt cde.txt aaaa.txt lll.txt ooo.txt if all the... (3 Replies)
Discussion started by: manasvi24
3 Replies

3. Shell Programming and Scripting

For loop question

Hi, I'm trying to put together a small script that will read a txt file that contains a list of two columns. Each column is the name of a folder.. e.g. AIX Server1 AIX Server2 AIX Server3 $ for i in `cat /opt/apacheprod/scripts/input/copy_list.txt` do PLATFORMVAR=`awk ' { print $1 } '... (7 Replies)
Discussion started by: Jazmania
7 Replies

4. Shell Programming and Scripting

loop question

hey guys what im trying to do is do a simple script that will ask for a password and on the 5th time it says access denied if the right password is still not entered this is what i have so far can anyone help me im not good with scripting thanks in advance #!/bin/bash secretname=secret... (2 Replies)
Discussion started by: randallrivy11
2 Replies

5. Shell Programming and Scripting

For Loop Question

I'm improving the way an existing script handles arrays, but the results aren't what I had in mind: e="Too many consecutive errors... System is probably unstable!" e="Cancelable Timer Wait Failed!" for errcd in ${e} do echo ${errcd} done The for loop interprets the spaces... (2 Replies)
Discussion started by: ironhalo
2 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Loop question ???

Hi I want to take an input untill the user presses 'n' or 'N' and run a command based on the input I want to store the output of the command in a file called output.txt ( append mode ). Please tell the script for the same. Here is the psuedocode while ( n or N pressed ) do read var run... (2 Replies)
Discussion started by: skyineyes
2 Replies

8. Shell Programming and Scripting

For Loop Question

Hi, I am writing a bash shell script. I will have a section of code devoted to a for loop: for FILE in *.hdf *.L2; do echo hello world done The complication is that sometimes there will not be any files with a ".hdf" extension, or sometimes there won't be any with a ".L2" extension. Since... (4 Replies)
Discussion started by: msb65
4 Replies

9. Shell Programming and Scripting

while loop question

while do print What is the next device number to be added to $dgroup? print Press \<Enter\> if there are no more devices to be added. read dev_num export dev_num symld -g $dgroup -sid $sname add dev $dev_num done In this while... (2 Replies)
Discussion started by: stepnkev
2 Replies

10. Shell Programming and Scripting

Loop question

hi, how would i go about making a loop which gets each line from a single text file, set it to a variable and then print it to screen? thanks eg: #!/bin/sh FILE="somefile.txt" text_line="" what kind of loop would use here? (18 Replies)
Discussion started by: strike
18 Replies
Login or Register to Ask a Question