script to edit strings based on patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to edit strings based on patterns
# 1  
Old 03-21-2009
script to edit strings based on patterns

Hello All,

Here is the file which I want to edit. The script should look for DB2 and if found then delete all lines related to DB2 connection string. Is there way this can be done using script ?

DB1 =
(DESCRIPTION =
(SDU = 32768
(enable = broken)
(ADDRESS = (PROTOCOL = TCP)(HOST = myserver.com)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = myserver.com)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME = DB1)
)
)
DB2 =
(DESCRIPTION =
(SDU = 32768)
(enable = broken)
(ADDRESS = (PROTOCOL = TCP)(HOST = anotherserver.com)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = anotherserver.com)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME = DB2)
)
)
DB3 =
(DESCRIPTION =
(SDU = 32768)
(enable = broken)
(ADDRESS = (PROTOCOL = TCP)(HOST = thirdserver.com)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = thirdserver.com)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME = DB3)
)
)
# 2  
Old 03-21-2009
The code:
Code:
sed '/^DB2/,/DB2/ s/^/#/' < inputfile > outputfile && \
cp outputfile inputfile

Would comment out all the DB2 lines but would leave the "(" at the start and ")" at the end untouched, would that be any use?

Code:
sed '/^DB2/,/DB2/ d' < inputfile > outputfile && \
cp outputfile inputfile

Would delete all the DB2 lines but would leave the "(" at the start and ")" at the end untouched, would that be any use?
# 3  
Old 03-21-2009
Are the entries always10 lines long? If they are, it's easy:
Code:
sed -i '/^DB2/,+9d' file

The -i switch edits in-place. But not all versions of sed support it, so if yours doesn't, you can do it the way TonyFullerMalv did.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux/Shell script - How to compare 2 arrays based on patterns and get the differences

I have FILE 1 (This file has all master columns/headers) A|B|C|D|E|F|G|H|STATUS FILE 2 A|C|F|I|OFF_STATUS 3|4|5|4|Y 6|7|8|5|Y Below command give me all headers of FILE 2 into array2.txt file paste <(head -1 FILE2.txt | tr '|' '\n')>array2.txt So I would like to compare... (2 Replies)
Discussion started by: jmadhams
2 Replies

2. Shell Programming and Scripting

Extract multiple occurance of strings between 2 patterns

I need to extract multiple occurance strings between 2 different patterns in given line. For e.g. in below as input ------------------------------------------------------------------------------------- mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)... (8 Replies)
Discussion started by: sameermohite
8 Replies

3. Shell Programming and Scripting

awk based script to ignore all columns from a file which contains character strings

Hello All, I have a .CSV file where I expect all numeric data in all the columns other than column headers. But sometimes I get the files (result of statistics computation by other persons) like below( sample data) SNO,Data1,Data2,Data3 1,2,3,4 2,3,4,SOME STRING 3,4,Inf,5 4,5,4,4 I... (9 Replies)
Discussion started by: ks_reddy
9 Replies

4. Shell Programming and Scripting

Concatenate text between patterns in individual strings

In any given file, wherever a certain data block exists I need to concatenate the values(text after each "=" sign) from that block. in that block. The block starts and ends with specific pattern, say BEGIN DS and END DS respectively. The block size may vary. A file will have multiple such blocks.... (12 Replies)
Discussion started by: Prev
12 Replies

5. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

6. Shell Programming and Scripting

shell script to format file based on specific patterns

Please help me out and drag me out the deadlock I am stuck into: I have a file. I want the statements under a if...then condition be listed in a separate file in the manner condition|statement.Following are the different input pattern and corresponding output parameters.any generic code to... (7 Replies)
Discussion started by: joyan321
7 Replies

7. Shell Programming and Scripting

patterns between strings...

I have a small requirement How to get patterns between string this way Input.. 05 ABC. TAGTAG 10 AAA PIC 9 10 BBB PIC X COMMET 10 CCC COMP PIC 9 05 DEF I wanted to get all the variable between 05 ABC and next 05 level out put should be AAA BBB (6 Replies)
Discussion started by: pbsrinivas
6 Replies
Login or Register to Ask a Question