Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

awk - Add text: case insensitive and cross-platform

I have a file in which I need to convert the lines. From

<id>ABC</id>
<guid isPermaLink="false">ABC</guid>

need to get the following:

<id>ABC123</id>
<guid isPermaLink="false">ABC123</guid>

i.e., add text before </id> or </guid>.

The problem is that I cannot make this case insensitive and cross-platform; for BSD and GNU. As far as I understand, sed and awk are not very suitable for this purpose because of the difference between the BSD and GNU versions.

At this point I'm kind of stumped and don't know how to do such a fairly simple operation. Maybe someone can suggest the best way to solve it? Maybe it is possible to do this using only the shell's functions?

Thx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With sub function of awk you could try following. Though experts always advise to use tools like xmlstarlet which understand xml better.

awk '{sub(/</(gu)?id/,"123&")} 1' Input_file

OR in case you want to make code to use ignorecase to match capital/smal letters then try following.

awk '{sub(/</([gG][Uu])?[iI][Dd]/,"123&")} 1' Input_file

OR in case you have GNU awk in with you try with IGNORECASE option:

awk -v IGNORECASE=1 '{sub(/</(gu)?id/,"123&")} 1' Input_file

Explanation: Using sub(substitution) function of awk to substitute </guid> OR </id> with 123</guid> and 123</id> respectively.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...