请教shell读写XML问题

现有 123.xml文件,内容是:
<?xml version="1.0" encoding="GBK"?>
<videoinfo>
        <videoname>呵呵</videoname>
        <videopath>20080110</videopath>
        <videosize>0.0</videosize>
        <widthOfImage>320</widthOfImage>
        <heightOfImage>240</heightOfImage>
        <kbps>260</kbps>
        <logo>260</logo>
        <videodesc>呵呵恶化和</videodesc>
        <state>1</state>
        <uploadtime>Thu Jan 10 16:00:53 CST 2008</uploadtime>
</videoinfo>

我想用shell 脚本编写一个程序,修改里面的内容。比如修改<state>1</state>为<state>2</state> ,
修改<videosize>0.0</videosize>为<videosize>333</videosize> 该怎么操作呢。谢谢各位高手。

davistar 发表于 2008-01-10 17:47

eg
[code] 
awk '{if(/state/){sub(/>[^<]*</,">2<")}print}' urfile
sed 's//(<state>/)[^<]*/(<//state>/)//12/2/' urfile
[/code]

hyhappy 发表于 2008-01-10 17:53

楼上的好厉害,只是看的不太明白 。能大概给解释一下吗?  
awk '{if(/state/){sub(/>[^<]*</,">2<")}print}' urfile
sed 's//(<state>/)[^<]*/(<//state>/)//12/2/' urfile     这两行是都可以实现上述操作吗?我第一个测试可以,第2个测试没成功。

linuxnextyear 发表于 2008-01-10 18:16

[quote]awk '{if(/state/){sub(/>[^<]*</,">2<")}print}' urfile
[/quote]

我也刚弄完xml的,帮你解释一下这个:
if(/state/): 匹配state行才执行后面的操作
sub(/>[^<]*</,">2<")
=sub(/正则/,"b"),用b替换正则匹配的内容
其中的  />[^<]*</ 为:
匹配 >...< 的意思
[^<]* 及不是<的多个字符

所以这句话就是:匹配以<开头,直到第一个> !

hyhappy 发表于 2008-01-10 21:31

首先感谢光明使者,精灵你们二位,哈哈。
<state>1</state>
awk '{if(/state/){sub(/>[^<]*</,">2<")}print}' urfile

这条命令难理解的就是/>[^<]*</这里了。我理解的是,匹配〉〈这个之间的内容,但是为什么[^<],看精灵解释说不是<的多个字符,但是〉〈里面已经没有〈这个符号了,为什么还要排除呢。 搞不懂。

7717060 发表于 2008-01-10 22:07

<?xml version="1.0" encoding="UTF-8"?>
<address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="add.xsd">
        <group groupname="default">
                <entry>
                        <id>0001</id>
                        <name>wds</name>
                        <persioninfo>
                                <age>24</age>
                                <work>student</work>
                                <birtyday>1984</birtyday>
                                <qq>125153052</qq>
                        </persioninfo>
                        <connect>
                                <address>neusoft institute of informaction </address>
                                <tel>
                                        <office>84835207</office>
                                </tel>
                                <email href="mailto:7717060@163.com"/>
                        </connect>
                </entry>
        </group>
</address>

#!/bin/bash
#-----------------------------------------------------------------------------------
#     filename: main.sh                                                            |
#       author: wds                                                    |
#        begin: 2008.1.1                                                           |
#          end: 2008.1.4                                                           |
#      version: v.1                                                                |
#-----------------------------------------------------------------------------------
xmladd=add.xml                                                                                                 
xmltemp=.temp2                                                                                                 
tim=$(date)                                                                                                    
count=$(more $xmladd | wc -l)                                                                                 

function exitt()                                                                                              
{
rm -rf $xmltemp
exit
}

