我正在尝试替换多行中的字符串。已经很晚了,我变得焦躁不安,也许有人会愿意为它争取一些SO点。我要替换的字符串是下面的SQL中的“STORED AS TEXTFILE” ...

PARTITIONED BY(load string, date string, source_file string)
STORED AS TEXTFILE
LOCATION '${staging_directory}/${tablename}';

并使其看起来像...
PARTITIONED BY(load string, date string, source_file string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION '${staging_directory}/${tablename}';

所以我的表情是
:%s/)\nSTORED AS TEXTFILE\nLOCATION '/)\rROW FORMAT DELIMITED \rFIELDS TERMINATED BY ',' \rSTORED AS TEXTFILE \rLOCATION '/g

它可以在文件中运行(使用vim),但是我无法使用它对目录中的所有文件使用一个命令。到目前为止,我已经尝试过...
sed -i "s/)\nSTORED AS TEXTFILE\nLOCATION '/)\rROW FORMAT DELIMITED \rFIELDS TERMINATED BY ',' \rSTORED AS TEXTFILE \rLOCATION '/g"

...并且我还尝试了以上语句,但所有空格均已转义。请帮忙!

最佳答案

gawk 就地编辑方法-自GNU awk 4.1.0起可用:

gawk -i inplace '$0~/STORED AS TEXTFILE/{ $0="ROW FORMAT DELIMITED" ORS "FIELDS TERMINATED BY \047,\047" ORS $0 }1' file*
  • -i inplace-每个输入文件的就地编辑
  • 10-08 00:47