Visit Our UNIX and Linux User Community


Complex sed replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex sed replacement
# 1  
Old 11-22-2007
Complex sed replacement

Hi,
I have a file that I would like to translate using sed.
I can do some basic sed commands, but Im afraid this level is beyond me.
I have this file -
...alter...
..
...65536...
...
...65536...
...
...alter...
...
...65536...
etc
What I would like to do is replace the first occurrence of the 65536 after each line that has 'alter' in it (there may be serveral lines with 65536 between 'alter' lines) .
Any help would be much appreciated thanks.
Bob
# 2  
Old 11-22-2007
If the input is always like the one you posted:

alter
one record
pattern to replace

Code:
% cat file
...alter...
..
...65536...
...
...65536...
...
...alter...
...
...65536...
% sed '/alter/{N;N;s/65536/whatever/;}' file
...alter...
..
...whatever...
...
...65536...
...
...alter...
...
...whatever...

If not (more records between the word alter and the pattern)
with Awk:

Code:
awk 'f&&/65536/{sub(/65536/,"whatever");f=0}/alter/{f=1}1' filename

Code:
awk 'f&&sub(/65536/,"whatever")&&f=0;/alter/{f=1}1' filename

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 11-22-2007
Thanks radoulov.
There may be any number of records between the
alter and 65536, so your awk code is on the mark.
I'll check it out first thing tomorrow (and yes, I'm on Solaris)
Do the two lines of code do the same?

Great work. I really appreciate the help.
# 4  
Old 11-23-2007
The awk script works great thanks, but I've discovered that there may be instances where there is no line of '65536' between the 'alter' lines.
Is there any way to skip to the next 'alter', if a 65536 line does not exist?

Thanks
# 5  
Old 11-23-2007
What do you mean by skip: you want to _ remove _ the records from alter to alter if there is not 65536 in between?
# 6  
Old 11-26-2007
awk

Hi,

This one should be ok for you.

code:

Code:
nawk ' 
{
if(f==1 && $0 ~ /65536/)
{
	sub(/65536/,"aaa")
	f=0
}
if ($0 ~ /alter/ )
	f=1
print
}' filename

# 7  
Old 11-26-2007
Apologies for the confusion in the original problem statement. I should have given more detail.
The file I am processing contains SQL statements (It's from an Oracle database export).
For each table these is a block of statements (each statement ends with ';') like -
create table tablename ....
....
.... 65536 ....
.... ;
alter table tablename
............ ;
alter table tablename
.... 65536 .... ;
alter table tablename
.... 65536 .... ;
alter table tablename
............ ;
<next create statement>

For each table there will only be one 'create' statement, but any number of 'alter' statements.
Each 'alter' statement (and the 'create' statement) has a variable number of lines.
The 'alter' statements may or may not (I missed this on the original problem definition) have the '65536'.

I am trying to change the '65536' to a variable value (using a unix variable) which comes from a parameter file. The value will be something like '524288', but only for the 'alter' statements.

So the resulting file would be like -
create table tablename ....
....
.... 65536 ....
.... ;
alter table tablename
............ ;
alter table tablename
.... 524288 .... ;
alter table tablename
.... 524288 .... ;
alter table tablename
............ ;
<next create statement>

The awk code works fine until it hits the first 'alter' with no '65536' in it, when it changes the '65536' of the next occurrence, which may be in the 'create' statement.

Your efforts are much appreciated.
Regards
Bob

Previous Thread | Next Thread
Test Your Knowledge in Computers #411
Difficulty: Medium
Aside from its AT&T UNIX base, XENIX incorporated elements from BSD, notably the vi text editor and its supporting libraries (termcap and curses).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed text replacement

Hello, I'm using Bash and Sed to replace text within a text file (1.txt) twice in one script. Using a for loop I'm initially replacing any 'apple' words with the variable 'word1' ("leg). I'm then using another for loop to replace any 'apple' words with the variable 'word2' ("arm"). This task is... (2 Replies)
Discussion started by: Flip-Flop
2 Replies

2. UNIX for Dummies Questions & Answers

sed replacement inplace

I need to make permanent changes in the file after find and replace using sed. for this i am using sed -i However this is not working. says sed: illegal option -- i I am working on Sun Solaris uname -a SunOS aspsun14 5.10 Generic_150400-13 sun4u sparc SUNW,SPARC-Enterprise any other work... (3 Replies)
Discussion started by: gotamp
3 Replies

3. Shell Programming and Scripting

sed replacement error

Hi ,"environment":"accent-release","build":"ac-1112_c_Alph_Nop-release_rele-1112_c_Alph_Nop-release_rele","tags":"" above is the config file, where in i want to replace value after build inside the double quotes... that value i'll be passing as parameter for example if m passing ... (1 Reply)
Discussion started by: nikhil jain
1 Replies

4. Shell Programming and Scripting

SED replacement

Hi, i have a file with lines, file.txt ------- test is fun testing is better I need to replace 'test' to 'develop' and i used, a=test b=develop sed "s,$a,$b,g" -------- but i see the word 'testing' is also replaced. Need some solution. Is there any way i could replace only 'test' ? (4 Replies)
Discussion started by: giri_luck
4 Replies

5. Shell Programming and Scripting

A complex sed statement

I have following requirement. Say, my text file contains following patterns {2010501005|XXGpvertex|9|0|17|0|{|{30100001|XXparameter_set|@@@@{{30001002|XXparameter|!prototype_path|$AB_COMPONENTS/Sort/Sort.mpc|3|2|Pf$|@{0|}} }}@0|@315000|78500|335000|99000|114000|87000|17|And the Sort|Ab... (8 Replies)
Discussion started by: Shell_Learner
8 Replies

6. Shell Programming and Scripting

Replacement with sed

I am trying to replace the line which has string "tablespace" not case senstive.... with below simple script: mysrcipt.sh sed "s/.*/TABLESPACE USERS/g" create_table > tmp mv tmp create_table Is there any better way to do it? If Search string tooooooo long it will be tough to code in... (4 Replies)
Discussion started by: ganeshd
4 Replies

7. Shell Programming and Scripting

Help with sed replacement

This seems like it should be an easy problem, but I'm a noob and I can't figure it out. I'm trying to use sed, but would be happy to use anything that does the job. I am trying to trim off a fixed number of unknown characters from 2 different : delimited fields while keeping the intervening... (4 Replies)
Discussion started by: helix_w
4 Replies

8. Shell Programming and Scripting

SED complex string replacement

sed -i 's:"ps -ef | grep $(cat $PID_FILE) | grep -v grep":"ps -C java -o pid,cmd | grep ${SERVER_NAME} | cut -d' ' -f1 | grep -v grep":g' scriptName That's what I'm attempting to do. I'm attempting to replace this: ps -ef | grep $(cat $PID_FILE) | grep -v grep with this: ps -C java -o... (5 Replies)
Discussion started by: cbo0485
5 Replies

9. Shell Programming and Scripting

Need Replacement for sed

Hi Can anyone provide me the replacement of sed with xargs perl syntax for the below sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' This should be without looping has to take minimal time for search (0 Replies)
Discussion started by: dbsurf
0 Replies

10. UNIX for Dummies Questions & Answers

Replacement using sed

Hi I have the following file that i need to run a sed command on 1<tab>running 2<tab>running 3<tab>running 4<tab>running I want to be able to replace a line i.e the second one with '2<tab>failed'. As the first number is unique that can be used to search for the relevant line (using ^2 i... (5 Replies)
Discussion started by: handak9
5 Replies

Featured Tech Videos