tags:

views:

24

answers:

2

Hey guys,

I tried to get some aliases from a specific config file in a short bash script. The config looks like:

[other config]

[alias]
alias: command -o option
alias2: command -l 2
alias3: command -o option

[other config]

How would you get these aliases separated? I would prefer a output like this:

alias: command -o option
alias2: command -l 2
alias3: command -o option

I already did some bad stuff like getting line numbers and so on... Any Ideas? Would be great!

A: 

Perl has a Config::GitLike module that may come in handy for parsing that git config file.

Looking at the documentation, you want to do:

#!perl -w

use strict;
use 5.010;
use Config::GitLike;

my $c = Config::GitLike->new(confname => 'config');
$c->load;

my $alias = $c->get( key => 'alias.alias' );
my $alias2 = $c->get( key => 'alias.alias2' );
my $alias3 = $c->get( key => 'alias.alias3' );

say "alias: $alias";
say "alias2: $alias2";
say "alias3: $alias3";
CanSpice
Thank you CanSpice! I got to solve this problem in my bash script and looking for some regex strings. Perhaps i can use config::gitlike to learn how to get these regex strings :)
noqqe
A: 

You can do this using sed:

sed -n -e '/\[alias\]/,/\[.*\]/s/:/:/p'

This will print all lines between [alias] and the next line containing [ and ] that have a colon on them.

Bart Sas
This works awesome! Thank you very much!
noqqe