[英]What is the most compact or efficient way of doing several subsitutions in a file in bash
I have a file data.base which looks like: 我有一个文件data.base,看起来像:
1234 XXXX
4321 XXXX
9884 ZZZZ
5454 YYYY
4311 YYYY
9882 ZZZZ
9976 ZZZZ
( ... random occurrences like this till 10000 lines) (...这样的随机出现直到10000行)
I would like to create a file called data.case which derives from data.base just with substitutions of XXXX, YYYY, ZZZZ for float numbers. 我想创建一个名为data.case的文件,该文件从data.base派生,只是用XXXX,YYYY,ZZZZ替换浮点数。
I wonder what would be the most compact/efficient/short way to do that on bash or friends. 我不知道在bash或朋友上做这件事的最紧凑/最有效/最快捷的方法是什么。
What I usually do is something like: 我通常要做的是:
sed -e "s/XXXX/1.34555/g" data.base > temp1
sed -e "s/YYYY/2.985/g" temp1 > temp2
sed -e "s/ZZZZ/-4.3435/g" temp2 > data.case
rm -fr temp1 temp2
But I do not think this is the most compact or efficient way when you have to deal with more than 3 substitutions. 但是当您必须处理3个以上的替换时,我认为这不是最紧凑或最有效的方法。
Thanks 谢谢
Thanks 谢谢
使用一个选项在同一sed中执行几个命令:
sed "s/XXXX/1.34555/g; s/YYYY/2.985/g"; s/ZZZZ/-4.3435/g" data.base > data.case
$ cat sedcommands
s/XXXX/1.34555/g
s/YYYY/2.985/g
s/ZZZZ/-4.3435/g
$ sed -f sedcommands data.base > data.case
you can make use of associative arrays in awk 您可以在awk中使用关联数组
awk 'BEGIN{
# add as needed
s["XXXX"]=1.3455
s["YYYY"]=2.985
s["ZZZZ"]=-4.3435
}
($2 in s) { print $1,s[$2] }' file
output 输出
$ ./shell.sh
1234 1.3455
4321 1.3455
9884 -4.3435
5454 2.985
4311 2.985
9882 -4.3435
9976 -4.3435
sed -e "s/XXXX/1.34555/g;s/YYYY/2.985/g;s/ZZZZ/-4.3435/g" sed -e“ s / XXXX / 1.34555 / g; s / YYYY / 2.985 / g; s / ZZZZ / -4.3435 / g”
or put them in a cmd file and list them out. 或将它们放在cmd文件中并列出。
Whilst sed
can do multiple substitutions in one pass, the general UNIX approach which is more widely applicable and can be combined with other commands is to use command piping: 尽管
sed
可以一次完成多个替换操作, 但是 通用性更高的UNIX方法 (可以与其他命令结合使用)是使用命令管道:
cat data.base | \
sed -e "s/XXXX/1.34555/g" | \
sed -e "s/YYYY/2.985/g" | \
sed -e "s/ZZZZ/-4.3435/g" > data.base
The redirection at the end will ' unlink
' the old data.base that is being used as input by cat
; 最后的重定向将'
unlink
' cat
用作输入的旧数据库; you could however still use a temporary file so that you can intercept error conditions and not have lost the original data.base in the process. 但是,您仍然可以使用一个临时文件,以便可以拦截错误情况并且不会在此过程中丢失原始的数据库。
(When using piping, its useful to be familiar with the tee
program, which saves the stream to a file whilst passing it on) (使用管道系统时,熟悉
tee
程序很有用,该程序可将流保存到文件中并继续传递)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.