How to remove certain lines using sed?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to remove certain lines using sed?
# 1  
Old 05-30-2013
How to remove certain lines using sed?

Hi I have the following kind of line sin my file .

print ' this is first'.

Code:
print ' this is firs and next '
  ' line continuous '. -- this is entire print line.

print ' this is first and next '
' line continuous and'
'still there now over'. -- this 3lines together a single print line.

Now i need to delete such all lines in my file in unix. Plese help me out is there any possible way to do this using sed.

Any help/idea would be a great help.

thanks in advance...

Last edited by Scott; 05-30-2013 at 12:54 PM.. Reason: Please use code tags - and post in the appropriate sub-forum (moved)
# 2  
Old 05-30-2013
Sivajee,

Check this out:
You have to use something like this to delete all the matching lines with the pattern:
Code:
sed '/this is fir/d' file





Code:
# cat -n file
     1  print ' this is firs and next '
     2    ' line continuous '. -- this is entire print line.
     3
     4    print ' this is first and next '
     5    ' line continuous and'
     6    'still there now over'. -- this 3lines together a single print line.


Code:
# cat -n file |sed '/this is fir/d'
     2    ' line continuous '. -- this is entire print line.
     3
     5    ' line continuous and'
     6    'still there now over'. -- this 3lines together a single print line.


Btw, in your code:
Code:
print ' this is first'

, entry not there.
pls provide more input.
# 3  
Old 05-30-2013
Hi,

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

Please let me know how to delete those printf lines using sed in unix.

Thanks in advance..
# 4  
Old 05-30-2013
Please use code tags as required by forum rules!

What OS / shell / system is that? I'd be surprised if you could, in any tool, output 'this is continuous line ' without a print command or a line continuation char in the line before. And, the only solution I could imagine to your problem is to keep a flag for "print command started" and then check for " ' " being the first char in the next line. afaik you can't do that with sed; awk might do.
# 5  
Old 05-31-2013
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:

Code:
$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 05-31-13 at 04:02 AM ---------- Previous update was 05-30-13 at 10:23 PM ----------

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 Smilie
# 6  
Old 05-31-2013
DUPLICATE THREAD of while loop
 
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 remove certain lines using sed?

Hi, I am new to unix and i started some scripting recently. Please go through the following script i wrote. #!/bin/sh file='path../tfile' file1='path../tfile1' rmfile='path../test2' C1=1 C2=1 exec 3< $file1 while read LINE1; do read LINE2 <&3 a=$LINE1 b=`expr $LINE2 - 1` ... (1 Reply)
Discussion started by: Subbu123
1 Replies

2. Shell Programming and Scripting

Locate and remove lines with sed

Gents, I would like to remove some lines from a big file ( file2). The objetive is to remove all the lines in file2 containing a certain string which are in file data2delete.. file data2delete contens: 2573.0 7260.01 2893.0 7255.01 2903.0 7245.01 2897.0 7255.01 2561.0 7255.01... (6 Replies)
Discussion started by: jiam912
6 Replies

3. Shell Programming and Scripting

Remove a range of lines from a file using sed

Hi I am having some issue editing a file in sed. What I want to do is, in a loop pass a variable to a sed command. Sed should then search a file for a line that matches that variable, then remove all lines below until it reaches a line starting with a constant. I have managed to write a... (14 Replies)
Discussion started by: Andy82
14 Replies

4. Shell Programming and Scripting

grep/sed to remove lines in file

Hi, I have a file with values, file1: BELL-1180-1180-81|577:1017| BELL-1180-1180-81|jm10i-auto-stub1/577:102| BELL-1180-1180-81|jm10i-auto-stub1/577:101| BELL-1180-1180-81|jm10i-auto-stub1/577:1700| BELL-1180-1180-81|jm10i-auto-stub1/577:1699| I need to remove the lines which has... (9 Replies)
Discussion started by: giri_luck
9 Replies

5. Shell Programming and Scripting

SED to remove a line above and lines below.

:confused:Hi All, I need help on removing lines in a text file. Sample file : When there is a match ip for IPAddress in my `cat ip.out`, proceed delete line above until string "Comp" is found. Thank you very much. ---------- Post updated at 12:56 AM ---------- Previous update was... (4 Replies)
Discussion started by: chiewming
4 Replies

6. Shell Programming and Scripting

What's the command to remove empty lines with sed?

3 10 20 10 100 100 10000 Output: 3 10 20 10 100 100 10000 ---------- Post updated at 07:59 AM ---------- Previous update was at 07:56 AM ---------- sed '/^$/d' file doesn't work. (8 Replies)
Discussion started by: cola
8 Replies

7. Shell Programming and Scripting

using sed to remove lines

Can somebody explain why my sed command is not working. I do the folloinwg: Generates a binary file to /tmp/x1.out /usr/lib/sa/sa2 -s 4:00 -e 8:00 -i 3600 -A -o /tmp/x1.out decodes the file (no problem so far) sar -f /tmp/x1.out When I do this it does not appear to delete the... (4 Replies)
Discussion started by: BeefStu
4 Replies

8. Shell Programming and Scripting

How to remove lines before and after with awk / sed ?

Hi guys, I need to remove the pattern (ID=180), one line before and four lines after. Thanks. (5 Replies)
Discussion started by: ashimada
5 Replies

9. Shell Programming and Scripting

Using sed to remove lines where field is empty

I was just looking at this post: https://www.unix.com/shell-programming-scripting/22893-delete-multiple-empty-lines.html. and I am looking to achieve the same with sed. So the idea is to delete lines from a file where a certain field has no value. Inputfile: EMID MMDDYY HOURS JOB EMNAME 0241... (4 Replies)
Discussion started by: figaro
4 Replies

10. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies
Login or Register to Ask a Question