我正在尝试从此更改文件中的文本:
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/