在 xml 中拆分字符串
Split string in xml
我有 xml 个包含数据的文件。
<STOKUNIT>
<BELGETARIGI>2017-09-20</BELGETARIGI>
<STOKKODU>1KKUPS0098</STOKKODU>
<STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
<RENK>139</RENK>
<BEDEN>.</BEDEN>
<STOKBARKODU>8680044399128</STOKBARKODU>
<FATURANO>6-R-7-4825</FATURANO>
<MAGAZAKODU>MGUR-02</MAGAZAKODU>
<MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
<MIKTAR>1.00</MIKTAR>
<SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
<SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
<KDVTUTAT>4.71</KDVTUTAT>
<FIYAT>30.89</FIYAT>
<TUTAR>30.89</TUTAR>
<INDIRIMTUTAR>0.00</INDIRIMTUTAR>
<BIRIMFIYAT>30.89</BIRIMFIYAT>
<NETTUTAR>30.89</NETTUTAR>
<ODEMETIPI> Nakit</ODEMETIPI>
<ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>
我只想拆分 FATURANO 那样
6-R-7
和
4825
这个“4825”应该有不同的名字。
<STOKUNIT>
<BELGETARIGI>2017-09-20</BELGETARIGI>
<STOKKODU>1KKUPS0098</STOKKODU>
<STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
<RENK>139</RENK>
<BEDEN>.</BEDEN>
<STOKBARKODU>8680044399128</STOKBARKODU>
<FATURANO>6-R-7</FATURANO>
***<NEWONE>4825</NEWONE>***
<MAGAZAKODU>MGUR-02</MAGAZAKODU>
<MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
<MIKTAR>1.00</MIKTAR>
<SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
<SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
<KDVTUTAT>4.71</KDVTUTAT>
<FIYAT>30.89</FIYAT>
<TUTAR>30.89</TUTAR>
<INDIRIMTUTAR>0.00</INDIRIMTUTAR>
<BIRIMFIYAT>30.89</BIRIMFIYAT>
<NETTUTAR>30.89</NETTUTAR>
<ODEMETIPI> Nakit</ODEMETIPI>
<ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>
我需要像这样转换 xml 数据,但如果你只回答我第一个问题如何正确拆分字符串,我将进行其他转换
由于 awk
标签是由 OP 添加的,因此尝试在其中回答。
awk -v s1=" " -F"[><]" '
/FATURANO/{
match(,/.*-/);
val1=substr(,RSTART,RLENGTH-1);
val2=substr(,RLENGTH+1);
print [=10=] RS s1 "<test>" val1 "</" val1 ">" RS s1 "<test>" val2 "</" val2 ">";
next
}
1
' Input_file
使用 xmlstarlet 和 bash 的 Parameter Expansion:
string=$(xmlstarlet select --template --value-of '//STOKUNIT/FATURANO' file.xml)
part1="${string%-*}"
part2="${string##*-}"
xmlstarlet edit --omit-decl --update '//STOKUNIT/FATURANO' --value "$part1" --append '//STOKUNIT/FATURANO' --type elem -n NEWONE --value "$part2" file.xml
如果您想就地编辑 file.xml,请将选项 -L 添加到最后一个 xmlstarlet 命令。
输出:
<STOKUNIT>
<BELGETARIGI>2017-09-20</BELGETARIGI>
<STOKKODU>1KKUPS0098</STOKKODU>
<STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
<RENK>139</RENK>
<BEDEN>.</BEDEN>
<STOKBARKODU>8680044399128</STOKBARKODU>
<FATURANO>6-R-7</FATURANO>
<NEWONE>4825</NEWONE>
<MAGAZAKODU>MGUR-02</MAGAZAKODU>
<MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
<MIKTAR>1.00</MIKTAR>
<SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
<SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
<KDVTUTAT>4.71</KDVTUTAT>
<FIYAT>30.89</FIYAT>
<TUTAR>30.89</TUTAR>
<INDIRIMTUTAR>0.00</INDIRIMTUTAR>
<BIRIMFIYAT>30.89</BIRIMFIYAT>
<NETTUTAR>30.89</NETTUTAR>
<ODEMETIPI> Nakit</ODEMETIPI>
<ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>
参见:xmlstarlet select --help
和 xmlstarlet edit --help
用单个xmlstarlet表达式:
xmlstarlet ed -u '//STOKUNIT/FATURANO' -x "substring(., 0,string-length(.)-4)" \
-a '//STOKUNIT/FATURANO' -t elem -n NEWONE \
-v "$(xmlstarlet sel -t -v 'substring(//STOKUNIT/FATURANO, string-length(//STOKUNIT/FATURANO)-3)' file.xml)" file.xml
ed
- 编辑 模式
-x
- xpath 表达式
-a
- 添加元素
-t
- 附加元素的 type(在 edit 模式下)
-n
- 附加元素的名称
-v
- updated/appended 元素的值
输出:
<?xml version="1.0"?>
<STOKUNIT>
<BELGETARIGI>2017-09-20</BELGETARIGI>
<STOKKODU>1KKUPS0098</STOKKODU>
<STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
<RENK>139</RENK>
<BEDEN>.</BEDEN>
<STOKBARKODU>8680044399128</STOKBARKODU>
<FATURANO>6-R-7</FATURANO>
<NEWONE>4825</NEWONE>
<MAGAZAKODU>MGUR-02</MAGAZAKODU>
<MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
<MIKTAR>1.00</MIKTAR>
<SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
<SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
<KDVTUTAT>4.71</KDVTUTAT>
<FIYAT>30.89</FIYAT>
<TUTAR>30.89</TUTAR>
<INDIRIMTUTAR>0.00</INDIRIMTUTAR>
<BIRIMFIYAT>30.89</BIRIMFIYAT>
<NETTUTAR>30.89</NETTUTAR>
<ODEMETIPI> Nakit</ODEMETIPI>
<ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>
我有 xml 个包含数据的文件。
<STOKUNIT>
<BELGETARIGI>2017-09-20</BELGETARIGI>
<STOKKODU>1KKUPS0098</STOKKODU>
<STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
<RENK>139</RENK>
<BEDEN>.</BEDEN>
<STOKBARKODU>8680044399128</STOKBARKODU>
<FATURANO>6-R-7-4825</FATURANO>
<MAGAZAKODU>MGUR-02</MAGAZAKODU>
<MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
<MIKTAR>1.00</MIKTAR>
<SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
<SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
<KDVTUTAT>4.71</KDVTUTAT>
<FIYAT>30.89</FIYAT>
<TUTAR>30.89</TUTAR>
<INDIRIMTUTAR>0.00</INDIRIMTUTAR>
<BIRIMFIYAT>30.89</BIRIMFIYAT>
<NETTUTAR>30.89</NETTUTAR>
<ODEMETIPI> Nakit</ODEMETIPI>
<ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>
我只想拆分 FATURANO 那样
6-R-7
和
4825
这个“4825”应该有不同的名字。
<STOKUNIT>
<BELGETARIGI>2017-09-20</BELGETARIGI>
<STOKKODU>1KKUPS0098</STOKKODU>
<STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
<RENK>139</RENK>
<BEDEN>.</BEDEN>
<STOKBARKODU>8680044399128</STOKBARKODU>
<FATURANO>6-R-7</FATURANO>
***<NEWONE>4825</NEWONE>***
<MAGAZAKODU>MGUR-02</MAGAZAKODU>
<MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
<MIKTAR>1.00</MIKTAR>
<SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
<SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
<KDVTUTAT>4.71</KDVTUTAT>
<FIYAT>30.89</FIYAT>
<TUTAR>30.89</TUTAR>
<INDIRIMTUTAR>0.00</INDIRIMTUTAR>
<BIRIMFIYAT>30.89</BIRIMFIYAT>
<NETTUTAR>30.89</NETTUTAR>
<ODEMETIPI> Nakit</ODEMETIPI>
<ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>
我需要像这样转换 xml 数据,但如果你只回答我第一个问题如何正确拆分字符串,我将进行其他转换
由于 awk
标签是由 OP 添加的,因此尝试在其中回答。
awk -v s1=" " -F"[><]" '
/FATURANO/{
match(,/.*-/);
val1=substr(,RSTART,RLENGTH-1);
val2=substr(,RLENGTH+1);
print [=10=] RS s1 "<test>" val1 "</" val1 ">" RS s1 "<test>" val2 "</" val2 ">";
next
}
1
' Input_file
使用 xmlstarlet 和 bash 的 Parameter Expansion:
string=$(xmlstarlet select --template --value-of '//STOKUNIT/FATURANO' file.xml)
part1="${string%-*}"
part2="${string##*-}"
xmlstarlet edit --omit-decl --update '//STOKUNIT/FATURANO' --value "$part1" --append '//STOKUNIT/FATURANO' --type elem -n NEWONE --value "$part2" file.xml
如果您想就地编辑 file.xml,请将选项 -L 添加到最后一个 xmlstarlet 命令。
输出:
<STOKUNIT>
<BELGETARIGI>2017-09-20</BELGETARIGI>
<STOKKODU>1KKUPS0098</STOKKODU>
<STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
<RENK>139</RENK>
<BEDEN>.</BEDEN>
<STOKBARKODU>8680044399128</STOKBARKODU>
<FATURANO>6-R-7</FATURANO>
<NEWONE>4825</NEWONE>
<MAGAZAKODU>MGUR-02</MAGAZAKODU>
<MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
<MIKTAR>1.00</MIKTAR>
<SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
<SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
<KDVTUTAT>4.71</KDVTUTAT>
<FIYAT>30.89</FIYAT>
<TUTAR>30.89</TUTAR>
<INDIRIMTUTAR>0.00</INDIRIMTUTAR>
<BIRIMFIYAT>30.89</BIRIMFIYAT>
<NETTUTAR>30.89</NETTUTAR>
<ODEMETIPI> Nakit</ODEMETIPI>
<ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>
参见:xmlstarlet select --help
和 xmlstarlet edit --help
用单个xmlstarlet表达式:
xmlstarlet ed -u '//STOKUNIT/FATURANO' -x "substring(., 0,string-length(.)-4)" \
-a '//STOKUNIT/FATURANO' -t elem -n NEWONE \
-v "$(xmlstarlet sel -t -v 'substring(//STOKUNIT/FATURANO, string-length(//STOKUNIT/FATURANO)-3)' file.xml)" file.xml
ed
- 编辑 模式-x
- xpath 表达式-a
- 添加元素-t
- 附加元素的 type(在 edit 模式下)-n
- 附加元素的名称-v
- updated/appended 元素的值
输出:
<?xml version="1.0"?>
<STOKUNIT>
<BELGETARIGI>2017-09-20</BELGETARIGI>
<STOKKODU>1KKUPS0098</STOKKODU>
<STOKADI>New Bone China 3-Lu Kupa Seti</STOKADI>
<RENK>139</RENK>
<BEDEN>.</BEDEN>
<STOKBARKODU>8680044399128</STOKBARKODU>
<FATURANO>6-R-7</FATURANO>
<NEWONE>4825</NEWONE>
<MAGAZAKODU>MGUR-02</MAGAZAKODU>
<MAGAZAADI>GURCISTAN TIFLIS DIGOMI CAD. MAGAZA</MAGAZAADI>
<MIKTAR>1.00</MIKTAR>
<SATISSORUMLUSUKODU>99055</SATISSORUMLUSUKODU>
<SATISSORUMLUSUADI>MARIAM SULIAVRI</SATISSORUMLUSUADI>
<KDVTUTAT>4.71</KDVTUTAT>
<FIYAT>30.89</FIYAT>
<TUTAR>30.89</TUTAR>
<INDIRIMTUTAR>0.00</INDIRIMTUTAR>
<BIRIMFIYAT>30.89</BIRIMFIYAT>
<NETTUTAR>30.89</NETTUTAR>
<ODEMETIPI> Nakit</ODEMETIPI>
<ODEMEACIKLAMASI> Nakit</ODEMEACIKLAMASI>
</STOKUNIT>