将双引号添加到文本文件
Add double quotes to text file
我有一个由 semi-colon 分隔的文本文件,其中多行可能带有 header。我必须为文本文件中的每一列添加双引号。我尝试使用 sed 使各个部分工作。但是,当合并时,它们不会 运行.
我的命令:
sed "s/;/\";"\/g a.txt
-(用“;”替换列(例如:d;"b";"c";"f)
sed "s/^/\"/"g a.txt
-(– 替换第一列周围的双引号)
sed "s/$/\"/"g a.txt
-(替换最后一列周围的双引号)
当我将它们组合如下时:
sed "s/;/\";"\/g";s/^/\"/g";s/$/\"/ g" a.txt
这不是 运行。我只能组合上述 sed 语句中的 2 个和 运行 但不能组合所有 3 个。
你可以这样试试
sed '2,$s/\([^;]*\)/\""/g'
从第2行开始保留header。
用单引号将脚本(和变量)括起来,除非您出于特定目的绝对需要双引号,例如让 shell 扩展变量。所以你的脚本应该是(清理了一些其他东西):
sed 's/;/";"/g' a.txt - (replace columns with ";" (ex:d;"b";"c";"f)
sed 's/^/"/' a.txt - (– replace double quotes around first column)
sed 's/$/"/' a.txt - (replace double quotes around last column)
合并:
sed 's/;/";"/g; s/^/"/; s/$/"/' a.txt
在某些 seds(支持 ERE 的 seds)中可以将其减少到:
sed 's/;/";"/g; s/^\|$/"/g' a.txt
我有一个由 semi-colon 分隔的文本文件,其中多行可能带有 header。我必须为文本文件中的每一列添加双引号。我尝试使用 sed 使各个部分工作。但是,当合并时,它们不会 运行.
我的命令:
sed "s/;/\";"\/g a.txt
-(用“;”替换列(例如:d;"b";"c";"f)sed "s/^/\"/"g a.txt
-(– 替换第一列周围的双引号)sed "s/$/\"/"g a.txt
-(替换最后一列周围的双引号)
当我将它们组合如下时:
sed "s/;/\";"\/g";s/^/\"/g";s/$/\"/ g" a.txt
这不是 运行。我只能组合上述 sed 语句中的 2 个和 运行 但不能组合所有 3 个。
你可以这样试试
sed '2,$s/\([^;]*\)/\""/g'
从第2行开始保留header。
用单引号将脚本(和变量)括起来,除非您出于特定目的绝对需要双引号,例如让 shell 扩展变量。所以你的脚本应该是(清理了一些其他东西):
sed 's/;/";"/g' a.txt - (replace columns with ";" (ex:d;"b";"c";"f)
sed 's/^/"/' a.txt - (– replace double quotes around first column)
sed 's/$/"/' a.txt - (replace double quotes around last column)
合并:
sed 's/;/";"/g; s/^/"/; s/$/"/' a.txt
在某些 seds(支持 ERE 的 seds)中可以将其减少到:
sed 's/;/";"/g; s/^\|$/"/g' a.txt