# KSH: A PROBLEM awk SUBSTRING

**URL:** <https://community.unix.com/t/ksh-a-problem-awk-substring/282162>\
**Category:** Shell Programming and Scripting\
**Created:** [February 3, 2011, 8:26am UTC](https://community.unix.com/t/ksh-a-problem-awk-substring/282162 "2011-02-03T08:26:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GERMANICO](https://community.unix.com/letter_avatar/germanico/32/6_449c533f586939b6e5f246632a46421f.png) [@GERMANICO](https://community.unix.com/u/GERMANICO)\
**Post date:** [February 3, 2011, 8:26am UTC](https://community.unix.com/t/ksh-a-problem-awk-substring/282162/1 "2011-02-03T08:26:14Z")

</div>

Hi Gurus,  
I am working with a korn shell script to simplify some operations of creation file of spool for a Bulk-Copy. Thi is my code:

In a for cycle:

```nohighlight
gzcat ${file} |awk '{ print substr($0,1,5)";"substr($0,108,122)";"substr($0,124,131)";"substr($0,139,152)";"substr($0,154,163)";"}' >> FileBcp.bcp

```

but in output I don't get only the interested columns, but the more columns the whole line...

This is what I would want:

```nohighlight
43031;20110128080700;ALBAM01;0;20110128080700;001234567;

```

questo � quello che ottengo:

```nohighlight
 43031;20110128080700;ALBAM01;0;20110128080700;001234567;012535007124830 *TAP3V1*03314 *393291408157 *033; ; ;00 ;012535007124830 *TAP3V1*03314 *393291408157 *03314* * 1111 +; ;00 ;012535007124830 *TAP3V1*03314 *393291408157 *03314* * 1111 +010032 355680200*;

```

where is the error?

I would want to bring on an only file

---

<div class="post-metadata">

**Author:** ![aix-guy](https://community.unix.com/user_avatar/community.unix.com/aix-guy/32/2175_2.png) [@aix-guy](https://community.unix.com/u/aix-guy)\
**Post date:** [February 3, 2011, 8:33am UTC](https://community.unix.com/t/ksh-a-problem-awk-substring/282162/2 "2011-02-03T08:33:16Z")

</div>

try switching the print statement to a printf statement:

```nohighlight
gzcat ${file} |awk '{
  printf("%s;%s;%s;%s;%s;%s;\n",substr($0,1,5),substr($0,108,122),substr($0,124,
131),substr($0,139,152),substr($0,154,163))
}' >> FileBcp.bcp

```

---

<div class="post-metadata">

**Author:** ![GERMANICO](https://community.unix.com/letter_avatar/germanico/32/6_449c533f586939b6e5f246632a46421f.png) [@GERMANICO](https://community.unix.com/u/GERMANICO)\
**Post date:** [February 3, 2011, 8:50am UTC](https://community.unix.com/t/ksh-a-problem-awk-substring/282162/3 "2011-02-03T08:50:37Z")

</div>

good idea, but I get this error:

```nohighlight
 
gzcat: compressed data not read from a terminal. Use -f to force decompression.

```

---

<div class="post-metadata">

**Author:** ![aix-guy](https://community.unix.com/user_avatar/community.unix.com/aix-guy/32/2175_2.png) [@aix-guy](https://community.unix.com/u/aix-guy)\
**Post date:** [February 3, 2011, 8:57am UTC](https://community.unix.com/t/ksh-a-problem-awk-substring/282162/4 "2011-02-03T08:57:20Z")

</div>

gzcat cats a gzipped file it does not compress.  
The gzcat utility behaves just like the gunzip -c command. (On some systems, gzcat is installed as zcat

---

<div class="post-metadata">

**Author:** ![GERMANICO](https://community.unix.com/letter_avatar/germanico/32/6_449c533f586939b6e5f246632a46421f.png) [@GERMANICO](https://community.unix.com/u/GERMANICO)\
**Post date:** [February 3, 2011, 9:21am UTC](https://community.unix.com/t/ksh-a-problem-awk-substring/282162/5 "2011-02-03T09:21:04Z")

</div>

Hi,  
the command works, but the result is not correct. In output it always brings the whole line....

---

<div class="post-metadata">

**Author:** ![aix-guy](https://community.unix.com/user_avatar/community.unix.com/aix-guy/32/2175_2.png) [@aix-guy](https://community.unix.com/u/aix-guy)\
**Post date:** [February 3, 2011, 9:21am UTC](https://community.unix.com/t/ksh-a-problem-awk-substring/282162/6 "2011-02-03T09:21:43Z")

</div>

So your script worked with the gzcat but just gave bad output. But now it gives a gzcat error?  
hmmm all I did was change from print to printf in the awk should not have affected the gzcat.

I guess I may need more view of the script. and a small sample of the file.

---

<div class="post-metadata">

**Author:** ![GERMANICO](https://community.unix.com/letter_avatar/germanico/32/6_449c533f586939b6e5f246632a46421f.png) [@GERMANICO](https://community.unix.com/u/GERMANICO)\
**Post date:** [February 3, 2011, 9:46am UTC](https://community.unix.com/t/ksh-a-problem-awk-substring/282162/7 "2011-02-03T09:46:19Z")

</div>

all ok I mistook me...!!!!

Thanks
