sed - 如何使用sed交换两列?

标签 sed

我正在尝试从此更改文件中的文本:

file.txt,c:\path\to\file
file.txt,c:\path with spaces\to\file
file.txt,c:\path\to\file with spaces
file.txt,c:\path\to\file with spaces
file.txt,c:\path\to\file with spaces

进行此类输出(文件的一个路径):
c:\path\to\file\file.txt
c:\path with spaces\to\file\file.txt
c:\path\to\file with spaces\file.txt
c:\path\to\file with spaces\file.txt
c:\path\to\file with spaces\file.txt

此ALMOST可以工作,但在行的末尾需要一个',':
sed 's@\(.*\),\(.*\),\(.*\)@\2,\1,\3@g' file

任何帮助都将不胜感激,我不知道sed怎么这么好...

编辑

这对我有用,但我仍然想在其中添加“\”:
sed 's@\(.*\),\(.*\)@\2,\1,\3@g' file

最佳答案

转义反斜杠\\

sed 's/^\(.*\),\(.*\)$/\2\\\1/g' file
                       --^^--

另外,您只需要两个捕获组。

但是由于我是一个awk的人。
awk -F, '{ print $2 "\\" $1 }' file

关于sed - 如何使用sed交换两列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13808262/

相关文章:

postgresql - 删除 Postgres 转储中的换行符

regex - 在不同的行中查找一个匹配项,后跟另一个匹配项并对输出进行排序

perl - 冒号和逗号之间的 Grep 数字

linux - 使用sed从文件中提取数据并 append 到Linux中的目标文件

linux - 如何使用 sed 删除度数符号(M-0 又名上标零?)

unix - sed 替换为回车符

python - 获取重复的行和文件的其余部分 [大文件 50G]

linux - SED - 删除后跟换行 (\n) 的字符串

UNIX - 将标题添加到每行的第一列

linux - 搜索特定单词并将其从 linux 文件中删除