Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在bash中按名称打印文件?_Bash - Fatal编程技术网

如何在bash中按名称打印文件?

如何在bash中按名称打印文件?,bash,Bash,如何使用grep或cat文件并按其名称打印值? 例如:file os release-我只想打印名称和版本 NAME="Ubuntu" VERSION="14.04.3 LTS, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.3 LTS" VERSION_ID="14.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.co

如何使用grep或cat文件并按其名称打印值? 例如:file os release-我只想打印名称和版本

NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian  
PRETTY_NAME="Ubuntu 14.04.3 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

假设您的文本文件名为test.txt。然后仅对名称和版本行运行以下命令

cat test.txt | grep "NAME\|VERSION"
如您在评论中所述的完整价值

cat text.txt | grep "NAME=\"Ubuntu\"\|VERSION=\"14.04.3 LTS, Trusty Tahr\""

如果要打印包含
名称
版本
的行,可以使用:

在grepping时使用
^
可以确保单词
名称
版本
出现在行首,这样您就不会grep其他行,例如
漂亮的名称
=
确保您不会grep行,例如
VERSION\u ID

但是,如果只想打印
NAME
VERSION
的值,可以执行以下操作:

$ grep "^NAME=\|^VERSION=" /etc/*-release | grep -E -o ".[a-z]\w+"
Ubuntu
Trusty 
Tahr

您可以写成:
grep'NAME\| VERSION'test.txt
grep
可以从文件中读取输入。如何指定绝对值?我只需要'NAME=“Ubuntu”VERSION=“14.04.3 LTS,Trusty Tahr“grep”NAME \ | VERSION'/etc/os release NAME=“Ubuntu”VERSION=“14.04.3 LTS,Trusty Tahr”PRETTY_NAME=“Ubuntu 14.04.3 LTS”VERSION_ID=“14.04”'使用:
grep^\(NAME\\ VERSION\)=“yourfile
$ grep "^NAME=\|^VERSION=" /etc/*-release | grep -E -o ".[a-z]\w+"
Ubuntu
Trusty 
Tahr