delete just the line above


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete just the line above
# 1  
Old 11-25-2011
delete just the line above

Hey guys,

I want to delete all of the lines that are before pattern1 and used this command

Code:
sed '/pattern1/,1d;p'

but it is very strange and doing some duplicate line, I've googled and other commands are not working, do you have any idea about that?
# 2  
Old 11-25-2011
Hi Johanni,

Try:
Code:
$ sed -n '/pattern/,$ p' infile

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 11-25-2011
not working Smilie
# 4  
Old 11-25-2011
Paste part of your input file and your sed command. It will be helpful to see the reason.

Regards,
Birei
# 5  
Old 11-25-2011
Code:
 sed '1{h;d}; /pattern/{x;d}; /pattern/!{x} ; $x' inputfile

EDIT: oops, i misread. I thought you wanted to delete only one line before pattern

Here:
Code:
sed '1,/pattern/d' inputfile

will delete all lines up to pattern. But bireis solution works also. If it is not working, your regex pattern may be wrong.
# 6  
Old 11-25-2011
input
Code:
   RDS radio  
feature
   Yes  
   Visual Radio  
feature
   No  
   Supported audio formats  
feature
   spEC  spEC  espEC  spEC  spEC  spEC  
   Supported image files  
feature
   spEC  
   Supported video files  
feature
   spEC  spEC  spEC  spEC  spEC  computer
   Design  
   Colour  
feature
   spEC  spEC  spEC  spEC design

I want to delete the lines above the lines that have "spEC", so the desired output is:
Code:
   RDS radio  
feature
   Yes  
   Visual Radio  
feature
   No  
   Supported audio formats  

   spEC  spEC  espEC  spEC  spEC  spEC  
   Supported image files  

   spEC  
   Supported video files  

   spEC  spEC  spEC  spEC  spEC  computer
   Design  
   Colour  

   spEC  spEC  spEC  spEC design

or either:
Code:
   RDS radio  
feature
   Yes  
   Visual Radio  
feature
   No  
   Supported audio formats  
   spEC  spEC  espEC  spEC  spEC  spEC  
   Supported image files  
   spEC  
   Supported video files  
   spEC  spEC  spEC  spEC  spEC  computer
   Design  
   Colour  
   spEC  spEC  spEC  spEC design

# 7  
Old 11-25-2011
I missunderstood your first question. Try:
Code:
$ cat infile 
   RDS radio  
feature
   Yes  
   Visual Radio  
feature
   No  
   Supported audio formats  
feature
   spEC  spEC  espEC  spEC  spEC  spEC  
   Supported image files  
feature
   spEC  
   Supported video files  
feature
   spEC  spEC  spEC  spEC  spEC  computer
   Design  
   Colour  
feature
   spEC  spEC  spEC  spEC design
$ sed -ne '$ { p; q }; $! N; /\n.*spec.*$/I D; P; D' infile  
   RDS radio  
feature
   Yes  
   Visual Radio  
feature
   No  
   Supported audio formats  
   spEC  spEC  espEC  spEC  spEC  spEC  
   Supported image files  
   spEC  
   Supported video files  
   spEC  spEC  spEC  spEC  spEC  computer
   Design  
   Colour  
   spEC  spEC  spEC  spEC design

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Mismatched free() / delete / delete [] line no missing

Could you tell me the possibilities of the reason to get the Mismatched free() / delete / delete . I unable to see the line no in the valgrind report. it displays the function name. with that function name, I am not able to find where exactly the issue is there.I am getting the Mismatched free()... (3 Replies)
Discussion started by: SA_Palani
3 Replies

2. UNIX for Dummies Questions & Answers

Script to delete files line by line in .txt document

Friends, I am in need to delete files from a directory on a regular basis. I want to place all the files to be deleted in delete .txt file and require a script to delete files, line by line from this delete.txt file. Please help me with this script as am new to unix scripting. (3 Replies)
Discussion started by: fop4658
3 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

6. Shell Programming and Scripting

Find specific line and delete line after.

I'm looking for a way to search a file, in this case init.rc for a specific match, service adbd /sbin/adbd, and delete the a specific line after it Original: # adbd is controlled by the persist.service.adb.enable system property service adbd /sbin/adbd disabled After deletion: #... (5 Replies)
Discussion started by: GabrialDestruir
5 Replies

7. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

8. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies

9. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies

10. UNIX for Dummies Questions & Answers

how to delete line with matching text and line immediately after

hello experts, I have a file: File1 Sample Test1 This is a Test Sample Test2 Another Test Final Test3 A final Test I can use sed to delete the line with specific text ie: sed '/Test2/d' File1.txt > File2.txt How can I delete the line with the matching text and the line immediately... (6 Replies)
Discussion started by: orahi001
6 Replies
Login or Register to Ask a Question