Johny Wave
Johny Wave

Reputation: 141

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.

Upvotes: 0

Views: 490

Answers (2)

Jetchisel
Jetchisel

Reputation: 7801

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.

Upvotes: 1

Edouard Thiel
Edouard Thiel

Reputation: 6228

Here is a solution:

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

Upvotes: 2

Related Questions