regex - how to extract substring from file with multiple lines -


i trying extract os name /etc/os-release contains:

... name="os name" ... 

but when execute:

sed 's/.*name="\([^"]*\).*/\1/' /etc/os-release 

it correctly capture os name, prints other lines intead of printing captured string, why?

content of os-release file

cat /etc/os-release  name="centos linux" version="7 (core)" id="centos" id_like="rhel fedora" version_id="7" pretty_name="centos linux 7 (core)" ansi_color="0;31" cpe_name="cpe:/o:centos:centos:7" home_url="https://www.centos.org/" bug_report_url="https://bugs.centos.org/"  centos_mantisbt_project="centos-7" centos_mantisbt_project_version="7" redhat_support_product="centos" redhat_support_product_version="7" 

sed command should output "centos linux" outputs lines:

$ sed 's/.*name="\([^"]*\).*/\1/' /etc/os-release centos linux version="7 (core)" id="centos" id_like="rhel fedora" version_id="7" centos linux 7 (core) ansi_color="0;31" cpe:/o:centos:centos:7 home_url="https://www.centos.org/" bug_report_url="https://bugs.centos.org/"  centos_mantisbt_project="centos-7" centos_mantisbt_project_version="7" redhat_support_product="centos" redhat_support_product_version="7" 

you can use -n option suppress regular output , /p mode in s command print result on particular line:

sed -ne 's/^name="([^"]+)".*/\1/p' /etc/os-release  centos linux 

you can use awk:

awk -f '["=]+' '$1=="name"{print $2}' /etc/os-release  centos linux 

or using grep -op:

grep -op '^name="\k[^"]+' /etc/os-release 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -