英文:
bash script creating a newline character?
问题
Hi I have a script to read 4.29.0 and convert to 4-29-0
The raw file I am reading is this:
$ cat package.xml | grep version
<?xml version="1.0"?>
<version>4.29.0</version>
The command I have to read is this:
VERSION=$(grep "version" package.xml | awk -F '>|<' '{print $3}' | sed 's/./-/g')
If I print it
echo "testing.. $VERSION"
This will print as follows
testing..
4-29-0
Why is there a new line character and how do I remove this? I want to see the string as
testing.. 4-29-0
英文:
Hi I have a script to read 4.29.0 and convert to 4-29-0
The raw file I am reading is this:
$ cat package.xml | grep version
<?xml version="1.0"?>
<version>4.29.0</version>
The command I have to read is this:
VERSION=$(grep "version" package.xml | awk -F '>|<' '{print $3}' | sed 's/\./-/g')
If I print it
echo "testing.. $VERSION"
This will print as follows
testing..
4-29-0
Why is there a new line character and how do I remoev this ? I want to see the string as
testing.. 4-29-0
答案1
得分: 2
使用xmlstarlet
:
xmlstarlet edit --update '//version' -x 'translate(text(),".","-")' package.xml |
xmlstarlet select --template --value-of '//version' -n
输出:
<pre>
4-29-0
</pre>
参见:xmlstarlet edit
和 xmlstarlet select
英文:
With xmlstarlet
:
xmlstarlet edit --update '//version' -x 'translate(text(),".","-")' package.xml |
xmlstarlet select --template --value-of '//version' -n
Output:
<pre>
4-29-0
</pre>
See: xmlstarlet edit
and xmlstarlet select
答案2
得分: 1
你的初始 [tag:grep] 不够集中。 你需要将
VERSION=$(grep "version" package.xml
更改为
VERSION=$(grep "<version>" package.xml
以确保只有包含相关内容的行将由awk命令处理。
英文:
Your initial [tag:grep] is not sufficiently focused. You need to change
VERSION=$(grep "version" package.xml
to
VERSION=$(grep "<version>" package.xml
to ensure only the line that has the relevant content will be processed by the awk command.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论