while loop stops after a function...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers while loop stops after a function...
# 1  
Old 02-06-2012
while loop stops after a function...

my ksh script is not working...
i wanna remove lines in file2.txt from file1.txt
Code:
# cat file1.txt
this is line one 
this is line two
this is line three
this is line four
this is line five

# cat file2.txt
this is line two
this is line three

# cat my_script.ksh
#!/bin/ksh

i=1
y=1
myfunc()
{
exec < file1.txt
while read line ; do
file1[i]=$line
if [[ ${file1[i]} = ${arr2[y]} ]] ; then
echo ${file1[i]} and ${file2[y]} are the same.
sed ''$i'd' file1.txt > new.txt
mv new.txt file1.txt
else
echo ${file1[i]} and ${file2[y]} are not the same.
fi
(( i=i+1 ))
done
return
}
exec < file2.txt
while read line2 ; do
file2[y]=$line2
myfunc     # calling the function
(( y=y+1 ))
done
cat file1.txt

the output is:
Code:
this is line one
this is line three
this is line four
this is line five

yes... the problem is while loop stops right after the function ends...

i can't reply anymore.... so i just add more here.

thanx balajesuri!!!
that works! what a simple solution!
however its not really matching line deleting...
what if i have a line "this is line two and extra"
grep -v -f file2.txt file1.txt deletes that line...

Last edited by methyl; 02-06-2012 at 06:27 AM.. Reason: please use code tags
# 2  
Old 02-06-2012
1. What're you trying to achieve?
2. What is the desired output from file1.txt and file2.txt?
# 3  
Old 02-06-2012
i want to delete matching lines in file2.txt from file1.txt
so my desired file1.txt output is
Code:
this is line one
this is line four
this is line five


Last edited by methyl; 02-06-2012 at 06:27 AM..
# 4  
Old 02-06-2012
A simple grep should do:
Code:
grep -v -f file2.txt file1.txt

---------- Post updated at 13:37 ---------- Previous update was at 12:45 ----------

Quote:
Originally Posted by curtis911
i can't reply anymore.... so i just add more here

thanx balajesuri!!!
that works! what a simple solution!
however its not really matching line deleting...
what if i have a line "this is line two and extra"
grep -v -f file2.txt file1.txt deletes that line...
1. You have "this is line two and extra" in which file. file1.txt or file2.txt?
2. And why can't you reply anymore? Please don't edit earlier posts. Other members might not be able to track.
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 02-06-2012
what if i have a line "this is line two and extra" in file1.txt, then your code will delete that line.
i want to remove lines that matching exactly to lines in file2.txt
thanx again
# 6  
Old 02-06-2012
The grep man-page shows you what options you have. In your case this should do:
Code:
grep -vxf file2.txt file1.txt

This User Gave Thanks to cero For This Post:
# 7  
Old 02-06-2012
"grep -vxf file2.txt file2.txt" worked perfectly!!! really appreciate it!!!
but do you see any problems in my script why main loop stops reading in after the myfunc anyways? :-)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass function parameter to do loop?

Hi All, I have created one function for KSH and was running well with one parameter input since I just had to use $1 to get the parameter. Now I want to do loop for each parameter(actually filenames) . I have try to use do loop, but $i does not resolve to parameter instead it resolves to 1,... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

If loop inside function not working.

check_deplver () { dir=/abc/def/ghi if ssh -o StrictHostKeychecking=no $1 "" 2> /dev/null then echo " output is " ssh -o StrictHostKeychecking=no $1 "ls -lrt $dir | grep -i abc" 2> /dev/null else echo " directory not presnt" fi } This is not working. But... (7 Replies)
Discussion started by: NarayanaPrakash
7 Replies

3. Shell Programming and Scripting

while loop stops after first iteration - remote ssh exit command problem?

I have written the following script to update some Debian boxes. #!/bin/bash mxg_hosts_file="/etc/mxg/ssh-hosts" while read line ; do mxg_host="$(echo ${line} | awk -F":" '{print $1}')" mxg_port="$(echo ${line} | awk -F":" '{print $2}')" echo "Connecting and Upgrading... (3 Replies)
Discussion started by: jelloir
3 Replies

4. Shell Programming and Scripting

Script with infinite loop stops after sometime

Hi I am working on a server that is set up and maintained by a third party. It seems whenever I run bash scripts in the background (with a &) with while loops in them they seem to me killed in around 2.5 hours. ( I am running them as a normal user with no special privileges ) . Is there a... (3 Replies)
Discussion started by: pkabali
3 Replies

5. Emergency UNIX and Linux Support

Function and LOOP

Hello, I need help : I've got configurations files in two differents directory. Each configuration files contains some information that I must have in order to send specific command. I do not know how to do it. I belive I need a loop in function but I can't make it work. Extension of... (4 Replies)
Discussion started by: Aswex
4 Replies

6. Shell Programming and Scripting

call a function in for loop

Hi all, I am trying to call a function in for loop in the below format #!/bin/bash abc() { commands } for (( i=0; i<=10; i++ )) do abc done The error i am getting when trying to execute: syntax error: unexpected end of file canany one help me where i am going wrong?? (5 Replies)
Discussion started by: gsr_kashyap
5 Replies

7. Programming

__read_nocancel Function Causes Infinite Loop

Does anyone know what __read_nocancel does and why it would go into an infinite loop? What I have gathered in my searches is that it pertains to server code. Yet, I'm not running this application in server mode. NOTE: There are server functions in the shared object, but the specific code... (3 Replies)
Discussion started by: marcus121
3 Replies

8. Shell Programming and Scripting

ssh stops a while loop

The while loop exits (early) when a simple ssh command is run. #!/bin/ksh #set -x #------------------------------------------------------------------------- # Functions Section #------------------------------------------------------------------------- while : do cat list.txt|while read... (1 Reply)
Discussion started by: bkdias26
1 Replies

9. Shell Programming and Scripting

how to get the similar function in while loop or for loop

Dear all How to write the shell script for the following statement: (C programming) for (i=0;i<30;i++) { if i=1 continue *skip this number (To do function here....) ... } similar statement in while loop.... I wrote the script in sh... (3 Replies)
Discussion started by: trynew
3 Replies

10. UNIX for Dummies Questions & Answers

Menu function stuck in a loop?

I having problem when I call this cleanupmenu function within a script. It continuously loops and goes to selection * That wasn't a valid selection. I have to kill it everytime to stop it. What am I doing wrong. I use this function menu in several other scripts and I don't have this problem at... (2 Replies)
Discussion started by: darthur
2 Replies
Login or Register to Ask a Question