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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to read 3 files and replace with required string 1

Status
Not open for further replies.

AIXdream

Technical User
Mar 27, 2007
33
US
Hi Im looking for script that will do following:
read file1
Check file 2 && file 3
if <item> found in file1=file2 replace file1 text with instuction1{/abc/123/222/zxy/ my name/abc/Unix}
if item found in file1=file3 replace file1 text with instruction2{/abc/123/333/xyz/my name/abc/Unix}

I was able to repace string in the file but cant replace with {/abc/123/333/xyz/my name/abc/Unix}
Regards
 
Can you post your code so far, it will make your requirements clearer and we can probably identify where you're going wrong. Some sample contents of the files and the expected results would help.

Annihilannic.
 
Hi,

Iam into learning process of shell scripting, I need script to replace gecos field in /etc/passwd
im trying to read 2 files list1 and list2(list of userIDs) and compare with $1 field of /etc/passwd,if contents of list1 matches the /etc/passwd then and run chuser command to replace gecos field to FunctionlistValue else set ServiceListValue else notfound perform manual

I have figured out something like this:
--------------------
#!/bin/sh
Functionlist="code here"
servicelist="Code here"
if [cat /tmp/list1 $1= cat /etc/passwd $1]
then
echo "Found $1 in list1\\n"
/usr/bin/chuser -R $1 gecos=$Functionlist
fi

if [cat /tmp/list2 $1= cat /etc/passwd $1]
then
echo"Found $1 in list2\\n"
/usr/bin/chuser -R $1 gecos=$servicelist

else
echo "User ID not found Please implement change manually \\n"
exit

please correct me, i havent tested this script.
 
This line doesn't make any sense:

Code:
 if [cat /tmp/list1 $1= cat /etc/passwd $1]

You can't compare the entire contents of two files just using one line like that, you will need to use some kind of "for" or "while" loop to iterate through the contents of the file(s) and do the comparison for each line (unless you use an external command to do it).

Code:
# for every user in /tmp/list1
for user in $(cat /tmp/list1)
do
        # if that user exists in /etc/passwd
        if grep "^${user}:" /etc/passwd
        then
                echo "Found ${user} in /etc/passwd"
                # update the gecos
                /usr/bin/chuser -R ${user} gecos="$Functionlist"
        fi
done

Annihilannic.
 
Thanks for the correcting and helping, it worked great!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top