function memo()                                                                                              
{
clear
echo 
echo "----------------------------------------------------------------------------------------------"
echo "|                            ****************************                                    |"
echo "|                            *    This is XML Homework  *                                    |"
echo "|                            *        Author: wds       *                                    |"
echo "|                            *        Address List      *                                    |"
echo "|                            ****************************                                    |"
echo "|                                                                                            |"
echo "|                                1.Show Informaction                                         |"
echo "|                                2.Add Informaction                                          |"
echo "|                                3.Del Informaction                                          |"
echo "|                                4.Change Infromaction                                       |"
echo "|                                5.Select Infromcation                                       |"
echo "|                                6.Help                                                      |"
echo "                                 7.Exit                                                      |"
echo "|                                                                                            |"
echo "______________________________________________________________________________________________"    
}

function add()                                                                                               
{                                                                
clear
echo "please input your informaction!"
echo -n "id: -->" ; read id                                                                           
echo -n "name:-->"; read nam
echo -n "age: -->"; read age
echo -n "work:-->"; read work
echo -n "birthday[year-month-day]: -->" ; read birthday
echo -n "qq: -->"; read qq
echo -n "address: -->>" ; read address
echo -n "office: -->>" ; read office
echo -n "email: -->>" ; read mail
echo "        " >> $xmladd
echo "<entry>" >> $xmladd
echo "<id>$id</id>" >> $xmladd                                                                              
echo "<name>$name</name>" >> $xmladd
echo "<persion info>" >>$xmladd
echo "<age>$age</age>" >> $xmladd
echo "<work>$work</work>"
echo "<birthday>$birthday</birthday>" >> $xmladd
echo "<qq>$qq</qq>" >>$xmladd
echo "</persion info>" >> $xmladd
echo "<connect>" >> $xmladd
echo "<address>$address</address>" >> $xmladd
echo "<tel>" >>$xmladd
echo "<office>$office</office>" >> $xmladd 
echo "</tel>" >> $xmladd
echo "<email href="mailto:$mail">" >> $xmladd
echo "</connect>" >>$xmladd
echo "</entry>" >>$xmladd
}

function error()                                                 
{
clear
echo "sorry please input [1-6]:"
sleep 2
memo
}
#-------------------------------------------------------------------------------------------------
function show()                                                                                              
{
echo "--------------------------------------------------------------------------------------------"
cat  $xmladd
echo "--------------------------------------------------------------------------------------------" 
echo "Now is $tim                                                page have $count line:"                   
echo "--------------------------------------------------------------------------------------------"
echo "Do you want save your informcation(y/Y)"
echo -n "-->"; read answer

if [ "$answer" = "y" -a "$answer" = "Y" ]
then
   echo "</group>" >> $xmladd
   echo "</address>" >> $xmladd
else
   continue
fi
}

function del()                                                                                                
{
echo "please input id:"
echo -n "id: -->"; read id                                                                                     
delnum=$(more $xmladd | grep -n "$id" | cut -d: -f1)
echo "$delnum" >> ww
delnum=$(expr $delnum - 1 )                                                                                  
pageup=$delnum
echo "$pageup" >>ww
delnum=$(expr $delnum + 16 )                                                                                  
pagedown=$delnum
echo "$pagedown" >>ww
cat $xmladd | sed "$pageup,"$pagedown"d" > .temp                                                                                                                      
more .temp > $xmladd       
rm -rf .temp                                                                                                   
echo "delete  OK!!!"                                                                                            
sleep 1
}

function change()                                                                                             

