Sponsored Content
Full Discussion: If with sed loop question
Top Forums Shell Programming and Scripting If with sed loop question Post 302662801 by Jazmania on Wednesday 27th of June 2012 06:26:49 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GSHADOW(5)                                                 File Formats and Conversions                                                 GSHADOW(5)

NAME
gshadow - shadowed group file DESCRIPTION
/etc/gshadow contains the shadowed information for group accounts. This file must not be readable by regular users if password security is to be maintained. Each line of this file contains the following colon-separated fields: group name It must be a valid group name, which exist on the system. encrypted password Refer to crypt(3) for details on how this string is interpreted. If the password field contains some string that is not a valid result of crypt(3), for instance ! or *, users will not be able to use a unix password to access the group (but group members do not need the password). The password is used when a user who is not a member of the group wants to gain the permissions of this group (see newgrp(1)). This field may be empty, in which case only the group members can gain the group permissions. A password field which starts with an exclamation mark means that the password is locked. The remaining characters on the line represent the password field before the password was locked. This password supersedes any password specified in /etc/group. administrators It must be a comma-separated list of user names. Administrators can change the password or the members of the group. Administrators also have the same permissions as the members (see below). members It must be a comma-separated list of user names. Members can access the group without being prompted for a password. You should use the same list of users as in /etc/group. FILES
/etc/group Group account information. /etc/gshadow Secure group account information. SEE ALSO
gpasswd(5), group(5), grpck(8), grpconv(8), newgrp(1). shadow-utils 4.5 01/25/2018 GSHADOW(5)
All times are GMT -4. The time now is 05:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy