Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I have written a script which scans two files and it replaces a
field in the second file with the field present in first file if the same record is present in both the file. Following is the script
cat first |
while read line
do
usr1=`cut -d ":" -f1 line`
pass1=`cut -d ":" -f2 line`
cat sec |
while read line1
do
usr2=`cut -d ":" -f1 line1`
pass2=`cut -d ":" -f2 line1`
if [ $usr1 = $usr2 ]
then
sed -e s/$pass2/$pass1/ line1
break
fi
done
done
But when I execute this script it gives me below error
$ sh scan.sh
cut: Cannot open line.
cut: Cannot open line.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line.
cut: Cannot open line.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line.
cut: Cannot open line.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line1.
$
Cut looks for a filename to read from (or accepts a stream from a pipe). In your case, you are attempting to read data from a variable. Cut is looking for a file named "line" and finds none.
Also, I think you are using the read command incorrectly. I tried this: cat filename | read line #this executed with no errors echo $line #the variable was empty
I don't know what the first line did, but it clearly did not put data into the variable "line".
I think "read" is used to take data from the terminal (or more correctly, standard input)
This seems like a homework problem, but I'll give you a clue anyway.
The error message "cut: Cannot open line1." implies that the program cut is trying to open a file called line1, which I don't believe is what you want.
If you do this at the command line:
Code:
man cut
you'll see why cut is trying to open file line1.
bhandu, you have on occasion posted scripts here. They will be more readable (more accurately) if you click on the [B]Go Advanced[B] button at the bottom of the editing window, enter your question (including the script), highlight the script by dragging your mouse, and then clicking on the # button at the top of the editing window.
I couldn't help thinking about this, maybe I like homework! You could have a look at this,
Code:
#!/bin/bash
#We assume that the files are strictly formatted, no spaces, empty lines, etc
#use temp files just while we're testing, when we're confident that the script works, use real filenames
cp sec new-sec
#creates an array of passwords from the first file
pass1=(`cut -d ":" -f2 first`)
#the index for the array
cnt=0
#Let's loop over the username entries in the first file
#You don't have to know the contents of file sec, only sedit it
#where it fits a given pattern
for usr1 in `cut -d ":" -f1 first` ; do
#If a line in the sec file starts with this username,
#replace the password no matter what it was before
#It will also keep lines with users that are not in first
sed -e "/^$usr1:/ s/:.*$/:${pass1[$cnt]}/" new-sec > tmp-sec
#make sure the result is saved in sec file
mv tmp-sec new-sec
#increase counter
cnt=$(($cnt+1))
done
#And just to verify
echo ========Original sec file =============
cat sec
echo ========New sec file =============
cat new-sec
And with comments and temp file handling removed it becomes
Code:
#!/bin/bash
pass1=(`cut -d ":" -f2 first`)
cnt=0
for usr1 in `cut -d ":" -f1 first` ; do
sed -e "/^$usr1:/ s/:.*$/:${pass1[$cnt]}/" sec > tmp-sec
mv tmp-sec sec
cnt=$(($cnt+1))
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.