# Execution Problems with awk

**URL:** <https://community.unix.com/t/execution-problems-with-awk/370345>\
**Category:** Shell Programming and Scripting\
**Created:** [January 13, 2018, 7:30am UTC](https://community.unix.com/t/execution-problems-with-awk/370345 "2018-01-13T07:30:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![echo\_manolis](https://community.unix.com/letter_avatar/echo_manolis/32/6_449c533f586939b6e5f246632a46421f.png) [@echo\_manolis](https://community.unix.com/u/echo_manolis)\
**Post date:** [January 13, 2018, 7:30am UTC](https://community.unix.com/t/execution-problems-with-awk/370345/1 "2018-01-13T07:30:07Z")

</div>

Ubuntu, Bash 4.3.48

Hi,

I have this input file:

```nohighlight
a1:b2:c30:g4:h12:j7 

```

and I want this output file:

```nohighlight
a1=g4:b2=h12:c30=j7

```

I can do it this with this code:

```nohighlight
awk -F':' '{print $1"="$4":"$2"="$5":"$3"="$6"}' INPUT > OUTPUT

```

In this case I have 6 columns, I calculate manually the half number of columns (6/2=3) and I wrotte $1 + $4 ... $3 + $6

Now I want to update my code with one that do everything in automatic, considering that the number of columns of the input file is unknown.

Some additional codes...

1) `awk -F':' '{print NF; exit}' INPUT > COUNT_NF # with this I know the number of columns`

2) I need a code to do this: `NF/2= hNF ` (half of NF)

3) I think that for the main code I have to create a loop... like this

`$1 "=" $(hNF+1)` this to repeat for hNF times (i=hNF)

example...

```nohighlight
awk -F':' -v hNF=$hNF ' { for (i=1; i<=hNF; i++) print $1"="$(hNF+1)":" }' INPUT > OUTPUT

```

Could you help me please!  
Thanks

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [January 13, 2018, 8:07am UTC](https://community.unix.com/t/execution-problems-with-awk/370345/2 "2018-01-13T08:07:20Z")

</div>

You can determine hNF in awk, just before the for loop.

---

<div class="post-metadata">

**Author:** ![echo\_manolis](https://community.unix.com/letter_avatar/echo_manolis/32/6_449c533f586939b6e5f246632a46421f.png) [@echo\_manolis](https://community.unix.com/u/echo_manolis)\
**Post date:** [January 13, 2018, 8:53am UTC](https://community.unix.com/t/execution-problems-with-awk/370345/3 "2018-01-13T08:53:02Z")

</div>

I don't know how... I'm not expert on this... I took the code from other sites :-/

```nohighlight
awk -F':' '{print NF; exit}' INPUT | awk '{print $1 / 2}' > halfcount

aaa=$(cat halfcount)

awk -F':' -v bbb=$aaa '{for (i=1; i<=aaa; i++) {print ($i)"="$(aaa+$i)":"} }' INPUT > OUTPUT

```

does not work :-/

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [January 13, 2018, 10:50am UTC](https://community.unix.com/t/execution-problems-with-awk/370345/4 "2018-01-13T10:50:08Z")

</div>

Here are a couple of options you could try:

```nohighlight
awk '{hNF=NF/2; s=""; for(i=1; i<=hNF; i++) s=s (i==1?"":OFS) $i "=" $(hNF+i); print s}' FS=: OFS=: file

```

```nohighlight
awk '{hNF=NF/2; for(i=1; i<=hNF; i++) printf "%s=%s%s",$i,$(hNF+i),(i<hNF)?OFS:ORS}' FS=: OFS=: file

```

```nohighlight
awk '{n=split($0,F); $0=""; for(i=1; i<=n/2; i++) $i=F "=" F[i+n/2] }1' FS=: OFS=: file

```

---

<div class="post-metadata">

**Author:** ![echo\_manolis](https://community.unix.com/letter_avatar/echo_manolis/32/6_449c533f586939b6e5f246632a46421f.png) [@echo\_manolis](https://community.unix.com/u/echo_manolis)\
**Post date:** [January 14, 2018, 10:12am UTC](https://community.unix.com/t/execution-problems-with-awk/370345/5 "2018-01-14T10:12:37Z")

</div>

is perfect! thank you !!!

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [January 14, 2018, 10:35am UTC](https://community.unix.com/t/execution-problems-with-awk/370345/6 "2018-01-14T10:35:44Z")

</div>

Hello echo manolis,

Following `sed` solution may also help you in same in case your Input\_file has 6 columns(mentioned by you in your post).

```nohighlight
sed 's/\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\)/\1=\4:\2=\5:\3=\6/' Input_file

```

Output will be as follows.

```nohighlight
a1=g4:b2=h12:c30=j7

```

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![echo\_manolis](https://community.unix.com/letter_avatar/echo_manolis/32/6_449c533f586939b6e5f246632a46421f.png) [@echo\_manolis](https://community.unix.com/u/echo_manolis)\
**Post date:** [January 14, 2018, 7:36pm UTC](https://community.unix.com/t/execution-problems-with-awk/370345/7 "2018-01-14T19:36:32Z")

</div>

Post solved
