# Setting the number of fields using awk

**URL:** <https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963>\
**Category:** Shell Programming and Scripting\
**Created:** [June 9, 2017, 7:11am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963 "2017-06-09T07:11:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gull04](https://community.unix.com/user_avatar/community.unix.com/gull04/32/14972_2.png) [@gull04](https://community.unix.com/u/gull04)\
**Post date:** [June 9, 2017, 7:11am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/1 "2017-06-09T07:11:00Z")

</div>

Hi Guys,

I've obviously had a senior moment here, what I'm trying to do is set the number of fields to 35 in a csv these should be appended to the end of the line. But what I'm getting is:-

Source Data

```nohighlight
[davem@deneb data]$ head out_file_01.txt
N1000,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,,5,2,V,50003414,,,,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,1T,5,2,V,50003414,,20090602,,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,14,50003414,,20091118,,,,,,20110930,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,20110930,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,25,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,V,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,01032549,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,41,5,2,V,50003414,,20111201,,,,,,20140430,01032549,

```

What I tried was a nice simple awk command - I'm not great at awk but this was the attempt.

```nohighlight
[davem@deneb data]$ head out_file_01.txt | awk -F"," '{NF=35}1' OFS=","
,,,,,,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,,5,2,V,50003414,,,,,,,,
,,,,,,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,1T,5,2,V,50003414,,20090602,,,,,,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,14,50003414,,20091118,,,,,,20110930,
,,,,,,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,20110930,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,25,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,V,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,
,,,00,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,01032549,
,,,00,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,41,5,2,V,50003414,,20111201,,,,,,20140430,01032549,
[davem@deneb data]$ 

```

I know that this is my stupidity - but I'm unsure how I've got it so wrong in such a simple statement.

Regards

Gull04

---

<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:** [June 9, 2017, 7:15am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/2 "2017-06-09T07:15:28Z")

</div>

Hello gull04,

Could you please try following and let me know if this helps you.

```nohighlight
awk -F, '{for(i=1;i<=35;i++){printf("%s%s",$i,i==35?"":",")};print ""}' Input_file

```

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![gull04](https://community.unix.com/user_avatar/community.unix.com/gull04/32/14972_2.png) [@gull04](https://community.unix.com/u/gull04)\
**Post date:** [June 9, 2017, 7:24am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/3 "2017-06-09T07:24:50Z")

</div>

Hi Ravinder,

What we get now is;

```nohighlight
[davem@deneb data]$ awk -F, '{for(i=1;i<=35;i++){printf("%s%s",$i,i==35?"":",")};print ""}' out_file_01.txt | head -10
,,,,,,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,,5,2,V,50003414,,,,,,,,
,,,,,,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,1T,5,2,V,50003414,,20090602,,,,,,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,14,50003414,,20091118,,,,,,20110930,
,,,,,,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,20110930,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,25,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,V,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,
,,,00,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,01032549,
,,,00,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,41,5,2,V,50003414,,20111201,,,,,,20140430,01032549,
[davem@deneb data]$ 

```

I have done this before, it was a very simple awk command - just the old grey matter doesn't seem to have the same recall any more.

Regards

Gull04

---

<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:** [June 9, 2017, 7:30am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/4 "2017-06-09T07:30:36Z")

</div>

Hello gull04,

IMHO if I understood your question correctly then you required to have 35 fields in each line? If yes then I think above code is doing the trick, see following.

```nohighlight
awk -F, '{for(i=1;i<=35;i++){printf("%s%s",$i,i==35?"":",")};print ""}' Input_file > Input_file_temp

awk -F, '{print NF}' Input_file_temp
35
35
35
35
35
35
35
35
35
35

```

Kindly do let me know if any queries, will try my best to help and learn.

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![gull04](https://community.unix.com/user_avatar/community.unix.com/gull04/32/14972_2.png) [@gull04](https://community.unix.com/u/gull04)\
**Post date:** [June 9, 2017, 7:49am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/5 "2017-06-09T07:49:44Z")

</div>

Hi Ravinder,

You are absolutely right the code is generating 35 fields - by removing data at the begining of the line and replacing each character removed with the separator. What I would actually like is the data to remain intact and the additional commas to be added to the end of the line.

So the first line;

```nohighlight
N1000,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,,5,2,V,50003414,,,,,,,,

```

Would become;

```nohighlight
N1000,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,,5,2,V,50003414,,,,,,,,,,,,,,

```

With the new fields at the end of the line, instead of at the start of the line;

```nohighlight
,,,,,,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,,5,2,V,50003414,,,,,,,,

```

Regards

Gull04

---

<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:** [June 9, 2017, 9:15am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/6 "2017-06-09T09:15:46Z")

</div>

Hello gull04,

Could you please try following and let me know if this helps you.

```nohighlight
awk -F, '{q=35-NF;val=substr($0,q+1);if(q){while(i<q){val=","val;i++}};i="";print val}' Input_file

```

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![gull04](https://community.unix.com/user_avatar/community.unix.com/gull04/32/14972_2.png) [@gull04](https://community.unix.com/u/gull04)\
**Post date:** [June 9, 2017, 9:34am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/7 "2017-06-09T09:34:48Z")

</div>

Hi Ravinder,

No change I'm afraid;

```nohighlight
[davem@deneb data]$ awk -F, '{q=35-NF;val=substr($0,q+1);if(q){while(i<q){val=","val;i++}};i="";print val}' out_file_01.txt | head -10
,,,,,,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,,5,2,V,50003414,,,,,,,,
,,,,,,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,1T,5,2,V,50003414,,20090602,,,,,,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,14,50003414,,20091118,,,,,,20110930,
,,,,,,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,20110930,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,25,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,V,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
,,,,0,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,
,,,00,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,01032549,
,,,00,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,41,5,2,V,50003414,,20111201,,,,,,20140430,01032549,

```

Regards

Gull04

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [June 9, 2017, 9:38am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/8 "2017-06-09T09:38:57Z")

</div>

I bet you have a DOS line terminator (\<CR\>, ^M, \r, 0x0D) in there ...

---

<div class="post-metadata">

**Author:** ![gull04](https://community.unix.com/user_avatar/community.unix.com/gull04/32/14972_2.png) [@gull04](https://community.unix.com/u/gull04)\
**Post date:** [June 9, 2017, 10:56am UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/9 "2017-06-09T10:56:59Z")

</div>

Hi Rudi,

You're almost correct, hidden away in the file was the following;

```nohighlight
[davem@deneb data]$ dos2unix < out_file_01.txt > out_file_02.txt
dos2unix: Binary symbol 0x00 found at line 5560351
dos2unix: Skipping binary file stdin
[davem@deneb data]$ vi out_file_01.txt
[davem@deneb data]$ dos2unix < out_file_01.txt > out_file_02.txt
[davem@deneb data]$ head out_file_02.txt | awk -F"," '{NF=35}1' OFS=","
N1000,024,2809003,,,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,,5,2,V,50003414,,,,,,,,,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,1T,5,2,V,50003414,,20090602,,,,,,,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,14,50003414,,20091118,,,,,,20110930,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20090520,20090520,32,5,2,V,50003414,,20091118,,,,,,20110930,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,25,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,1T,5,2,V,50003414,,20130711,,,,,,20140430,01032549,,,A0070C,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,22812-0007,1,165,US,20110411,20090520,32,5,2,V,50003414,,20091118,,,,,,20140430,01032549,,,,
N1000,024,2809003,52215,1985,3,DYNAMIC AVLEASE INC,PO BOX 7,,BRIDGEWATER,VA,228120007,1,165,US,20110411,20090520,41,5,2,V,50003414,,20111201,,,,,,20140430,01032549,,,,
[davem@deneb data]$ 

Such is life, I was sure that I'd already ran dos2unix on it - but it would seem not - I hate the way that vi now suppresses the control characters.

Anyway many thanks to everyone for the assistance.

Regards

Gull04

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [June 9, 2017, 3:37pm UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/10 "2017-06-09T15:37:36Z")

</div>

In most versions of `vi` , even if it doesn't usually show the \<carriage-return\> characters, you might notice that when you first open a file for editing, there is a note in the status line just after you open the file similar to the following:

```nohighlight
"file" [dos] 2L, 8C

```

when the file you just opened has DOS \<carriage-return\>\<newline\> line separators instead of UNIX \<newline\> line terminators.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [June 9, 2017, 4:04pm UTC](https://community.unix.com/t/setting-the-number-of-fields-using-awk/366963/11 "2017-06-09T16:04:20Z")

</div>

You may also want to run the file command:

```nohighlight
file fi*
file1: ASCII text, with CRLF line terminators
file1~: ASCII text, with CRLF, CR, LF line terminators
file2: ASCII text

```
