sed - pattern match - apply substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - pattern match - apply substitution
# 8  
Old 09-13-2016
I think i got it, but i ask thread-o/p to confirm. The presentation of his problem was perhaps a bit off from optimal:

There is ONE file with some database statements:

Code:
CREATE OR REPLACE VIEW DB_V.TAB1 AS SELECT * FROM DB_T.TAB1;
....
CREATE OR REPLACE VIEW DB_V.TAB10 AS SELECT * FROM DB_T.TAB10;
CREATE INDEX TAB1_COL1 ON TAB1 (COL1);
...
CREATE INDEX TAB6_COL1 ON TAB6 (COL1);
CREATE INDEX TAB7_COL1 ON TAB7 (COL1)
...
CREATE INDEX TAB10_COL1 ON TAB10 (COL1);

Now, some of these statements are already processed, others are not. Which are processed is in a second file (the "spool file":

Code:
TAB1_COL1
TAB2_COL1
TAB3_COL1
TAB4_COL1
TAB5_COL1
TAB6_COL1

Thread-o/p wants to be able to restart the (first) command file after it has been stopped and for this (to avoid already done things a second time) comment out all already run commands. This is done by adding a "--" at the beginning of the line. What he was trying to do was to read the spool file line by line and for each line prepend all lines containing the table (?, index?) in question with comment signs.

@Thread-o/p: please tell us if this is correct. If yes, your script was "almost correct", but to prepend a whole line with something you could have done easier:

Code:
while read ITEM ; do
     sed '/'"$ITEM"'/ s/^/-- /' /your/command/file > /your/command/file.tmp
     mv /your/command/file.tmp /your/command/file
done < /path/to/spool/file

I hope this helps.

bakunin

PS: on second thoughts, Dons concerns of course still apply! You have to prevent i.e. "COL1" to trigger changes in lines containing "COL19". Maybe adding a space after "$ITEM" like this
Code:
while read ITEM ; do
     sed '/'"$ITEM"' / s/^/-- /' /your/command/file > /your/command/file.tmp
     mv /your/command/file.tmp /your/command/file
done < /path/to/spool/file

would suffice but to decide that you will have to analyse the command file you are trying to change.

Last edited by bakunin; 09-13-2016 at 03:45 AM.. Reason: afterthoughts
This User Gave Thanks to bakunin For This Post:
# 9  
Old 09-14-2016
Thank you Ravinder Singh, RudiC, Don Cragun, Bakunin for your inputs. Have used the RudiC's suggestion along with Don's excellent considerations. The error I was facing for
Code:
sed "/$line/ s/^/--/g"

was
Code:
sed: Function TAB1_COL1 s/^/--/g cannot be parsed /

However, when in command prompt when I assign line=TAB1_COL1 and then sed "/$line/ s/^/--/g" gives me the correct result. However, used the awk solution for now. Bakunin, yes, you summarized accurately my requirement. Will give a try of your suggestion
Code:
sed '/'"$ITEM"'/ s/^/-- /'

# 10  
Old 09-14-2016
Quote:
Originally Posted by chill3chee
Thank you Ravinder Singh, RudiC, Don Cragun, Bakunin for your inputs. Have used the RudiC's suggestion along with Don's excellent considerations. The error I was facing for
Code:
sed "/$line/ s/^/--/g"

was
Code:
sed: Function TAB1_COL1 s/^/--/g cannot be parsed /

However, when in command prompt when I assign line=TAB1_COL1 and then sed "/$line/ s/^/--/g" gives me the correct result. However, used the awk solution for now. Bakunin, yes, you summarized accurately my requirement. Will give a try of your suggestion
Code:
sed '/'"$ITEM"'/ s/^/-- /'

There is no logical difference between the commands:
Code:
sed "/$line/ s/^/--/g"

and:
Code:
sed '/'"$line"'/ s/^/-- /'

The diagnostic message:
Code:
sed: Function TAB1_COL1 s/^/--/g cannot be parsed /

would be more likely to occur if the slashes around the pattern had been dropped... I.e.:
Code:
sed "$line s/^/--/g"

instead of:
Code:
Code:
sed "/$line/ s/^/--/g"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

2. Shell Programming and Scripting

Pattern match with awk/sed - help

I need to grep for the pattern text inside the square brackets which are in red and not in green..my current code greps patterns both of them, which i don't want Input fileref|XP_002371341.1| oxoacyl-ACP reductase, putative gb|EPT24759.1| 3-ketoacyl-(acyl-carrier-protein) reductase ... (2 Replies)
Discussion started by: selvankj
2 Replies

3. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

4. UNIX for Dummies Questions & Answers

sed multiline pattern match

How can I write a script that takes a cisco config file and outputs every occurrence of two, or more, pattern matches through the whole config file? For example, out of a config file, i want to print out every line with interface, description and ip address through the whole file, and disregard... (3 Replies)
Discussion started by: knownasthatguy
3 Replies

5. Shell Programming and Scripting

Sed Pattern Match

Hi, I would like to use SED to do the following string replacement: asd1abc to www1cda asd2abc to www2cda ... asd9abc to www9cda I can use 'asd.abc' to find the orignal string, however I don't know how to generate the target string. Any suggestion? Thanks, ... (2 Replies)
Discussion started by: mail4mz
2 Replies

6. Shell Programming and Scripting

Match a pattern starting with sub-pattern using sed

Hi all, I've been experiencing a difficulty trying to match a number and write it to a new file. My input file is: input.txt It contains the lines: 103P 123587.256971 3.21472112 3.1517423 1.05897234566427 58.2146258 12.35478 25.3612489 What would be the sed command to... (17 Replies)
Discussion started by: Biederman
17 Replies

7. Shell Programming and Scripting

sed pattern match problem

Hi all, hoping this is a simple one, tried looking but just can't see the solution As an example I've got a list of words that all start Ben..... Bendicks Benefiber Ben Benylin I need to only change the line Ben with Ben 10, ignoring the other lines. I tried the following ... (1 Reply)
Discussion started by: mrpugster
1 Replies

8. Shell Programming and Scripting

sed pattern substitution issue?

Hello everyone ... I'm going crazy, I hope some of you can help me ... I have to replace a line in a crontab like this: 5 2 * * 2 root backupdat with this: 5 5 * * 3 root backupdat the command I use is the following: sed -i.bak -e 's/5 2 * * 2 root backupdat/5 5 * * 3 root... (4 Replies)
Discussion started by: ionral
4 Replies

9. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

10. Shell Programming and Scripting

pattern match and substitution, can you help?

pattern match and substitution, can you help? file named test.txt I want to replace all the words Event with the word Fatal in all lines containing the word ERR - but I also want to keep the output of the other lines not matching ERR Test.txt: Event 13 INF egegegege Event 14 INF... (4 Replies)
Discussion started by: frustrated1
4 Replies
Login or Register to Ask a Question