If condition and for loop within sed statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition and for loop within sed statement
# 1  
Old 04-11-2013
If condition and for loop within sed statement

Hi,

I tried to go through a lot of online material but could not find concrete solution.
My issues is like this :

I've got a input file like this :
Code:
<a>
        <startDate>19700101000000</startDate>
        <endDate>20300101000000</endDate>
</a>
        <startDate>19700101000000</startDate>
        <endDate>20300101000000</endDate>
<b>
        <startDate>19700101000000</startDate>
        <endDate>20300101000000</endDate>
</b>
        <startDate>19700101000000</startDate>
        <endDate>20300101000000</endDate>
<c>
        <startDate>19700101000000</startDate>
        <endDate>20300101000000</endDate>
</c>
<d>
        <startDate>19700101000000</startDate>
        <endDate>20300101000000</endDate>
</d>

Now I want to write a single sed command in such a way that I would give $i as input to sed statement to replace date value of ith occurance.
e.g. if i = 2 , then value of 19700101000000 within <stardate></startdate> within <b></b> is changed to 20200101000000. Similarly if i =1, then startdate within a,if i=3 then from within <c> is changed to 20200101000000.
I tried doing :
Code:
sed '/startDate/{s/19700101000000/20200101000000/;};H' "FILENAME.xml"

how to iterate it to loop through a to d and change values as mentioned above.

Please assist.
# 2  
Old 04-11-2013
How does your actual file look like? Does it have a,b,c,d? And why a single sed?

--ahamed
# 3  
Old 04-11-2013
No Actual file does not have tags like a,b,c...
Actual file has <PRE1>,<PRE2>,<PRE3>......
Also here i mean by single sed command is a one liner command. We can append as many -e,-n but want to complete it as a single liner.
# 4  
Old 04-11-2013
Will the elements within the <PREn> tags remain the same? i.e. 2?
Will awk do?

--ahamed
# 5  
Old 04-11-2013
No they will also not remain same. But will definitely contain <startdate> and <enddate> tags.
I know awk would be much easier to do, but can't we do it with sed ?
# 6  
Old 04-11-2013
what do you mean not in sequential order?

--ahamed
# 7  
Old 04-11-2013
Please see my updated reply. :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

If statement with unmatched condition

Hi Gurus, I'm facing some issues with multiple conditions in my if statement. if (!($InputLine=~/^Date/)) && (!($fields eq "VEN")) { Above is the line troughing some syntax errors. I am trying to avoid the below creteria lines to process in my logic. Records starting with... (4 Replies)
Discussion started by: hi.villinda
4 Replies

3. Shell Programming and Scripting

Two condition in if statement

Hi, I need to put two condition in if statement, but it is not working. Please suggest. if ---------- Post updated at 07:05 AM ---------- Previous update was at 06:55 AM ---------- Also when i put below command in script it is not running, but manually it is running ... (4 Replies)
Discussion started by: learnbash
4 Replies

4. Shell Programming and Scripting

While Loop with if else condition

Hi, I was trying to write a shell script which reads csv file and sends mail in html format along with tables. Hope i have completed 1st part , but while sending mail i was trying to highlight some rows in the table based on the egrep outcome. If the string exists in line/INPUT, i am trying to... (4 Replies)
Discussion started by: varmas424
4 Replies

5. UNIX for Dummies Questions & Answers

LINUX Multiple condition in IF Statement - Pls help

Hi All, I am trying to put multiple conditions in an IF Statement (using $$). the Linux script somehow doesnt like it. The logic I am trying to implement is as follows, 1. I will first search for DateFile.txt 2. If it exists & there is a P_BUS_DATE value in it, then assign the date value... (5 Replies)
Discussion started by: dsfreddie
5 Replies

6. Shell Programming and Scripting

Use of -z in while loop condition

Hi, Could you please tell what is the meaning of -z in while loop condition. For example, while ; do echo "*** Enter the age " readage (3 Replies)
Discussion started by: vidyaj
3 Replies

7. Shell Programming and Scripting

if condition in a while loop

Gurus, I need to read a line from a file and strip the characters from it and compare the stripped value with the value I pass to the script while executing it. Below is the code for the same. But when i execute the code, it is throwing an error. #!/bin/ksh . /home/.i_env ... (14 Replies)
Discussion started by: svajhala
14 Replies

8. Shell Programming and Scripting

An issue with condition statement in shell script

Hello forum members. please go through the below mentioned issue and let me know the right solution. I have to write a script which runs another script .the executable script take input parmeters.so iam writing the the script below . Sample Code:Begins #! /bin/ksh echo " enter... (2 Replies)
Discussion started by: rajkumar_g
2 Replies

9. Shell Programming and Scripting

Condition statement in perl

#!/usr/bin/perl $output1 = "/home/log.txt" $output2 = "/home/grep.txt" #Statement1 creates an output file called log.txt. #Statement2 greps a line from log.txt and store the result in grep.txt I want to create a condition where if the file grep.txt is empty repeat process. Thanks. (1 Reply)
Discussion started by: sureshcisco
1 Replies

10. UNIX for Dummies Questions & Answers

Multiple Condition If statement

Hi, I would like to create an IF statement where if a variable is equal to at least one of 2 (or more) values then the script proceeds. For example: TEST_VAR=2 if ; then echo success! else echo failure fi I understand that the above syntax is wrong but I feel it must be close. Any... (1 Reply)
Discussion started by: msb65
1 Replies
Login or Register to Ask a Question