Sed打印下一行并更改它

Sed打印下一行并更改它,sed,Sed,我的档案: "gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home" "$t":"randomtext"}]} "gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home" "$t":"randomtext222"}]} "title":{"type":"text" "$t":"randomtext"} "gd$postalAddress":[{"r

我的档案:

"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"$t":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"$t":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"$t":"randomtext"}]}
我需要在邮寄后的行上更改$t,但不需要在带标题的行后更改$t

这将打印我要更改的行

sed -n '/postalAddress/ {n;p}' file.txt
这将打印我要更改的行,包括更改:

sed -n '/postalAddress/ {n;p}' file.txt | sed 's/"$t":"/CHANGE/'
但是我如何在我的文件中更改它

以下是我的文件的外观:

"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
使用
sed

sed '/postalAddress/{n;s/\$t/CHANGE/;}' file
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
使用
awk
可以执行以下操作:

awk '/postalAddress/ {print;getline;sub(/\$t/,"CHANGE")}1' file
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
将标志与awk一起使用:

awk '/postalAddress/ {f=NR+1} f==NR {sub(/\$t/,"CHANGE");f=0}1 file
使用
sed

sed '/postalAddress/{n;s/\$t/CHANGE/;}' file
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
使用
awk
可以执行以下操作:

awk '/postalAddress/ {print;getline;sub(/\$t/,"CHANGE")}1' file
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
将标志与awk一起使用:

awk '/postalAddress/ {f=NR+1} f==NR {sub(/\$t/,"CHANGE");f=0}1 file
使用
sed

sed '/postalAddress/{n;s/\$t/CHANGE/;}' file
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
使用
awk
可以执行以下操作:

awk '/postalAddress/ {print;getline;sub(/\$t/,"CHANGE")}1' file
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
将标志与awk一起使用:

awk '/postalAddress/ {f=NR+1} f==NR {sub(/\$t/,"CHANGE");f=0}1 file
使用
sed

sed '/postalAddress/{n;s/\$t/CHANGE/;}' file
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
使用
awk
可以执行以下操作:

awk '/postalAddress/ {print;getline;sub(/\$t/,"CHANGE")}1' file
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
"CHANGE":"randomtext"}]}
将标志与awk一起使用:

awk '/postalAddress/ {f=NR+1} f==NR {sub(/\$t/,"CHANGE");f=0}1 file

您确实应该使用一种理解您的数据的语言来实现这一点,这种语言看起来是JSON,所以可能是Python。但我们可以使用sed来实现:

sed '/}]}/ s/"$t":"/CHANGE/' file.txt

"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext"}]}

这取决于标点符号在您关心的行末尾的方式,而不是在您不关心的行末尾的方式。

您确实应该使用理解您的数据的语言来实现这一点,该语言似乎是JSON,所以可能是Python。但我们可以使用sed来实现:

sed '/}]}/ s/"$t":"/CHANGE/' file.txt

"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext"}]}

这取决于标点符号在您关心的行末尾的方式,而不是在您不关心的行末尾的方式。

您确实应该使用理解您的数据的语言来实现这一点,该语言似乎是JSON,所以可能是Python。但我们可以使用sed来实现:

sed '/}]}/ s/"$t":"/CHANGE/' file.txt

"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext"}]}

这取决于标点符号在您关心的行末尾的方式,而不是在您不关心的行末尾的方式。

您确实应该使用理解您的数据的语言来实现这一点,该语言似乎是JSON,所以可能是Python。但我们可以使用sed来实现:

sed '/}]}/ s/"$t":"/CHANGE/' file.txt

"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext"}]}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext222"}]}
"title":{"type":"text"
"$t":"randomtext"}
"gd$postalAddress":[{"rel":"http://schemas.google.com/g/2005#home"
CHANGErandomtext"}]}

这取决于标点符号在你关心的行末尾的方式,而不是你不关心的行。

谢谢,但我不能根据标点符号来做这件事。我能识别更改的唯一方法是根据Postladdress后的行。输出不是OP想要的,你刚才看到的是他发布的
sed
,也不是他想要的。谢谢,但我不能根据标点符号来做这件事。我能识别更改的唯一方法是根据Postladdress后的行。输出不是OP想要的,你刚才看到的是他发布的
sed
,也不是他想要的。谢谢,但我不能根据标点符号来做这件事。我能识别更改的唯一方法是根据Postladdress后的行。输出不是OP想要的,你刚才看到的是他发布的
sed
,也不是他想要的。谢谢,但我不能根据标点符号来做这件事。我能识别变化的唯一方法是根据Postladdress后的行。输出不是OP想要的,你只是查看了他发布的
sed
,而不是他想要的。