Shell deleting specific lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell deleting specific lines
# 1  
Old 10-10-2010
Shell deleting specific lines

Hi,

I'am working under unix solaris

I have a text file with set of lines, each set of lines (BLOCK) have
three fixed lines :
Quote:
FIRSTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SECNDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
THEND
Between SECND line and THEND we have N lines, N differ from a block to another
Quote:
FIRSTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SECNDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LINE1xxxxx
LINE2xxxxxxxxxxxxxxx
...
LINENxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
THEND
FIRSTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SECNDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LINE1xxxxx
LINE2xxxxxxxxxxxxxxx
...
LINENxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
THEND
FIRSTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SECNDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LINE1xxxxx
LINE2xxxxxxxxxxxxxxx
...
LINENxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
THEND
.
.
.
sample :
Quote:
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
7888DUBN998FFFFFFFFFF
9999FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
7333DUBN998FFFFFFFFFF
7333DUBN998FFFFFFFFFF
7333DUBN998FFFFFFFFFF
5688DUBN998FFFFFFFFFF
9999FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000677999
4444DUBN998FFFFUUUF
9999FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDJHF 004444999
7888DUBN998FSSSFFFF
7654DUBN998FSSSFFFF
9999FFDD 88 RFR 88888 0000R YJ
THEND
i have to make a script wich delete each 3 fixed lines if N=0 (no lines between SECND end THEND line) :
delete every set of this three sucessive lines (FIRST SECND THEND)
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
THEND

sample :
before the script i have this text file
Quote:
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
7888DUBN998FFFFFFFFFF
6666FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDJHF 004444999
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
7333DUBN998FFFFFFFFFF
5688DUBN998FFFFFFFFFF
7777FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000677999
4444DUBN998FFFFUUUF
7777FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDJHF 004444999
THEND
after the script i will have :

Quote:
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
7888DUBN998FFFFFFFFFF
6666FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
7333DUBN998FFFFFFFFFF
5688DUBN998FFFFFFFFFF
7777FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000677999
4444DUBN998FFFFUUUF
7777FFDD 88 RFR 88888 0000R YJ
THEND
thanks a lot for your help

Last edited by salbanito; 10-10-2010 at 06:37 PM..
# 2  
Old 10-10-2010
May not be the most 'compact' code, but it will work:

Code:
#!/usr/bin/env ksh

awk '
        /^FIRST/ {         # hold first/second lines until we have a "body"
                first = $0;
                snarf = 0;
                next;
        }
        /^SECND/ {
                second = $0;
                next;
        }

        /^THEND/ {
                if( snarf )        # if we printed a body, safe to print last line
                        printf( "%s\n", $0 );
                snarf = 0;
                next;
        }

        {
                if( ! snarf )         # print first/second lines on first line of body
                        printf( "%s\n%s\n", first, second );
                printf( "%s\n", $0 );

                snarf = 1;
        }
' <input-file


Last edited by agama; 10-10-2010 at 07:09 PM.. Reason: typo
# 3  
Old 10-10-2010
Simple code with little bug.

Code:
awk 'BEGIN{RS=ORS="FIRST";FS="\n"} NF>4' infile

0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
7888DUBN998FFFFFFFFFF
6666FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000999999
7333DUBN998FFFFFFFFFF
5688DUBN998FFFFFFFFFF
7777FFDD 88 RFR 88888 0000R YJ
THEND
FIRST0873HGGD EE 889
SECNDKIIKIE 99099 88 7 DDSFF 000677999
4444DUBN998FFFFUUUF
7777FFDD 88 RFR 88888 0000R YJ
THEND
FIRST

you need manually move the last FIRST to the beginning of the output.

Last edited by rdcwayx; 10-10-2010 at 09:17 PM..
# 4  
Old 10-19-2010
agama and rdcwayx, thanks for your answers, sorry for my late answer

I will test the code of agama, because it hasn't a bug
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vi editor deleting lines with specific pattern

Hi, I need to delete all lines in the file using vi editor which start with word aternqaco. Please assist. aternqaco.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment aternqa.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

2. Shell Programming and Scripting

Help required deleting specific lines from file

Hi, I have a file with 20 columns of data and hundreds of lines of the same format. Here is an example line. The data repeats underneath with the same format. 15 1 4 GLY - 1 65 LYSH 23 N - 24 H - 634 O 0.188 157.552 487 48.70I have been sorting this data by hand but I was wondering if I... (3 Replies)
Discussion started by: livbaddeley
3 Replies

3. Shell Programming and Scripting

selecting and deleting specific lines with condition

I have a set of data as below: The first field, $1 represent "|". The $3 (3rd field) and $6 (6th field) in my data file represent "number-molecule" which has arrangement as below: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ... (9 Replies)
Discussion started by: vjramana
9 Replies

4. Shell Programming and Scripting

Deleting specific lines in a file

Hello, I have a file filled with dates, such as: 04-08-2011 message 04-08-2011 message 03-08-2011 message 01-08-2011 message 31-07-2011 message 24-07-2011 message 15-07-2011 message 13-12-2008 message 26-11-2007 message And I want to delete those lines whose date is older than 10... (5 Replies)
Discussion started by: asanchez
5 Replies

5. Shell Programming and Scripting

deleting specific lines in a file

Hello, I have a file like: 26-07-2011 sunz02 message1 26-07-2011 sunz02 message2 26-07-2011 sunz02 message3 15-07-2011 sunz02 message1 15-07-2011 sunz02 message2 15-07-2011 sunz02 message3... (5 Replies)
Discussion started by: asanchez
5 Replies

6. Shell Programming and Scripting

Deleting specific lines in a file

Hello, I have a file like this one: 03-07-2011 sunz02 message1 03-07-2011 sunz02 message2 03-07-2011 sunz02 message3 01-07-2011 sunz02 message1 01-07-2011 sunz02 message2 01-07-2011 sunz02 ... (1 Reply)
Discussion started by: asanchez
1 Replies

7. Shell Programming and Scripting

deleting specific lines in a file

I want to delete all lines from a file (orig_file) that contain the regex values (bad_inv_list) I tried a for each loop with sed but it isn't working for file in `cat bad_inv_list`; do sed '/$file/d' orig_file > pared_down_file.1 mv pared_down_file.1 orig_file done I've added... (2 Replies)
Discussion started by: verge
2 Replies

8. Shell Programming and Scripting

awk : deleting specific incorrect lines

Hello friends, I searched in forums for similar threads but what I want is to have a single awk code to perform followings; I have a big log file going like this; ... 7450494 1724465 -47 003A98B710C0 7450492 1724461 -69 003A98B710C0 7450488 1724459 001DA1915B70 trafo_14:3 7450482... (5 Replies)
Discussion started by: enes71
5 Replies

9. UNIX for Dummies Questions & Answers

deleting specific lines from all files in a directory

I have a directory full of text data files. Unfortunately I need to get rid of the 7th and 8th line from them all so that I can input them into a GIS application. I've used an awk script to do one at a time but due to the sheer number of files I need some kind of loop mechanism to automate... (3 Replies)
Discussion started by: vrms
3 Replies

10. Shell Programming and Scripting

Deleting specific lines in a file

I have a file which has some lines starting with a particular word. I would like to delete 5 lines before each such line containing that particular word. eg: line1 line2 line3 line4 line5 line6 "particular word"... I would like to delete line2-line6 and all such occurences in that... (4 Replies)
Discussion started by: ramu_1980
4 Replies
Login or Register to Ask a Question