1

I have the following file.dev

        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        }

In this file the text parts part 1 (from line 28 till 38) and part 2 (from line 40 till 50) are parts which the user switch between. As we can see part 2 is commented out and part one is active.

So i'm trying to automate that using bash script in such a way that the user only enters the part number he wants and the other is commented out. This way the use must not comment out each line.

# example
if [ "$userEntry" = "part2"]
then
deactivate part one by typing adding from line 28 till 38 and activate part 2 by removing the # 

and the output would look like

        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        }

Note that the lines order in the file.dev do not change.

I hope i could make my question clear and thanks in advance

2
  • There is just one thing, how do the user know if either part1 or part2 is enabled? Commented May 2, 2021 at 19:07
  • Cross-site duplicate: unix.stackexchange.com/questions/128593/…. Commented May 8, 2021 at 18:52

1 Answer 1

2

With ed if it is available/acceptable.

#!/usr/bin/env bash

if [[ "$userEntry" == "part2" ]]; then
  printf '%s\n' '40,50s/^[[:blank:]]*#//' '28,38s/^/#/' ,p Q |
  ed -s file.txt
fi

Will just print the new output to stdout but the file will not be change/edited. Change Q to w if in-place editing is needed. Remove the ,p to silence the output.


With sed

sed '40,50s/^[[:blank:]]*#//;28,38s/^/#/' file.txt

Note that different sed version has different syntax when using the -i flag if in-place editing is needed.


As per the OP's explanation.

#!/usr/bin/env bash

part1=28
part2=40

if [[ "$userEntry" == "part2" ]]; then
  if [[ $(grep -nm1 \# file.txt | cut -d':' -f1) == "$part2" ]]; then
     sed '40,50s/^[[:blank:]]*#*//;28,38s/^/#/' file.txt
  else
     sed '28,38s/^/#/' file.txt
  fi
elif [[ "$userEntry" == "part1" ]]; then
  if [[ $(grep -nm1 \# file.txt | cut -d':' -f1) == "$part1" ]]; then
     sed '28,38s/^[[:blank:]]*#*//;40,50s/^/#/' file.txt
  else
     sed '40,50s/^/#/' file.txt
  fi
fi

Requites GNU grep(1)

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

8 Comments

thanks alot. I tried your code. it returns the following error test.sh: line 9: ed: command not found. Yes the change must be saved in the file
Well, ed is not installed so use sed with the -i flag/option to edit the file.
when applying sed with flag -i, it write # correctly however, in the question it is meant that if user enters part1, then the # must be removed in part one (in case its outcommented) and thus becomes active code and part2 should be commented with # and thus becomes inactive and vice versa
I have posted a new solution, try it out, I did no add the -i flag though.
perfect. you're really a gold mine. Thanks alot
|

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.