0

I have one project's .git/config file in which I have following contents:

[user]
    name = <FullName>
    email = <EmailID>
    username = <UserName>
[core]
    editor = nvim
    whitespace = fix,-indent-with-non-tab,trailing-space,cr-at-eol
    pager = delta
[web]
    browser = google-chrome
....
....
[alias]
    a = add --all
    ai = add -i
    #############
    ap = apply
    as = apply --stat
    ac = apply --check
....
....
[filter "lfs"]
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    process = git-lfs filter-process
    required = true

Now I want to copy multiple sections and their contents like [alias], [filter "lfs"] etc. but not the [user] and [web] sections from this .gitconfig file to another project's (which is under same parent as this project's directory) .gitconfig file.

Now I know I can loop thro' and print lines of this file, but not idea how to write multiple sections to another file(with less code clutter as possible) such that it doesn't overwrite target file's original contents:

while read line || [ -n "$line" ]; do echo $line; done < $filename;

Help is much appreciated...

2

While you could do this via bash/awk, I'd recommend using an INI file parser. For example, you can do the following in Python using configparser library:

import sys
import configparser

def print_section(conf, section):
    print("[{}]".format(section))
    for key in conf[section]:
        print("    {} = {}".format(key, conf[section][key]))


c = configparser.RawConfigParser()
c.read('config.ini')

sections = ['alias', 'filter "lfs"']
for s in sections:
    print_section(c, s)

Oneliner of the same:

printf "import sys; import configparser; c = configparser.RawConfigParser(); c.read('config.ini'); sections = ['alias', 'filter \"lfs\"'];\nfor s in sections:\n  print(\"[{}]\".format(s));\n  for key in c[s]:\n    print(\"    {} = {}\".format(key, c[s][key]))"  | python3
8
  • Your answer in Python is good, but can you also specify how can I do this in Awk/Sed ? I would really like to add it to Shell script that I have already created
    – Vicky Dev
    Nov 25 at 17:38
  • @VickyDev It's really not worth the pain to do it in shell. If you're looking to integrate this into a script (or run from cmdline), you can run the same as a oneliner - updated the answer.
    – P.P
    Nov 25 at 18:08
  • Ok, if I write it to file using python like with open(...) and then fwrite append, how can I force newline when every next line(of section, means section title then newline and then section content line by line exact structure) is being written ?
    – Vicky Dev
    Nov 25 at 18:10
  • Right now with basic fwrite it's writing 4 spaces instead of next line with indentation, how do I solve that?
    – Vicky Dev
    Nov 25 at 18:11
  • @VickyDev The code in the answer does format the sections. Isn't that work for you?
    – P.P
    Nov 25 at 18:31

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.