# Function and LOOP

**URL:** <https://community.unix.com/t/function-and-loop/283318>\
**Category:** Emergency UNIX and Linux Support\
**Created:** [February 23, 2011, 4:37am UTC](https://community.unix.com/t/function-and-loop/283318 "2011-02-23T04:37:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aswex](https://community.unix.com/user_avatar/community.unix.com/aswex/32/1433_2.png) [@Aswex](https://community.unix.com/u/Aswex)\
**Post date:** [February 23, 2011, 4:37am UTC](https://community.unix.com/t/function-and-loop/283318/1 "2011-02-23T04:37:02Z")

</div>

Hello,

I need help :

I've got configurations files in two differents directory. Each configuration files contains some information that I must have in order to send specific command.

I do not know how to do it. I belive I need a loop in function but I can't make it work.

Extension of configuration files are .cfg

The current directory is :

```nohighlight
_TOOLS/_CONFIG

```

The Directory were are stored the OLD configuration files is :

```nohighlight
_TOOLS/_CONFIG/_HISTO_FIC_CFG

```

Here is the contain of one configuration file

```nohighlight
#tronc_staf# /OPERATIONNEL/PSY2V4R1

#branche# ALLEGES_BEST
#branche# TRACES
#nbr_feuille_par_#branche# PHYSICAL_MODEL/FORECAST 98

#branche#supprime# RESTART_SAT
#brancheSpecifique# RESTART_SAT RESTARTICE RESTARTICE
#brancheSpecifique# RESTART_SAT RESTART RESTART

```

Info :

I must have, for each configuration file a resulat that will looks like this :

```nohighlight
/OPERATIONNEL/PSY2V4R1/ALLEGES_BEST
/OPERATIONNEL/PSY2V4R1/TRACES
/OPERATIONNEL/PSY2V4R1/PHYSICAL_MODEL/FORECAST
/OPERATIONNEL/PSY2V4R1/RESTARTICE
/OPERATIONNEL/PSY2V4R1/RESTART

```

NOTE : for #brancheSpecifique# I need the last part of the line

ex : #brancheSpecifique# RESTART\_SAT RESTART RESTART : I am keeping the last RESTART

I do not need to keep :

```nohighlight
#branche#supprime# RESTART_SAT

```

I hope you will be able to help me. I can't do it on my own. The LOOP will get all informations in ALL configuration files (current and OLD)

Thanks

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_449c533f586939b6e5f246632a46421f.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [February 23, 2011, 4:45am UTC](https://community.unix.com/t/function-and-loop/283318/2 "2011-02-23T04:45:24Z")

</div>

Using the hash # as a field separator in config file is a bad choice and may lead to confusion.  
By convention, the # is used for commenting lines, so i suggest you to choose another delimiter ( `:;,` colon in the example below) so you will stay compliant with the standard.

```nohighlight
sed 's/#/:/g' _TOOLS/_CONFIG/config.cfg > _TOOLS/_CONFIG/newconfig.cfg

```

```nohighlight
nawk '/tronc/{p=$2}/branche# /{x=p"/"$2}/Spec/{x=p"/"$NF}x{print x;x=n}' config.cfg

```

```nohighlight
# cat tst
#tronc_staf# /OPERATIONNEL/PSY2V4R1

#branche# ALLEGES_BEST
#branche# TRACES
#nbr_feuille_par_#branche# PHYSICAL_MODEL/FORECAST 98

#branche#supprime# RESTART_SAT
#brancheSpecifique# RESTART_SAT RESTARTICE RESTARTICE
#brancheSpecifique# RESTART_SAT RESTART RESTART

```

```nohighlight
# nawk '/tronc/{p=$2}/branche# /{x=p"/"$2}/Spec/{x=p"/"$NF}x{print x;x=n}' tst
/OPERATIONNEL/PSY2V4R1/ALLEGES_BEST
/OPERATIONNEL/PSY2V4R1/TRACES
/OPERATIONNEL/PSY2V4R1/PHYSICAL_MODEL/FORECAST
/OPERATIONNEL/PSY2V4R1/RESTARTICE
/OPERATIONNEL/PSY2V4R1/RESTART
#

```

---

<div class="post-metadata">

**Author:** ![Aswex](https://community.unix.com/user_avatar/community.unix.com/aswex/32/1433_2.png) [@Aswex](https://community.unix.com/u/Aswex)\
**Post date:** [February 23, 2011, 5:03am UTC](https://community.unix.com/t/function-and-loop/283318/3 "2011-02-23T05:03:05Z")

</div>

Hi ctsgnb,

Thanks for the info but I can't change this. Is the way all configurations files are made. I am not the owner of the configuration files so I can't change them.

Thanks.

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_449c533f586939b6e5f246632a46421f.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [February 23, 2011, 5:15am UTC](https://community.unix.com/t/function-and-loop/283318/4 "2011-02-23T05:15:21Z")

</div>

Ok, no pb. FYI i updated my previous post

---

<div class="post-metadata">

**Author:** ![Aswex](https://community.unix.com/user_avatar/community.unix.com/aswex/32/1433_2.png) [@Aswex](https://community.unix.com/u/Aswex)\
**Post date:** [February 23, 2011, 5:29am UTC](https://community.unix.com/t/function-and-loop/283318/5 "2011-02-23T05:29:19Z")

</div>

Thanks ctsgnb,

That's make you now my new best friend 😉

I will try this. I believe I will have to send this command line on both of directories to create a temporary file and then use a loop to read it line by line to send my specific command.

Thanks again for your help.
