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,108
    Thanks
    49
    Thanked
    26 times in 25 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 Vivid 200

    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
    7,781
    Thanks
    300
    Thanked
    738 times in 638 posts
    • DanceswithUnix's system
      • Motherboard:
      • M5A-97 EVO R2.0
      • CPU:
      • FX-8350
      • Memory:
      • 16GB ECC 1333
      • Storage:
      • 660GB Linux, 500GB Games (Win 10)
      • Graphics card(s):
      • Sapphire Nitro R9 380 4GB
      • PSU:
      • 650W Corsair TX
      • Case:
      • Antec 300
      • Operating System:
      • Fedora 24 + Win 10 Pro 64 (yuk)
      • Monitor(s):
      • Benq XL2730Z 1440p + Samsung 2343BW 2048x1152
      • Internet:
      • 80Mb/20Mb VDSL

    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,108
    Thanks
    49
    Thanked
    26 times in 25 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 Vivid 200

    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,560
    Thanks
    143
    Thanked
    211 times in 157 posts
    • virtuo's system
      • Motherboard:
      • Asus GRYPHON Z87
      • CPU:
      • i7 4790K @4.8Ghz Corsair H100i GTX
      • Memory:
      • 32Gb G.Skill TridentX 2400 @ CAS9
      • Storage:
      • Samsung 840 EVO 120Gb + Many, many HDs
      • Graphics card(s):
      • EVGA 980Ti FTW
      • PSU:
      • EVGA Supernova G2 750W
      • Case:
      • be quiet! Dark Base Pro 900 (Orange)
      • Operating System:
      • Win10, Fedora
      • Monitor(s):
      • 2x Dell U2515H 1440p DELL U3415W Ultrawide for Work
      • Internet:
      • PlusNet Unlimited 80Mb

    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,108
    Thanks
    49
    Thanked
    26 times in 25 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 Vivid 200

    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
  •