1

I have a file.dev that contains the following:

1 DEVICES {
    2            GLOBAL-CONFIG {
    3               framerate = "20000";
    4               subframes = "0";
    5               max_consec_timeouts = "10";
    6               max_total_timeouts = "1000";
    7               schedmode = "Standard";
    8               clustermode = "Standard";
    9           }
    10           IO-DEVICES {
    11            }
    12           COMPUTING-DEVICES {
    13                RT_WORKSTATION FDT-C-XM-0120 = {
    14                    hostname = "FDT-C-XM-0120";
    15                    ipaddress = "fdt-c-XM-0120.fdtel.exter";
    16                    DISPLAYS {
    17                        main = "FDT-C-XM-0120:0.0";
    18                    }
    19                    SCHEDPARAM {
    20                        active = "0";
    21                        framerate = "20000";
    22                        subframes = "0";
    23                        max_consec_timeouts = "10";
    24                        max_total_timeouts = "1000";
    25                    }
    26                }
    27              
    28              RT_HOST fdt-c-agx-0008 = { 
    29                    hostname = "fdt-c-agx-0008";
    30                    ipaddress = "fdt-c-agx-0008";
    31                    SCHEDPARAM {
    32                        active = "0";
    33                        framerate = "20000";
    34                        subframes = "0";
    35                        max_consec_timeouts = "10";
    36                        max_total_timeouts = "1000";
    37                    }
    38                }
    39              
    40    #             RT_HOST fdt-c-agx-0003 = { 
    41    #                    hostname = "fdt-c-agx-0003";
    42    #                   ipaddress = "fdt-c-agx-0003.fdtel.exter";
    43    #                    SCHEDPARAM {
    44    #                        active = "0";
    45    #                        framerate = "20000";
    46    #                        subframes = "0";
    47    #                        max_consec_timeouts = "10";
    48    #                        max_total_timeouts = "1000";
    49    #                    }
    50    #                }
    51            }
    52        }

this file is located in a the directory C:/TechSAT/ADS2/config/file.dev

As you can see there are three entry for the variable hostname (line 14, 29 and 41) .

What i'm tying to do is to wrtie a bash script that find only the first entry (line 14) and save its value FDT-C-XM-0120 in a variable like $hostnameValue = FDT-C-XM-0120 for test purposes.

I actually found a semi-working solution like:

var=$(grep -r 'hostname' C:/TechSAT/ADS2/config/devices.dev)

However, this prints all matches like

         hostname = "FDT-C-VM-0120";
                    hostname = "fdt-c-agx-0008";
#                    hostname = "fdt-c-agx-0003";

and in my case the var must contain the value of the first match which is FDT-C-XM-0120

Thanks in advance

4
  • 1
    There is a option -m in GNU grep 3.6 Commented May 2, 2021 at 15:19
  • @ctac_ thanks for the command. Yes i found it. However it prints the whole line and would look like hostname = "FDT-C-VM-0120" with the spaces before and i just want the value FDT-C-VM-0120 Commented May 2, 2021 at 15:28
  • And what have you try ? Commented May 2, 2021 at 15:36
  • @ctac_ i've tried the following var1=$(grep -r 'hostname' -m1 C:/TechSAT/ADS2/config/devices.dev) and as already said the output is the whole line Commented May 2, 2021 at 15:47

1 Answer 1

1

Awk can do what you want.

awk -F'"' '/hostname/{print $2 ; exit}' file.txt

Output

FDT-C-XM-0120

With GNU grep and cut

grep -m1 'hostname' file.txt | cut -d'"' -f2

Both solutions is using a double quotes " as the delimiter, with awk -F'"' and with cut -d'"' the double quotes needs to escaped, either use a \ or enclose it with a single quote '. So it can be written as -F\" and -d\"

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks alot. it works perfectly. May i ask from where did you set the delimeter regarding awk command to get the desired output ?
@Zang322, I have added a short explanation, thanks for the comment.
Great. Thanks for the explanation. would be glad if you could vote up my question.
would be thankful if you could help me here :) stackoverflow.com/questions/67359144/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.