简体   繁体   English

我们可以从文件中执行grep和sed并将输出推送到变量中

[英]Can we do grep and sed from a file and push the output into a variable

Trying to do grep and sed from a file and push the output into a variable 尝试从文件中执行grep和sed并将输出推送到变量中

grep "^/dev/disk/by-id/scsi-*" /tmp/disks.txt | sed '{'s/=.*//'}'

Output would be like this: 输出将是这样的:

/dev/disk/by-id/scsi-3644a8420420897001f2af8cc054d33bb
/dev/disk/by-id/scsi-3644a8420420897001ef50fcb0f778b86-part3
/dev/disk/by-id/scsi-3644a8420420897001ef50fcb0f778b86-part2

Error: 错误:

[/~]# grep "^/dev/disk/by-id/scsi-*" /tmp/disks.txt | sed {'s/=.*//'} >> $x
-bash: $x: ambiguous redirect

if i do this 如果我这样做

x=`grep "^/dev/disk/by-id/scsi-*" /tmp/disks.txt | sed {'s/=.*//'}` 

or 要么

x=$(grep "^/dev/disk/by-id/scsi-*" /tmp/disks.txt | sed {'s/=.*//'})

I get : 我明白了:

[root@localhost ~]# $x
-bash: /dev/disk/by-id/scsi-4644a8420420897001f2af8cc054d33bb: No such file or directory

Somehow, it is trying to resolve the disk path. 不知何故,它正试图解决磁盘路径。

Can we push all 3 lines into a variable? 我们可以将所有3行都推入变量吗? Thanks! 谢谢!

The below line does push the values to the variable. 下面的行确实将值推送到变量。

x=$(grep "^/dev/disk/by-id/scsi-*" /tmp/disks.txt | sed {'s/=.*//'})

But $x will execute each line in the x. $x将执行$x每一行。 Try echo $x to get the output of all the line stored in the variable. 尝试使用echo $x获取存储在变量中的所有行的输出。 Although not a good idea to store multiple line in a variable as there are chances of garbling of end line characters, you can also try to store the output to a file and read it. 尽管在变量中存储多行并不是一个好主意,因为有可能存在末尾字符的乱码,但您也可以尝试将输出存储到文件中并读取它。 This will save you formattign related errors. 这样可以节省格式化相关的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM