[英]Linux find and replace from command line
I'm looking for a linux command line script to do the following. 我正在寻找一个Linux命令行脚本来执行以下操作。
Assume I have a file 假设我有一个文件
ABC|1|READ11|Reading|1
ABj|2|READ11|Reading|2
CBD|3|WRIT11|Reading|3
BCD|4|READ11|Reading|4
CDE|5|WRIT11|Reading|5
CBA|6|READ11|Reading|6
for every line that start with the letter 'C', if it has 'WRIT11', replace with 'REPL11' 对于以字母“ C”开头的每一行,如果其具有“ WRIT11”,则替换为“ REPL11”
File after script 脚本后文件
ABC|1|READ11|Reading|1
ABj|2|READ11|Reading|2
CBD|3|REPL11|Reading|3
BCD|4|READ11|Reading|4
CDE|5|REPL11|Reading|5
CBA|6|READ11|Reading|6
It can be done like this: 可以这样完成:
sed 's/\(^C.*\)WRIT11/\1REPL11/g' your_file
Explanation: 说明:
s/search_this/replace_it_with_this/g
s at the beginning is for search, the g at the end means globally, not just for one line. s/search_this/replace_it_with_this/g
开头是用于搜索,结尾处的g表示全局,而不仅仅是一行。
In the search_this part the part within \\(
and \\)
can be written as \\1
in the replace_it_with_this part. 在search_this部分中,
\\(
和\\)
的部分可以在replace_it_with_this部分中写为\\1
。
The ^C
means there's a C at the beginning of the line and the followed .
该
^C
意味着有在该行和随后的开始一个C .
means any character and the *
means that the preceeding character can occur not at all or multiple times. 表示任何字符,
*
表示前面的字符不能全部出现或多次出现。
Use something like this: 使用这样的东西:
awk -F'|' '($1 ~ /^C.*$/) && ($3 =="WRIT11") \
{print $1"|"$2"|REPL11|"$4"|"$5; next;}{print;}' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.