[英]sed command in ubuntu
I want to find the USB PORT of the device connected to the machine. 我想找到连接到机器的设备的USB端口。 I used the command
我用的命令
dmseg | grep "ttyUSB" | grep "attached"
I got the output as 我得到的输出为
[ 525.763315] usb 1-1: FTDI USB Serial Device converter now attached to ttyUSB0
[ 525.796039] usb 1-1: FTDI USB Serial Device converter now attached to ttyUSB1
But I need the port only. 但是我只需要端口。 SO I used the command
所以我用了命令
cut -d ' ' -f14
I got 我有
ttyUSB0
ttyUSB1
I want to replace the value of a file with this output.So i used the se command 我想用此输出替换文件的值。所以我用了se命令
sed -i "s/\b\SERIAL_INTERFACE=\b.*/SERIAL_INTERFACE=$(dmesg | grep "ttyUSB" | grep "attached" | cut -d ' ' -f13)/g" /home/ubuntu/webserver/properties.cfg
But it shows the error sed: -e expression #1, char 46: unterminated `s' command 但它显示错误sed:-e表达式#1,字符46:未终止的s命令
Help me to figure out this. 帮我弄清楚这个。
Thank you in advance. 先感谢您。
Replace: 更换:
sed -i "s/\b\SERIAL_INTERFACE=\b.*/SERIAL_INTERFACE=$(dmesg | grep "ttyUSB" | grep "attached" | cut -d ' ' -f13)/g" /home/ubuntu/webserver/properties.cfg
with: 有:
sed -i "s/\bSERIAL_INTERFACE=\b.*/SERIAL_INTERFACE=$(dmesg | grep "ttyUSB" | grep "attached" | cut -d ' ' -f14| tr '\n' ' ' | tee save.tmp)/g" /home/ubuntu/webserver
The problem was that the pipeline produced multiple output lines. 问题是管道产生了多条输出线。 The solution is to add
tr '\\n' ' '
to remove the newlines. 解决方案是添加
tr '\\n' ' '
以删除换行符。
Four other comments: 其他四条评论:
The S
in SERIAL_INTERFACE was escaped for no apparent reason. SERIAL_INTERFACE中的
S
没有明显原因被转义。 I removed that escape. 我删除了那个逃生路线。
You reported success with the command cut -d ' ' -f14
but the pipeline command used cut -d ' ' -f13
. 您报告了命令
cut -d ' ' -f14
成功,但是管道命令使用了cut -d ' ' -f13
。
As mentioned in the comments, you do have quotes within quotes but that is just fine: the inner quotes are inside of $(...)
and thus do not interfere with the outer quotes. 如评论中所述,您确实在引号内有引号,但这很好:内部引号位于
$(...)
内部,因此不会干扰外部引号。
The output of this command looks like: 该命令的输出如下所示:
SERIAL_INTERFACE=ttyUSB0 ttyUSB1
Since you haven't said what your desired output is, I don't know if this is what you want or not. 由于您尚未说出所需的输出,因此我不知道这是否是您想要的。
s
command" error? s
command”错误? Suppose we have a shell variable that contains a newline: 假设我们有一个包含换行符的shell变量:
$ echo "$string"
a
b
When you substitute this variable into a sed command, sed sees the newline character as terminating a line which terminates the command. 当您将此变量替换为sed命令时,sed会将换行符视为终止命令的行。 The result is an "unterminated
s
command": 结果是一个“ unsminated
s
命令”:
$ echo hi | sed "s/hi/$string"
sed: -e expression #1, char 6: unterminated `s' command
By contrast, without the newline character, it works fine: 相比之下,没有换行符,它可以正常工作:
$ string="a b"
$ echo hi | sed "s/hi/$string/"
a b
In summary, when substituting shell variables into sed
commands, one has to be very careful. 总之,在将shell变量替换为
sed
命令时,必须非常小心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.