Results 1 to 5 of 5

Thread: Shell scripting challenge

  1. #1
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,152
    Thanks
    57
    Thanked
    29 times in 27 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media M350

    Shell scripting challenge

    I'm trying to write a shell script in Bash that will remove an entire section in a text file if that section exists.

    Here's an example of a file that needs to be modified:

    Code:
    [Section1]
    PropA=123
    PropB=456
    PropC=789
    
    [Section2]
    PropA=xxx
    PropB=yyy
    PropC=zzz
    
    [Section3]
    PropA=000
    PropB=111
    PropC=222
    The shell script needs to read the file and remove a specified section (defined by an input variable) and then save the modified file. So, for example, if the input was 'Section2', the resulting file would end up like this:

    Code:
    [Section1]
    PropA=123
    PropB=456
    PropC=789
    
    [Section3]
    PropA=000
    PropB=111
    PropC=222
    If the specified section does not exist then the file is not modified.

    Any ideas in how to do this with a combination of awk and sed?

  2. #2
    root Member DanceswithUnix's Avatar
    Join Date
    Jan 2006
    Location
    In the middle of a core dump
    Posts
    12,986
    Thanks
    781
    Thanked
    1,588 times in 1,343 posts
    • DanceswithUnix's system
      • Motherboard:
      • Asus X470-PRO
      • CPU:
      • 5900X
      • Memory:
      • 32GB 3200MHz ECC
      • Storage:
      • 2TB Linux, 2TB Games (Win 10)
      • Graphics card(s):
      • Asus Strix RX Vega 56
      • PSU:
      • 650W Corsair TX
      • Case:
      • Antec 300
      • Operating System:
      • Fedora 39 + Win 10 Pro 64 (yuk)
      • Monitor(s):
      • Benq XL2730Z 1440p + Iiyama 27" 1440p
      • Internet:
      • Zen 900Mb/900Mb (CityFibre FttP)

    Re: Shell scripting challenge

    Shell script?

    Unix is a way of thinking not an operating system, you should use the right tool for the job. Good shell scripts are awful to write and maintain, so most people end up using Bash scripts which are heavyweight, slow and system specific. Try a better tool if possible, I think this should be close enough for you to hack into what you want with a few Googles about the ruby language:

    Code:
    #!/usr/bin/ruby
    
    sectionName = ARGV[0]
    fileName = "datafile"
    inSection = false
    newContents = []
    
    File.open( fileName ).each() {
        | line |
    
        if line =~ /\[(.*)\]/
            inSection = ($1==sectionName)
        end
        newContents << line if !inSection
    }
    
    file = File.open( fileName, "w" )
    newContents.each() {
        | line |
    
        file.write( line )
    }
    edit: I didn't like the substring match() for seeing if you are in the correct section. The =~ regex operator has parenthesis in the regex which extracts the section name into $1 in the next block, so just compare that to the expected string.
    Last edited by DanceswithUnix; 01-09-2017 at 01:40 PM.

  3. #3
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,152
    Thanks
    57
    Thanked
    29 times in 27 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media M350

    Re: Shell scripting challenge

    Many thanks! I'll give that a go.

  4. #4
    Senior Member
    Join Date
    Aug 2013
    Location
    North Wales
    Posts
    1,849
    Thanks
    165
    Thanked
    271 times in 202 posts
    • virtuo's system
      • Motherboard:
      • Gigabyte Aorus Master X570
      • CPU:
      • Ryzen 9 5950x
      • Memory:
      • 64Gb G.Skill TridentZ Neo 3600 CL16
      • Storage:
      • Sabrent 2TB PCIE4 NVME + NAS upon NAS upon NAS
      • Graphics card(s):
      • RTX 3090 FE
      • PSU:
      • Corsair HX850 80+ Platinum
      • Case:
      • Fractal Meshify 2 Grey
      • Operating System:
      • RedStar 3, Ubuntu, Win 10
      • Monitor(s):
      • Samsung CRG90 5140x1440 120hz
      • Internet:
      • PlusNet's best, but still poor, attempt

    Re: Shell scripting challenge

    Started writing an answer using AWK, but then realised it's just a standard INI file, right?

    http://www.pixelbeat.org/programs/crudini/

    Code:
    crudini --del filename Section2
    job done. I know it's not really the purpose of the thread, but if it's something you need to use in production rather than just a code challenge, then why not leverage someone else's hard work?
    Last edited by virtuo; 01-09-2017 at 03:27 PM.

  5. #5
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,152
    Thanks
    57
    Thanked
    29 times in 27 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media M350

    Re: Shell scripting challenge

    Yeah, it's just like a normal INI file but happens to be on a Linux server. I've got a long awk line that now does the job too if anyone is interested in an awk-based solution:

    Code:
    awk 'BEGIN { FOUND=0 } /^\[Section2\]$/ {FOUND=1;next} /^\[.*\]$/ {FOUND=0} (if (FOUND==0) {print $0}; next}' input_file.cfg > input_file.cfg.new
    This will create a new file, input_file.cfg.new, based on the processed input_file.cfg. So I guess a one-liner would look like this:

    Code:
    awk 'BEGIN { FOUND=0 } /^\[Section2\]$/ {FOUND=1;next} /^\[.*\]$/ {FOUND=0} (if (FOUND==0) {print $0}; next}' input_file.cfg > input_file.cfg.new; rm -f input_file.cfg; mv input_file.cfg.new input_file.cfg

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •