skip to Main Content

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.

2

Answers


  1. Here is a solution:

    grep '^conn' file.txt | cut -d ' ' -f 2
    
    Login or Signup to reply.
  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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search