开发者

sed - how to delete more that one space in a row?

开发者 https://www.devze.com 2023-03-27 02:12 出处:网络
I have a file with : STRING.STRING2.STRING3= VALUE1 bla bla text bla bla And i want to obtain something like this:

I have a file with :

STRING.STRING2.STRING3            = VALUE1 bla bla text bla bla

And i want to obtain something like this:

<string name="STRING.STRING2.STRING3">VALUE1 bla bla text bla bla</string>

using only shell commands like grep or sed and others

until now i reached to that:

TEMP=`cat $file |
        sed '/^\#/d' $file       
        sed 's@=@">@'                
        sed 's@\.@_@g'               
        sed 's:(\\s){3}::g'`                

echo "$TEMP " | sed 's@^@<string nam开发者_如何学编程e="@'  |
        sed 's@$@</string>@'

(also the 5th doesnt work too well...)


If you use bash, can be done completely in the shell:

str="STRING.STRING2.STRING3            = VALUE1 bla bla text bla bla"
shopt -s extglob
IFS="+( )=+( )"
read v1 v2 <<< "$str"
printf '<string name="%s">%s</string>\n' "$v1" "$v2"

produces

<string name="STRING.STRING2.STRING3">VALUE1 bla bla text bla bla</string>

Edit: to process a file

shopt -s extglob
while IFS="+( )=+( )" read v1 v2; do 
  printf '<string name="%s">%s</string>\n' "$v1" "$v2"
done < filename


Something like:

awk -F= '{gsub(/[[:space:]]*/,"",$1); gsub(/^[[:space:]]*/,"",$2); print "<string name=\""$1"\">"$2"</string>"}' test.txt

where text.txt is your file


sed -r 's/^([^ ]+) *= *(.+)$/<string name="\1">\2<\/string>/' < file
0

精彩评论

暂无评论...
验证码 换一张
取 消