I am attempting to parse samba .conf file for a specific share section...

    oplocks = no
    wide links = no
    writeable = yes
    delete readonly = yes
    path = /LOCALSITE/CMI
    comment = CMI Data
    write list = @cbishare 
    valid users = @cbishare
    create mode = 775
    directory mode = 775

Here is the code I am using to get the specific section using awk...

awk '/\[\<CMI\>\]/{flag=1;next}/^$/{flag=0}flag' /etc/samba/smb.conf

When I run this awk command without the code to get multiple lines {flag=1;next}/^${flag=0}flag I get just '[CMI]'.

Is there something wrong with my code to get all lines up to a newline char($)?

  • You are missing a /: '/\[\<CMI\>\]/{flag=1;next}/^$/{flag=0}flag' – NuTTyX Sep 23 '14 at 21:58
  • Yes. Just the CMI section which should consist of all lines below "[CMI]" to the next empty/newline char($). this is to be used in a script on a stripped down version of Puppy linux and to keep it very small I am not allowed to install additional packages/libraries. – Matt Pedigo Sep 23 '14 at 22:00
  • You could also divide record by double new-line and awk simply by the title: awk 'BEGIN {RS="\n\n"} /\[CMI\]/ {print}' /etc/samba/smb.conf – NuTTyX Sep 23 '14 at 22:04
  • samba conf file(smb.conf) has to use specific format where the sections are seperated by a new line. Altering the format may cause unwanted results for required samba shares. Sorry but not an option but thanks. – Matt Pedigo Sep 23 '14 at 22:10
  • Didn't get your question right. You needed the "extra" new-line char then. Using flags: awk '/\[\<CMI\>\]/{flag=1}/^$/{if (flag) print;flag=0}flag' /etc/samba/smb.conf, using RS: awk 'BEGIN {RS="\n\n";ORS="\n\n"} /\[CMI\]/ {print}' /etc/samba/smb.conf – NuTTyX Sep 23 '14 at 22:18

I found the problem after thinking about the new line char in the file and how i got the test file. The test file that i am using was copied to the machine from a windows machine using Webmin via the samba configuration function and placing that out on a windows share in which several people had open the file for examination. When copying the file back to a linux machine, all newline chars were replaced by "^m" which was causing awk to total not find any newlines. Fix with fromdos command in the tofrodos package.

Found it here... https://stackoverflow.com/questions/811193/how-to-convert-the-m-linebreak-to-normal-linebreak-in-a-file-opened-in-vim

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.