While loop

this is the i/p file i have :

$cat test2
DISPLAY
TWO
THREE
DISPLAY
MEEN
MOVE
DISPLAY

script used :

#!/bin/sh
file='sj/test2'
while read LINE
do
echo $LINE |grep -q "DISPLAY"
if [ "$?" -eq 0 ];then
 sed /$LINE/d $file
fi
done < $file;


i am wondering why this is giving following output :

TWO
THREE
MEEN
MOVE
TWO
THREE
MEEN
MOVE
TWO
THREE
MEEN
MOVE

it's been repeating the lines.. why ??

while read LINE
do
echo $LINE |grep -q "DISPLAY"
if [ "$?" -eq 0 ];then
 sed /$LINE/d $file
fi
done < $file;

This prints the modified contents of the file test2 3 times[number of DISPLAY occurred in your original file] :slight_smile:

Why do this in a loop?? single use of sed will do the trick sed '/DISPLAY/d' $file

Oh, yeah. but i need to this through script only. actually this is my requiremtn which i was posted earlier.

HI,

the shell is bash shell and the system is windows.

i need to delete entire print lines... Like my file is having some printf statements. I need to remove all the printf lines in my file. These printf statements might be in a single line or it might stretch for 2 lines also( in this case these two lines have to be deleted as these two line together a printf statement). say my file is as follows:

$cat testfile.txt
1. pritnf 'this is first line ' .
2. printf ' this is second line '
   'this is continuous line ' .
3. print ' this is 3rd line '
  ' it is still there '
 ' now its over ' .
4. set a = b
5. MOVE A TO B

output :
4. set a = b
5. MOVE A TO B

---------- Post updated at 04:03 AM ---------- Previous update was at 03:51 AM ----------

And is there any way to use grep in this script to reduce the repetations to the minimum. i mean it should not the repeat the output as a result of grep.

Please help me out

lets say that the lines to be deleted are comments!!

All the comments should have a single quote ' . So we can exclude these lines by these commands

sed "/'/d" $file
grep -v "'" $file

Yeah, but there may be another statements which can have these quotes but need not to be deleted. Thats why i am struggling.
Say this is the syntax of the pritn statement.
print ' ----data---'.
in the file this print statement can be in a single line or multiple lines as follows:

single line :
print ' this is single line'.

multiple line:
print ' this is first line '
'this is second line'
'this is third line'. ( These statements can end with either "." or " ' ")

so my actual file is like this :

$cat ipfile
1. print ' this is single line'.
2. print ' this is first line '
'this is second line'
'this is third line'.
3. a='this is variable value'
4.move a to b.
5. set a to 1.


O/p file should be:

3. a='this is variable value'
4.move a to b.
5. set a to 1.

Can there be lines like these??

1. print ' this is single line'.
2. print ' this is first line '
'this is second line'
'this is third line'.
3. a='this is variable value'
c='this is another variable'
4.move a to b.
5. set a to 1.

To correct your script:

...
do
 echo "$LINE" | sed "/DISPLAY/d"
done ...