Sta*_*ord 8 sed text-processing
some name;another thing; random; value value value value value
Run Code Online (Sandbox Code Playgroud)
我正在尝试替换random;
使用 sed后出现的空格。some name
例如,保留其中的空间很重要。
这将用逗号替换所有空格。我如何匹配像这样的表达式*;*;*;
并将 sed 与该行的其余部分一起使用并用逗号替换空格?
sed -e 's/ /,/g'
Run Code Online (Sandbox Code Playgroud)
谢谢
Kus*_*nda 10
使用gsub()
中awk
的最后;
-delimited领域:
$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $NF); print }' file
some name;another thing; random;,value,value,value,value,value
Run Code Online (Sandbox Code Playgroud)
使用sed
并假设我们想;
用逗号替换最后一个之后的所有空格:
$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value
Run Code Online (Sandbox Code Playgroud)
注释sed
脚本:
h ; # Duplicate line into hold space
s/.*;// ; # Delete up to the last ;
y/ /,/ ; # Change space to comma in remaining data
x ; # Swap pattern and hold spaces
s/;[^;]*$// ; # Delete from the last ;
G ; # Append hold space delimited by newline
s/\n/;/ ; # Replace the embedded newline with ;
; # (implicit print)
Run Code Online (Sandbox Code Playgroud)
“保持空间”是提供的单独存储缓冲区sed
。“模式空间”是从输入中读取数据并可以对其应用修改的缓冲区。