简体   繁体   English

在bash中的文件中进行几种替换的最紧凑或最有效的方法是什么

[英]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.

相关问题 使用Bash编写JSON文件的最有效方法是什么? - What is the most efficient way of writing a JSON file with Bash? 在 Bash 中用另一个文件内容替换文件内容的最有效(就速度和行数而言)方法是什么? - What is the most efficient (in terms of speed and number of lines) way to replace a file content by another file content in Bash? 在bash脚本中测试多个值的最有效方法是什么 - What is the most efficient way to test multiple values in a bash script 在bash中移动多个通配符的最有效方法是什么? - What's the most efficient way to move multiple wildcards in bash? 在 bash 中执行异常处理的最有效方法? - Most efficient way to perform exception handling in bash? bash重定向文件描述符3是读取tcp端口并写入文本文件的最有效方法吗? - Is bash redirection of file descriptor 3 the most efficient way to read a tcp port and write to a text file? 在python中创建具有权限的文件夹的最有效方法是什么(与运行bash / shell命令相比)? - What is the most efficient way to create a folder with permissions in python (vs. running bash/shell commands)? 在bash脚本中串联不同压缩类型的文件的最有效方法是什么? - What is the most efficient way to concatenate files of different compression types in a bash script? 在 Bash 中转置文件的有效方法 - An efficient way to transpose a file in Bash 在 Bash 中获取文件(仅当它存在时)的最简洁方法是什么? - What is the most concise way to source a file (only if it exists) in Bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM