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 and regular expressions?
Many thanks in advance,
Mark
Hello,
Following may help you in same.
awk 'NR==1 {gsub(/\-/," ")} 1' file_name
Output will be as follows.
<mytagone>hello 1 2 3 world</mytagone>
<mytagtwo>hello 1-2-3 world</mytagtwo>
EDIT: one more solution for same.
awk -va=1 'a==1 {gsub(/\-/," "); a++} 1' file_name
Output will be as follows.
<mytagone>hello 1 2 3 world</mytagone>
<mytagtwo>hello 1-2-3 world</mytagtwo>
EDIT: Adding one more solution with sed
.
sed '1s/\(.*\)\(\-\)\(.*\)\(\-\)\(.*\)/\1 \3 \5/' file_name
Ouptut will be as follows.
<mytagone>hello 1 2 3 world</mytagone>
<mytagtwo>hello 1-2-3 world</mytagtwo>
Thanks,
R. Singh
1 Like
Subbeh
3
sed '/<mytagone>/s/-/ /g' file
1 Like
OR
$ awk '!f && $0 ~ "<"tag".*>"{f=gsub(/-/," ")}f' tag='mytag' file
1 Like
Thanks to everyone for their responses.
sed '/<mytagone>/s/-/ /g' file
... is sufficient for my requirement.
But I wonder is there a sed solution to:
From:
<mytag_one>hello 1_2_3 world</mytag_one>
<mytag_two>hello 1_2_3 world</mytag_two>
To:
<mytag_one>hello 1 2 3 world</mytag_one>
<mytag_two>hello 1_2_3 world</mytag_two>
Mark
rdrtx1
6
an awk:
awk '/<mytag_one>/ {gsub("_", " ", $3); $0="<" $2 ">" $3 "<" $4 ">"} 1' FS="[<>]" infile
Try:
sed -e :a -e '/mytag_one/s/\(>.*\)_\(.*<\)/\1 \2/; ta' file
or
sed -e :a -e '/mytag_one/s/\(>[^_<]*\)_/\1 /; ta' file