Delete lines that contain a pattern from specific line to the end.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines that contain a pattern from specific line to the end.
# 1  
Old 08-25-2014
Delete lines that contain a pattern from specific line to the end.

Gents,

I am trying to delete all lines which start with "H" character, but keeping the fist header. Example In the input file I will delete all lines starting from line 8 which contents character "H" to the end of the file.

I try

Code:
sed '8,10000{/^H/d;}' file

But as don't know the end of the file I would like to get something more efficient.

Input file
Code:
H26 14    Day of year               72-74  I3      1-999          None    -     
H26 15    Time hhmmss               75-80  3I2     000000-235959  None    -     
H26                                                                             
H26                                                                             
H26      1         2         3         4         5         6         7         8
H26 5678901234567890123456789012345678901234567890123456789012345678901234567890
H26                                                                             
R2856.0            6985.01p1                   404100.0 2576300.0 164.6305 53400
R2856.0            6986.01p1                   404150.0 2576300.0 164.5305 53230
R2856.0            6987.01p1                   404200.1 2576300.0 164.5305 53230
R2856.0            6988.01p1                   404250.0 2576300.0 163.9305 53230
H26 14    Day of year               72-74  I3      1-999          None    -     
H26 15    Time hhmmss               75-80  3I2     000000-235959  None    -     
H26                                                                             
H26                                                                             
H26      1         2         3         4         5         6         7         8
H26 5678901234567890123456789012345678901234567890123456789012345678901234567890
H26
R2856.0            7017.01p1                   405700.0 2576300.1 163.7305 43154
R2856.0            7018.01p1                   405750.0 2576300.0 163.6305 43154
R2856.0            7019.01p1                   405800.0 2576300.0 163.5305 43154
H26 14    Day of year               72-74  I3      1-999          None    -     
H26 15    Time hhmmss               75-80  3I2     000000-235959  None    -     
H26                                                                             
H26                                                                             
H26      1         2         3         4         5         6         7         8
H26 5678901234567890123456789012345678901234567890123456789012345678901234567890
H26

output file
Code:
H26 14    Day of year               72-74  I3      1-999          None    -     
H26 15    Time hhmmss               75-80  3I2     000000-235959  None    -     
H26                                                                             
H26                                                                             
H26      1         2         3         4         5         6         7         8
H26 5678901234567890123456789012345678901234567890123456789012345678901234567890
H26                                                                             
R2856.0            6985.01p1                   404100.0 2576300.0 164.6305 53400
R2856.0            6986.01p1                   404150.0 2576300.0 164.5305 53230
R2856.0            6987.01p1                   404200.1 2576300.0 164.5305 53230
R2856.0            6988.01p1                   404250.0 2576300.0 163.9305 53230
R2856.0            7017.01p1                   405700.0 2576300.1 163.7305 43154
R2856.0            7018.01p1                   405750.0 2576300.0 163.6305 43154
R2856.0            7019.01p1                   405800.0 2576300.0 163.5305 43154

Thanks for your help
# 2  
Old 08-25-2014
To define the range until the end of your inputfile use the dollar sign:
Code:
sed '8,${/^H/d;}' /path/to/your/file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Delete lines above and below specific line of text

I'm trying to remove a specific number of lines, above and below a specific line of text, highlighted in red: <STMTTRN> <TRNTYPE>CREDIT <DTPOSTED>20151205000001 <TRNAMT>10 <FITID>667800001 <CHECKNUM>667800001 <MEMO>BALANCE </STMTTRN> <STMTTRN> <TRNTYPE>DEBIT <DTPOSTED>20151207000001... (8 Replies)
Discussion started by: bomsom
8 Replies

3. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

4. UNIX for Dummies Questions & Answers

How to delete specific lines (2n+3 line, n=0,1,2...296) in a file?

Dear everyone, I have a file with 900 lines (there is only numbers in one line, no string), I only need the lines 2+3n (n=0,1...296), i.e line 2, 5, 8, 11...888. I tried google but only the results such as how to delete all the odd lines or all the even lines with 'awk' command. Thanks in... (4 Replies)
Discussion started by: phamnu
4 Replies

5. Shell Programming and Scripting

Delete multiple lines starting with a specific pattern

Hi, just tried some script, awk, sed for the last 2 hours and now need help. Let's say I have a huge file of 800,000 lines like this : It's a tedious job to look through it, I'd like to remove those useless lines in it as there's a few thousands : Or to be even more precise : if line1 =... (6 Replies)
Discussion started by: Zurd
6 Replies

6. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

7. Shell Programming and Scripting

Delete all lines after a specific line ?

Hello. My file is like this: a b c d e f g h i I want to delete all lines after the 3rd line, means after the "c". Is there any way to do this? The lines differ between them and the lines I want to delete does not have a specific word, or the lines I want to keep (a,b,c) does not have a... (4 Replies)
Discussion started by: hakermania
4 Replies

8. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

9. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

10. Shell Programming and Scripting

check position of end of line for some specific lines

-------------------------------------------------------------------------------- Have to check in a file that the lines starting with 620 and 705 are ending at same posiotin. 82012345 62023232323 70523949558 62023255454 9999 In the above lines, i have to check the lines starting... (1 Reply)
Discussion started by: senthil_is
1 Replies
Login or Register to Ask a Question