简体   繁体   English

从 bash 中的文件中获取特定文本

[英]Get specific text from file in bash

i have problem to show specific text.我无法显示特定的文字。 Example i will get only version number from text in file version.txt this: Version = "0.11.0"示例我将只从文件 version.txt 中的文本中获取版本号: Version = "0.11.0"

can someone help me?有人能帮我吗?

thanks谢谢

You want just the numbers?你只想要数字吗?

Perhaps也许

awk -F\" '/Version/ {print $2}' version.txt

You can simply use cut :您可以简单地使用cut

cat version.txt | cut -d '=' -f2 | xargs

Or awk as others suggested:或其他人建议的awk

cat version.txt | awk '{print $3}'

Both output: 0.11.0 output: 0.11.0

The logic is based on the file format being consistent rather than focusing on the number extraction.逻辑基于文件格式一致,而不是专注于数字提取。
cut : prints the value after = | cut : 打印= | 之后的值xargs : trims spaces xargs :修剪空格
awk : prints the value on third place awk :在第三位打印值

These allow for example both xxx and xxx-RELEASE例如,这些允许 xxx 和 xxx-RELEASE

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

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