0

Below is a device.dev file that contains some configuration info for a specific application called ads2.

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        }
9        IO-DEVICES {
10        }
11        COMPUTING-DEVICES {
12            RT_WORKSTATION x-NB-0144 = {
13                hostname = "x-x-0144";
14                ipaddress = "xxx.x.x.xx";
15                DISPLAYS {
16                    main = "FDT-C-VM-0094:0.0";
17                }
18                SCHEDPARAM {
19                    active = "0";
20                    framerate = "20000";
21                    subframes = "0";
22                    max_consec_timeouts = "10";
23                    max_total_timeouts = "1000";
24                }
25            }
27  
28            RT_HOST xxx-c-agx-vw-89 = { 
29                hostname = "xxxxx@xxxx-desktop";
30                ipaddress = "xx.xx.xx.xx";         
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    }

I'm trying to write a bash script that accepts an input which is an IP-address and then access the device.dev file and pass it to the variable ipaddress in line 30.

So is it possible to access devices.dev file via bash script?

Thanks in advance

2
  • "So is it possible to access devices.dev file via bash script?" - Yes, this is possible. What have you tried? What is your question? Commented Mar 22, 2021 at 21:06
  • @Tsyvarev. the question is how since I already tried ex. sed on .txt and it worked, however it didn't on .dev file. But i will try the answer by Pierluigu Commented Mar 23, 2021 at 7:11

2 Answers 2

1

Using GNU awk and focusing on the second entry only:

awk -v ipadd="1.1.1.1" '/ipaddress/ { cnt++ } /ipaddress/ && cnt==2 { lne=gensub(/(^.*\")(.*)(\".*$)/,"\\1"ipadd"\\3",$0);print lne;next }1' devices.dev > devices.tmp && mv -f devices.tmp devices.dev

Explanation:

awk -v ipadd="1.1.1.1" '/ipaddress/ {                                          # Pass the ip address to change to as a variable ipadd to awk
            cnt++                                                              # Where there is ipaddress in the line, increment a cnt variable
         } 
/ipaddress/ && cnt==2 {                                                        # Where there is ipaddress in the line and cnt is 2, process
            lne=gensub(/(^.*\")(.*)(\".*$)/,"\\1"ipadd"\\3",$0);               # Set a variable lne to an entry that substitutes the existing IP address for the one passed in ipadd using awk's gensub function
            print lne;                                                         # Print the amendment
            next                                                               # Skip to the next line
         }1' devices.dev > devices.tmp && mv -f devices.tmp devices.dev        # Print all none amended lines and redirect the output to a temp file (devices.tmp) before over writing the devices.dev file with the temp file
Sign up to request clarification or add additional context in comments.

5 Comments

This what i get mv: cannot stat ‘devices.tmp’: No such file or directory. Besides, the devices.dev file is located at c:\Techsat\ADS2\config\devices.dev
There was a "typo" on the tmp file name. Try it now.
Thanks man. You're really a gold mine. However, the file must only be located in c:\Techsat\ADS2\config\devices.dev. So can I simply refer to it by just writng the path instead of devices.dev?
It turns out its straight forward and works, just passed the path to the file. Thanks
Would be honored if i could et your contact on linkdn.)
1

Assuming you want to replace value exactly at line 30, you can create this file sed.sh in the same dir of device.dev

#!/bin/bash
IP=$1
sed -i "30s/ipaddress.*/ipaddress = \"$IP\"/" device.dev

and execute with

bash sed.sh 1.2.3.4

device.dev will have 1.2.3.4 at line 30

4 Comments

Thanks for the answer, What if the file is located in certain directory?
And the ip-adress at line 30 in which i want to set through user-input could not be located at line 30. the variable ip-adress is mentioned two times and the one to be replaced is always the second
if the file is located in another directory just hardcode the path to device.dev inside the script (you can also parametrize with $2 unix.stackexchange.com/questions/298706/…)
if the line you want to replace is always after a certain line, I think the simpler method is to specify a range in which replace apply. Try modifying the sed command with different options, like: sed -i "20,1000s/ipaddress.*/ipaddress = \"$IP\"/"" device.dev will apply from 20th to 1000th line

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.