简体   繁体   English

将文件中的键/值从unix提取到另一个文件

[英]Extract Key/value from file to another file in unix

Hello i have used this code to extract Key/Value from file to another file 您好我已经使用此代码从文件中提取键/值到另一个文件

BugReports: https://r-forge.r-project.org/tracker/?group_id=194
NeedsCompilation: no
Repository: CRAN
Date/Publication: 2014-03-02 12:40:42" > FichierTest


IFS=":" 
echo $(while read f1 f2
do 
   echo "$f1 ; $f2" 
done < FichierTest) > test.dsc

content of test.dsc: test.dsc的内容:

BugReports ;  https //r-forge.r-project.org/tracker/?group_id=194
NeedsCompilation ;  no
Repository ;  CRAN
Date/Publication ;  2014-03-02 12 40 42

I just want to change the first colon ":" to semi-colon ";" 我只想将第一个冒号“:”更改为分号“;” in each line 在每一行

Many Thanks 非常感谢

Why not just use one-time matching with sed ? 为什么不与sed一次性匹配?

sed -e 's|:|;|' file

Output: 输出:

BugReports; https://r-forge.r-project.org/tracker/?group_id=194
NeedsCompilation; no
Repository; CRAN
Date/Publication; 2014-03-02 12:40:42

Or if you really want to replace everything: 或者,如果你真的想要替换一切:

sed -e 's|:|;|g' file

Output: 输出:

BugReports; https;//r-forge.r-project.org/tracker/?group_id=194
NeedsCompilation; no
Repository; CRAN
Date/Publication; 2014-03-02 12;40;42

Add spaces to replacement if needed. 如果需要,添加空格以替换。

By the way, the reason why your output gets spaces instead of colons in some parts is because IFS still has its effect on word splitting. 顺便说一下,你的输出在某些部分得到空格而不是冒号的原因是因为IFS仍然对单词拆分有影响。 You have to quote your command substitution on it to fix it: 您必须在其上引用命令替换来修复它:

IFS=":" 
echo "$(while read f1 f2
do 
   echo "$f1 ; $f2" 
done < FichierTest)"

Output: 输出:

BugReports ;  https://r-forge.r-project.org/tracker/?group_id=194
NeedsCompilation ;  no
Repository ;  CRAN
Date/Publication ;  2014-03-02 12:40:42

And the proper way to do it actually is this: 实际上这样做的正确方法是:

while IFS=: read -r f1 f2; do 
   echo "$f1 ; $f2" 
done < FichierTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM