Sed or Awk to remove specific lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed or Awk to remove specific lines
# 1  
Old 05-08-2009
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 constraint "informix".fk_esc_acct_from_fi);
EOF

I want to use sed or awk to remove the 'alter table' statement which in this example is two lines, it can be three or even four lines. I know how to remove one line with sed but I do not know how to tell sed "remove the line with the words 'foreign key' and continue removing lines up to and including the first ';' that you run into". These lines do not always appear at the end of the file. I am using KSH on a Solaris 10 box.

Thanks for any help,

Mike
# 2  
Old 05-08-2009
Try this:

Code:
 awk '/alter table/{f=1}/;$/{f=0;next}!f' file

# 3  
Old 05-08-2009
Try this:

Code:
sed '/alter[^;]*;/s///g;/alter[^;]*/{N;s/alter[^;]*\n[^;]*;//g}' file

cheers,
Devaraj Takhellambam
# 4  
Old 05-08-2009
I should have included a better example - sorry - here is the real file:

create table "informix".esc_acct
(
fi_id decimal(9,0) not null ,
ea_nbr char(14) not null ,
rc_svcg_st_cd char(2) not null ,
ea_type_nm char(8) not null ,
ea_closd_dt date,
primary key (fi_id,ea_nbr) constraint "informix".pk_esc_acct
) extent size 12 next size 8 lock mode page ;
revoke all on "informix".esc_acct from "public" as "informix";

alter table "informix".esc_acct add constraint (foreign key (fi_id)
references "informix".fi constraint "informix".fk_esc_acct_from_fi);

When I ran Franklin's script I got:

nawk '/alter table/{f=1}/;$/{f=0;next}!f' esc_acct_resize.sql

{ TABLE "informix".esc_acct row size = 34 number of columns = 5 index size = 36 }
create table "informix".esc_acct
(
fi_id decimal(9,0) not null ,
ea_nbr char(14) not null ,
rc_svcg_st_cd char(2) not null ,
ea_type_nm char(8) not null ,
ea_closd_dt date,
primary key (fi_id,ea_nbr) constraint "informix".pk_esc_acct


So it trunc'd off too much... I am messing with the cmds that Franklin sent me.

Thanks,

Mike
# 5  
Old 05-08-2009
Ok, I have adjust the code, I didn't expect other lines with a semicolon at the end:

Code:
awk '/alter table/{f=1} /;$/ && f{f=0;next} !f'  file

# 6  
Old 05-08-2009
Thanks!

Beautiful Franklin - works very nicely. I will now set about deconstructing the lines to figure them out for future application.

MUCH APPRECIATED!!!!

Mike
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

sed or awk to remove specific column to one range

I need to remove specific column to one range source file 3 1 000123456 2 2 000123569 3 3 000123564 12 000123156 15 000125648 128 000125648 Output required 3 000123456 2 000123569 3 000123564 12 000123156 15 000125648 128 000125648 (6 Replies)
Discussion started by: ranjancom2000
6 Replies

3. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

5. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

7. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

8. Shell Programming and Scripting

Selecting specific 'id's from lines and columns using 'SED' or 'AWK'

Hello experts, I am new to this group and to 'SED' and 'AWK'. I have data (text file) with 5 columns (C_1-5) and 100s of lines (only 10 lines are shown below as an example). I have to find or select only the id numbers (C-1) of specific lines with '90' in the same line (of C_3) AND with '20' in... (6 Replies)
Discussion started by: kamskamu
6 Replies

9. 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

10. Shell Programming and Scripting

how to remove specific lines from a file

When restoring a file in my uninstall program I need to remove the lines I added to a file during the install. In between the file can be modified by the users. Assume file1 is as follow: xxx str2 xxxx ..... ...The Following lines containing str* have to be removed... xxx str1 xxxx xxx ... (17 Replies)
Discussion started by: bluemoon1
17 Replies
Login or Register to Ask a Question