echo "Please Input Id:"
echo -n "id: -->"; read id
delnum=$(more $xmladd | grep -n "$id" | cut -d: -f1)                                                          
delnum=$( expr $delnum - 1 )
pageup=$delnum
delnum=$( expr $delnum + 15 )
pagedown=$delnum
cat $xmladd | sed -n "$pageup,"$pagedown"p"                                                                  
echo -n "New id: -->" ; read id                                                                               
echo -n "New name:-->"; read name
echo -n "New age: -->"; read age
echo -n "New work:-->"; read work
echo -n "New birthday[year-month-day]: -->" ; read birthday
echo -n "New qq: -->"; read qq
echo -n "New address: -->>" ; read address
echo -n "New office: -->>" ; read office
echo -n "New email: -->>" ; read mail
echo "<entry>" >> $xmltemp
echo "<id>$id</id>" >> $xmltemp
echo "<name>$name</name>" >> $xmltemp
echo "<persion info>" >>$xmltemp
echo "<age>$age</age>" >> $xmltemp
echo "<work>$work</work>" >> $xmltemp
echo "<birthday>$birthday</birthday>" >> $xmltemp
echo "<qq>$qq</qq>" >>$xmltemp
echo "</persion info>" >> $xmltemp
echo "<connect>" >> $xmltemp
echo "<address>$address</address>" >> $xmltemp
echo "<tel>" >>$xmltemp
echo "<office>$office</office>" >> $xmltemp
echo "</tel>" >> $xmltemp
echo "<email href="mailto:$mail">" >> $xmltemp
echo "</connect>" >>$xmltemp
echo "</entry>" >>$xmltemp
cat $xmladd | sed "$pageup,"$pagedown"d" > .temp
more .temp > $xmladd
more $xmltemp >> $xmladd
}

function selectt()                                                                                            
{
while :
do
echo "Please Input Id:"
echo -n "id: -->"; read id
delnum=$( expr $delnum - 1 )
pageup=$delnum
delnum=$( expr $delnum + 15 )
pagedown=$delnum
cat $xmladd | sed -n "$pageup,"$pagedown"p"
echo -n "Back enter (e/E) continue select (*)"; read answer
if [ "$answer" = "E" -o "$answer" = "e" ]
then
  return memo
else
continue
fi
done
}

function help()                                                                                                
{
while :
do
clear
echo "
        1.show: funciton show it's can list all the informaction of xml.
        2.add:  function add it's can insert into your need informcation to the xml.
        3.del: function del it's can delete informaction from you xml.
        4.change: function change if xml have some informcation you dont need you can change use this function.
        5.select: function select  if your xml have a lots of informcation you can use this function to select your need informcaction.
        6.help:  fucntion help is help your how to use this software
        7.exit: quit 
"
echo -n "Go back to then memo!(Y/y)"; read answer
if [ "$answer" = "y" -o "$answer" = "Y" ]
then
  return memo
else
  continue
fi
done
}

while :                                                                                                        
do
memo
echo "please input your choice:"
read choice
case "$choice"
  in 
   1 ) show ;;
   2 ) add ;;
   3 ) del ;;
   4 ) change ;;
   5 ) selectt;;
   6 ) help ;;
   7 ) exitt ;;
   * ) error;;
esac
done

[[i] 本帖最后由 7717060 于 2008-1-10 22:12 编辑 [/i]]

jinl 发表于 2008-01-10 23:25

[quote]原帖由 [i]hyhappy[/i] 于 2008-1-10 21:31 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=7835443&ptid=1040744][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
首先感谢光明使者,精灵你们二位,哈哈。
1
awk '{if(/state/){sub(/>[^ [/quote]
/>[^<]*/</ 匹配> 开始任意个非<字符,并以<结束
为什么要用[^<]来匹配>...<这样形式的内容,又涉及到正则表达式的最长匹配原则
如果用/>.*</来匹配,那么如果遇到 <>1<>2<>这样的字符串,将匹配最长的>..<
即1<>2为匹配内容,而不是你需要的1作为匹配内容,所以/>[^<]*</将确保匹配第一个><中间的内容

请教shell读写XML问题的更多相关文章

  1. Linux Shell脚本读写XML文件

    在Linux下如何用Shell脚本读写XML?现有一个config.xml <?xml version="1.0" encoding="UTF-8"?&g ...

  2. 在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)

    在Linux下如何用Shell脚本读写XML?现有一个config.xml <?xml version="1.0" encoding="UTF-8"?&g ...

  3. PHP怎么读写XML?(四种方法)

    PHP怎么读写XML?(四种方法) 一.总结 1.这四种方法中,字符串的方式是最原始的方法.SimpleXML和DOM扩展是属于基于树的解析器,把整个文档存储为树的数据结构中,需要把整个文档都加载到内 ...

  4. C#读写xml文件的常用方法

    已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...

  5. PHP读写XML文件的四种方法

    PHP对XML文件进行读写操作的方法一共有四种,分别是:字符串方式直接读写.DOMDocument读写. XMLWrite写和XMLReader读.SimpleXML读写,本文将依次对这四种方法进行介 ...

  6. 在.net中序列化读写xml方法的总结

    在.net中序列化读写xml方法的总结 阅读目录 开始 最简单的使用XML的方法 类型定义与XML结构的映射 使用 XmlElement 使用 XmlAttribute 使用 InnerText 重命 ...

  7. ADO.NET 快速入门(六):读写 XML

    ADO.NET 和 DataSet 可以读写 XML Schema 和 XML.获取更多信息,请参考 How do I...Use XML and the DataSet?   DataSet 的 S ...

  8. C#_在.net中序列化读写xml方法的总结

    阅读目录 开始 最简单的使用XML的方法 类型定义与XML结构的映射 使用 XmlElement 使用 XmlAttribute 使用 InnerText 重命名节点名称 列表和数组的序列化 列表和数 ...

  9. Java 读写XML文件 API--org.dom4j

    om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...

  10. Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

