Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 缩进与单词匹配的行,然后用替换字符打印_Linux_Ubuntu_Awk_Sed - Fatal编程技术网

Linux 缩进与单词匹配的行,然后用替换字符打印

Linux 缩进与单词匹配的行,然后用替换字符打印,linux,ubuntu,awk,sed,Linux,Ubuntu,Awk,Sed,我正在尝试将所有出现的open替换为not open。我想打印原始文本,修改后的行缩进并以原始形式保留,然后是修改后的形式 此命令不够: sed 's/open/not open/' file.txt 样本输入: this is the first line of text this text is very open and this is the next line of text 样本输出: this is the first line of text this text i

我正在尝试将所有出现的
open
替换为
not open
。我想打印原始文本,修改后的行缩进并以原始形式保留,然后是修改后的形式

此命令不够:

sed 's/open/not open/' file.txt
样本输入:

this is the first line of text
this text is very open
and this is the next line of text 
样本输出:

this is the first line of text
    this text is very open
    this text is very not open 
and this is the next line of text 

给定此
input.txt

this is the first line of text this text is very open and this is the next line of text 产出:

this is the first line of text this text is very open this text is very not open and this is the next line of text 这是正文的第一行 这篇文章非常开放 这篇文章很不开放 这是下一行文字 第一个子句只跳过任何与
not open
匹配的行(这意味着读到
的行门没有打开。一定要打开它。
没有替换)。
b
命令跳到脚本末尾。如果不需要这种逻辑,可以省略这一部分


第二个子句在任何其他行提到的
open
上键入,并对其执行三个操作:缩进四个空格,立即打印,然后进行替换。最后,
sed
在计算结束时自动打印该行(修改或未修改,即使已打印)。鉴于此
input.txt

this is the first line of text this text is very open and this is the next line of text 产出:

this is the first line of text this text is very open this text is very not open and this is the next line of text 这是正文的第一行 这篇文章非常开放 这篇文章很不开放 这是下一行文字 第一个子句只跳过任何与
not open
匹配的行(这意味着读到
的行门没有打开。一定要打开它。
没有替换)。
b
命令跳到脚本末尾。如果不需要这种逻辑,可以省略这一部分

第二个子句在任何其他行提到的
open
上键入,并对其执行三个操作:缩进四个空格,立即打印,然后进行替换。最后,
sed
在评估结束时自动打印行(修改或未修改,即使已打印)。

这可能适用于您(GNU-sed):

sed'/\/!Bs/^/\t/p;s/\/not&/g文件
不包含单词
open
的任何行正常打印。否则,请在行首插入一个选项卡并打印原始行,然后将所有
打开的
替换为
未打开的
并打印。

这可能适用于您(GNU-sed):

sed'/\/!Bs/^/\t/p;s/\/not&/g文件

不包含单词
open
的任何行正常打印。否则,请在行首插入一个制表符并打印原始行,然后将所有
open
替换为
not open
并打印。

请尽量更清楚。编辑问题,给出示例输入和预期输出。好的,我看到输入了。预期输出在哪里?
sed'/open/not open/
将用
not not open
替换
not open
,并将“very open”替换为“very not open”,而不是“not not other open”……请尽量弄清楚。编辑问题,给出示例输入和预期输出。好的,我看到输入了。预期的输出在哪里?
sed'/open/not open/
not open
替换为
not not open
,并将“very open”替换为“very not open”,而不是“not very open”……首先,你为什么回答一个显然不符合质量规则的问题?很明显,因为这根本不是一个问题。进一步注意,您的脚本使
无法打开
来自
无法打开
我正试图提供帮助。我没有批评海报的英语技能,而是猜测了这个问题,并试图给出一个有用的、有教育意义的答案。我相信你是有诚意的,但你看到这没有多大帮助。最好是要求更多的信息,并等待,除非它将提供(或不提供)。。顺便说一句,我不会批评糟糕的英语,因为我的英语也大多是不正确的。但是,我认为这个问题的质量问题并不是由英语引起的。@AdamKatz谢谢你,先生,我只想替换这个单词,打印原文,在替换完这个单词后再打印出来word@odishoeivazi:如果这是你想要的,请接受它作为答案。否则,请澄清你的问题。首先,你为什么回答一个明显不符合质量规则的问题?很明显,因为这根本不是一个问题。进一步注意,您的脚本使
无法打开
来自
无法打开
我正试图提供帮助。我没有批评海报的英语技能,而是猜测了这个问题,并试图给出一个有用的、有教育意义的答案。我相信你是有诚意的,但你看到这没有多大帮助。最好是要求更多的信息,并等待,除非它将提供(或不提供)。。顺便说一句,我不会批评糟糕的英语,因为我的英语也大多是不正确的。但是,我认为这个问题的质量问题并不是由英语引起的。@AdamKatz谢谢你,先生,我只想替换这个单词,打印原文,在替换完这个单词后再打印出来word@odishoeivazi:如果这是你想要的,请接受它作为答案。否则,请澄清你的问题。