sed search and replace after xml tag


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 8  
Hi stomp,
You are correct in noting that some shells (including recent versions of bash and ksh93) can use ((cnt++)) as a shortcut for cnt=$((cnt+1)) and MadeInGermany's suggestion of cnt=$((cnt+=1)) and can also use last=${var: -1} as a shortcut for RudiC's suggestion of last=${var#${var%?}} to get the last character from the expansion of $var, but more shells (in fact any POSIX-conforming shells) support arithmetic expansions ($((expression))), and the ${var#pattern} and ${var%pattern} parameter expansions. The arithmetic command (((expression))) and the substring parameter expansions (${var:offset} and ${var:offset:length}) are extensions allowed, but not defined by the standards, and are not provided by as many shells.

But, I don't know of any shell where cnt=$((cnt++)) is required to be the same as cnt=$((cnt+1)) because the order in which a post-increment operation on a variable and an assignment to the same variable happens is unspecified. In both ksh (version: (AT&T Research) 93u+ 2012-08-01) and bash (version: 3.2.57(1)-release (x86_64-apple-darwin15)), the commands:
Code:
cnt=1
cnt=$((cnt++))
echo $cnt

produce the output:
Code:
1

Imagine the sequence of operations of cnt=$((cnt++)) as:
  1. save the value of cnt (1),
  2. increment the value of cnt to 2
  3. return the saved value (1) as the result of the arithmetic expansion, and
  4. finally, assign the returned value to cnt (negating the post-increment).
But, it could also be processed as:
  1. save the value of cnt (1),
  2. return the saved value (1) as the result of the arithmetic expansion,
  3. assign the returned value to cnt, and
  4. finally, increment the value of cnt to 2.

You have exactly the same problem in C and C++ with:
Code:
#include <stdio.h>
int main() {
	int	i = 1;
	int	j;

	for(j = 1; j <= 5; j++)
		printf("%d\n", i = i++);
}

Building it on some systems gives the warning:
Code:
cc     x.c   -o x
x.c:7:23: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
                printf("%d\n", i = i++);
                                 ~  ^
1 warning generated.

and, when run, producing the output:
Code:
1
1
1
1
1

or:
Code:
1
2
3
4
5

(But, on the system I'm currently using, it always produces just 1s as the output.)
This User Gave Thanks to Don Cragun For This Post:
# 9  
Sorrry, I wanted to suggest
Code:
cnt=$((cnt+1))

Added the = without seeing the double assignment.
BTW, only the other assignment, should be possible as well
Code:
: $((cnt+=1))

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #265
Difficulty: Easy
Alan Turing was prosecuted by the UK in 1952 for homosexual acts.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

2. How to Post in the The UNIX and Linux Forums

How to replace value of password tag in xml with blanks when special characters are there?

Hi All, I am trying to replace the values inside <password> tag in an xml file but it doesn't replace certain passwords: For eg: Server/home/sperinc>cat TextXML.txt <appIds> <entry name="AccountXref"> <type id="ldap"> <realm>nam</realm> ... (7 Replies)
Discussion started by: saroopkris85
7 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

Need to replace XML TAG

As per the requirement I need to replace XML tag with old to new on one of the XML file. Old<com : DEM>PHI</com : DEM> New<com : DEM>PHM</com : DEM> Please someone provide the sed command to replace above mentioned old XML tag with new XML tag (2 Replies)
Discussion started by: siva83
2 Replies

5. Shell Programming and Scripting

XML Parse between to tag with upper tag

Hi Guys Here is my Input : <?xml version="1.0" encoding="UTF-8"?> <xn:MeContext id="01736"> <xn:VsDataContainer id="01736"> <xn:attributes> <xn:vsDataType>vsDataMeContext</xn:vsDataType> ... (12 Replies)
Discussion started by: pareshkp
12 Replies

6. Shell Programming and Scripting

sed substition within XML tag

Hi all, I basically want to remove certain characters from within a certain XML tag: From: <mytagone>hello 1-2-3 world</mytagone> <mytagtwo>hello 1-2-3 world</mytagtwo> To: <mytagone>hello 1 2 3 world</mytagone> <mytagtwo>hello 1-2-3 world</mytagtwo> Is this possible using sed... (6 Replies)
Discussion started by: Jedimark
6 Replies

7. Shell Programming and Scripting

How to retrieve the value from XML tag whose end tag is in next line

Hi All, Find the following code: <Universal>D38x82j1JJ </Universal> I want to retrieve the value of <Universal> tag as below: Please help me. (3 Replies)
Discussion started by: mjavalkar
3 Replies

8. Shell Programming and Scripting

Need an efficient way to search for a tag in an xml file having millions of rows

Hi, I have an XML file with around 1 billion rows in it and i am trying to find the number of times a particular tag occurs in it. The solution i am using works but takes a lot of time (~1 hr) .Please help me with an efficient way to do this. Lets say the input file is <Root> ... (13 Replies)
Discussion started by: Sheel
13 Replies

9. Shell Programming and Scripting

Search for word in a xml file and replace it with something else

Hello Unix Users, I am very new to Unix so I am not sure how do I do the following. I need a script such that when I type the following in the command prompt > . scriptName.sh wordToBeReplaced DirectoryLocation will find the word someword located in a somefile.xml in DirectoryLocation... (8 Replies)
Discussion started by: 5211171
8 Replies

10. Shell Programming and Scripting

Sed command to clean xml tag

Hi, Can someone help me come up with a generic sed command to clean a tag off its attributes? For eg. Input String - <tag attrib=new>This String</tag> should undergo a sed transformation to get Output String - <tag >This String</tag> This works - echo "<tag attrib=new>This</tag>" |... (3 Replies)
Discussion started by: iamwha1am
3 Replies

Featured Tech Videos