简体   繁体   English

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

[英]How print lined from file by name in bash?

how i can grep or cat file and print values by it names ? 我怎么能grep或cat文件并按名称打印值? for example: file os-release - i want print only NAME and VERSION 例如:file os-release - 我想只打印NAME和VERSION

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/"

If you want to print the lines containing NAME and VERSION entirely, you can use grep : 如果要完全打印包含NAMEVERSION的行,可以使用grep

$ grep "^NAME=\|^VERSION=" /etc/*-release
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"

Having ^ when grepping ensures that the words NAME and VERSION appear at the beginning of the line, so that you won't grep other lines such as PRETTY_NAME . 在grepping时使用^确保单词NAMEVERSION出现在行的开头,这样您就不会grep其他行,例如PRETTY_NAME The = ensures that you won't grep lines such as VERSION_ID . =确保您不会grep诸如VERSION_ID

If you just want to print the values of NAME and VERSION , however, you can do the following: 但是,如果您只想打印NAMEVERSION的值,则可以执行以下操作:

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

Suppose your text file name is test.txt. 假设您的文本文件名是test.txt。 Then run following command for name and version lines only 然后仅对名称和版本行运行以下命令

cat test.txt | grep "NAME\|VERSION"

For complete value as you informaed in comments 您在评论中获得的完整价值

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

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

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