简体   繁体   中英

Bash - get content of file

I need to get content of file in bash but i only need specific words which are ahead of a key word.

The file looks like this:

conn NameOfConnection1
    some settings

conn NameOfConnection2
    some settings

conn NameOfConnection3
    some settings

I would need a bash script which would output only NameOfConnection1,2,3 on each line.

//EDIT:

Little catch. The file have this:

    # It is best to add your IPsec connections as separate files in /etc/ipsec.d/
include /etc/ipsec.d/*.conf

And some conns are in seperate folder.

Here is a solution:

grep '^conn' file.txt | cut -d ' ' -f 2

Using awk can print/extract the desired output.

awk '$1 == "conn" {print $2}' file.txt

the above code basically means, If field/column $1 is conn then print the second $2 field/column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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