随机推荐

  1. opensbi入门

    OpenSBI 入门 声明 本文为本人原创,未经允许,严禁转载. FW_JUMP FW_PAYLOAD FW_DYNAMIC FW_JUMP OpenSBI 带跳转地址的固件(FW_JUMP)是一种仅 ...

  2. 【转载】老男孩读PCIe

    目录 老男孩读PCIe之一:从PCIe速度说起 老男孩读PCIe之二:PCIe拓扑结构 老男孩读PCIe之三:PCIe分层结构 老男孩读PCIe之四:TLP类型 老男孩读PCIe之五:TLP结构 老男 ...

  3. Cilium系列-11-启用带宽管理器

    系列文章 Cilium 系列文章 前言 将 Kubernetes 的 CNI 从其他组件切换为 Cilium, 已经可以有效地提升网络的性能. 但是通过对 Cilium 不同模式的切换/功能的启用, ...

  4. [mysql]MGR简介与部署

    前言 MySQL Group Replication,简称MGR,是MySQL官方于2016年推出的一个全新的高可用解决方案,采用Paxos分布式一致性协议作为高可用和一致性解决方案.在MGR之前的高 ...

  5. jsp+servlet实战项目

    第一步:新建maven项目,项目中添加dao,entity,service,servlet,util包第二步:导入依赖 第三步:数据库建表 第四步:entity实体包(疯转) 第五步:在util工具包 ...

  6. 开源.NetCore通用工具库Xmtool使用连载 - HTTP请求篇

    [Github源码] <上一篇> 介绍了Xmtool工具库中的XML操作类库,今天我们继续为大家介绍其中的HTTP请求类库. 在现如今的软件需求场景中,HTTP网络请求几乎是开发过程中必然 ...

  7. chatglm2-6b在P40上做LORA微调

    背景: 目前,大模型的技术应用已经遍地开花.最快的应用方式无非是利用自有垂直领域的数据进行模型微调.chatglm2-6b在国内开源的大模型上,效果比较突出.本文章分享的内容是用chatglm2-6b ...

  8. KRpano项目微信出现"关于潜在的违法或违规内容"

    最近,部分小伙伴反应某些KRPano项目在微信中,出现"关于潜在的未发或违规内容"而无法播放的问题,会看到下图中的提示: 出现原因 这个问题是由于KRPano项目中的webvr.j ...

  9. 「codeforces - 1208F」Bits and Pieces

    link. 考虑把原问题写成一个在 \(\left(\log_2 \max v \right) \times n\) 的矩阵里选出三列,我们首先预处理出 \(j \cap q\).具体,我们需要对于每 ...

  10. oracle:修改表空间

    近来新建了一张表,发现放错表空间,且表中已有数据,故想到修改已有表的表空间 1.查看表所在的表空间select table_name,tablespace_name from dba_tables w ...