Sed: add text if pattern not found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed: add text if pattern not found
# 1  
Old 04-08-2009
Sed: add text if pattern not found

Hello,

I would like to add the line TIMEZONE="CET" if the pattern TIMEZONE is not found between the range <JOB and JOB> :

Example:
Src file:

<!DOCTYPE DEFTABLE SYSTEM "deftable.dtd">
<DEFTABLE >
<JOB
TASKTYPE="Job"
TIMEFROM="0030"
TIMEZONE="CET"
</JOB>
<JOB
TASKTYPE="Job"
TIMEFROM="1130"
</JOB>
</DEFTABLE>


Sed script:

Code:
#!/bin/ksh

INPUT=$1

sed '
/<JOB/,/JOB>/{
/TIMEZONE/!{
/TIMEFROM/ a\
     TIMEZONE="CET"
}
}' $INPUT > output.txt


The string TIMEZONE="CET" is always added even if the pattern TIMEZONE is present.

I don't see where is the mistake... Any help will be really appreciated!

Thanks a lot!
# 2  
Old 04-08-2009
With awk:

Code:
awk '
/<JOB/ {f=1}
f==1 && /TIMEZONE/ {f=0}
/<\/JOB>/ && f {print "TIMEZONE=\"CET\"";f=0}
{print}' $INPUT > output.txt

Regards
# 3  
Old 04-09-2009
Thanks Franklin!

in fact, my XML file is more complex than my example...
Your awk script is not exactly what I need!

My need:

For each block (starting with <JOB and ending with JOB>), check if pattern TIMEZONE is present, if not, add the text TIMEZONE="CET" after the pattern TIMEFROM if present!

With my sed script, the text is always added even the text TIMEZONE is present! (and I don't want that!)
# 4  
Old 04-09-2009
Quote:
Originally Posted by mutunzi
in fact, my XML file is more complex than my example...
Post a more complex example of your file.

Regards
# 5  
Old 04-09-2009
I got the following sed code to work:

Code:
#!/bin/ksh
 
INPUT=$1
 
sed '
/<JOB/,/JOB>/{
/TIMEFROM/ { 
        n
        /TIMEZONE/ !{
                        i\
        TIMEZONE="CET"
        }
}
}' $INPUT > output.txt

The output is as follows:

Code:
<!DOCTYPE DEFTABLE SYSTEM "deftable.dtd">
<DEFTABLE >
<JOB
TASKTYPE="Job"
TIMEFROM="0030"
TIMEZONE="CET"
</JOB>
<JOB
TASKTYPE="Job"
TIMEFROM="1130"
        TIMEZONE="CET"
</JOB>
</DEFTABLE>

Only the indented TIMEZONE was added.
# 6  
Old 04-21-2009
Thanks rwuerth! That works fine...

Best regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

2. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

3. Shell Programming and Scripting

Append text on particular line after pattern found

hi, i have /etc/inittab, I want to add another line after that when i find a pattern "l6:6:wait:/etc/rc.d/rc 6". original l6:6:wait:/etc/rc.d/rc 6 after-change l6:6:wait:/etc/rc.d/rc 6 /sbin/if-pp-to-cng (3 Replies)
Discussion started by: learnbash
3 Replies

4. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

5. Shell Programming and Scripting

Displaying text till pattern match found in a line

Hi All, From the below line if we want to display all the text till found pattern dot/. I was trying with the below code but couldn't able to print text before the pattern. it display texts which is found after pattern. awk '/assed/{print;getline;print}' file_name | sed 's/^*. *//' input... (4 Replies)
Discussion started by: Optimus81
4 Replies

6. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

7. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

8. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. Shell Programming and Scripting

change line found by pattern using sed

I want to change a line like CPM_THRESHOLD 0.8 // to a new value using sed I am trying sed -i "s/CPM_THRESHOLD/CPM_THRESHOLD\t$COH\t\t\/\//" $INPUT_4 but how can i substitute the whole line begining with CPM_THRESHOLD and substitute it? (2 Replies)
Discussion started by: larne
2 Replies

10. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies
Login or Register to Ask a Question