# Reading data from file using awk

**URL:** <https://community.unix.com/t/reading-data-from-file-using-awk/324962>\
**Category:** Shell Programming and Scripting\
**Created:** [February 27, 2013, 12:01pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962 "2013-02-27T12:01:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristinu](https://community.unix.com/letter_avatar/kristinu/32/6_22734026db12803ebb3e059622ac3534.png) [@kristinu](https://community.unix.com/u/kristinu)\
**Post date:** [February 27, 2013, 12:01pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/1 "2013-02-27T12:01:41Z")

</div>

I have a file as below. It contains two data sets separated by \>.  
I want to pipe each data set to another program called psxy. How  
can I get the different records

Have started doing as follows but it only passes the first data set

```nohighlight
awk 'BEGIN {RS=">"};{print $0}' p.dat

```

```nohighlight
cat p.dat

1 0.1
2 1.6
3 1.3
4 1.5
>
5 0.1
6 1.6
7 1.3
8 1.5

```

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_22734026db12803ebb3e059622ac3534.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [February 27, 2013, 12:10pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/2 "2013-02-27T12:10:40Z")

</div>

Are you just trying to delete the line with "\>"? If not, post what that sample data should look like after it's been prepared for psxy.

Regards,  
Alister

---

<div class="post-metadata">

**Author:** ![kristinu](https://community.unix.com/letter_avatar/kristinu/32/6_22734026db12803ebb3e059622ac3534.png) [@kristinu](https://community.unix.com/u/kristinu)\
**Post date:** [February 27, 2013, 12:17pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/3 "2013-02-27T12:17:36Z")

</div>

I do not want to delete \>. I do not want to create 2 files and passing each one to psxy separately.

Suppose I have 2 files

```nohighlight
cat p1.dat
1 0.1
2 1.6
3 1.3
4 1.5

```

```nohighlight
cat p2.dat

5 0.1
6 1.6
7 1.3
8 1.5

```

Then calling psxy in this way

```nohighlight
psxy p1.dat ...
psxy p2.dat ...

```

As I do not want to end up with lot of files, I put the data in one file separating the data using \>.

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_22734026db12803ebb3e059622ac3534.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [February 27, 2013, 12:59pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/4 "2013-02-27T12:59:06Z")

</div>

Replace r with the record number to be extracted:

```nohighlight
awk 'BEGIN {n=1}; n>r {exit}; $0==">" {++n; next}; n==r' r=2 file

```

For modern AWK's which support RS regular expressions:

```nohighlight
awk 'NR>r {exit}; NR==r {printf "%s", $0}' RS='>\n' r=2 file

```

If portability is a factor, use the first suggestion.

Regards,  
Alister

---

<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:** [February 27, 2013, 4:02pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/5 "2013-02-27T16:02:43Z")

</div>

kristinu, does psxy accept - as a filename indicating that it should read from standard input?  
If it does, you can execute psxy inside an awk script feeding it the portions of your input file between the ( `>` ) separator line without creating intermediate files.

---

<div class="post-metadata">

**Author:** ![kristinu](https://community.unix.com/letter_avatar/kristinu/32/6_22734026db12803ebb3e059622ac3534.png) [@kristinu](https://community.unix.com/u/kristinu)\
**Post date:** [February 27, 2013, 7:17pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/6 "2013-02-27T19:17:24Z")

</div>

I have done like and works good

```nohighlight
awk 'NR>r {exit}; NR==r {printf "%s", $0}' RS='>\n' r=2 p.dat \
  | psxy -JX4/4 -R0.3/0.6/0/1.2 -B0.1f0.05:"":/a0.2f0.1:"y":/a0.2f0.1:."":WSne \
         -m -K > p.ps

```

Don, how can I do as you say. Currently I have to loop through the records one by one changing the value of r.

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_22734026db12803ebb3e059622ac3534.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [February 27, 2013, 9:01pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/7 "2013-02-27T21:01:47Z")

</div>

> [@kristinu](#):
>
> Don, how can I do as you say. Currently I have to loop through the records one by one changing the value of r.

Redirect the awk printf statement to a pipe. Make sure to call close() after each print or all records will be sent to the same instance of psxy.

Regards,  
Alister

---

<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:** [February 28, 2013, 1:17am UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/8 "2013-02-28T01:17:11Z")

</div>

> [@kristinu](#):
>
> I have done like and works good
> 
> ```plaintext
> awk 'NR>r {exit}; NR==r {printf "%s", $0}' RS='>\n' r=2 p.dat \
> | psxy -JX4/4 -R0.3/0.6/0/1.2 -B0.1f0.05:"":/a0.2f0.1:"y":/a0.2f0.1:."":WSne \
> -m -K > p.ps
> 
> ```
> 
> Don, how can I do as you say. Currently I have to loop through the records one by one changing the value of r.

kristinu,  
Sorry for the delay in responding. Hopefully, alister's comments were enough to explain what I was suggesting. If not, the following should work. My script is a little more complex than your original script because the awk I'm using doesn't allow multiple characters in RS. So, you can simplify it on your system, but the following is more portable to other systems.

```plaintext
awk 'NR == 1 || $1 == ">" {
        if(r++) close(cmd)
        cmd = sprintf("%s %s > p%03d.ps", "psxy -JX4/4 -R0.3/0.6/0/1.2",
                "-B0.1f0.05:\"\":/a0.2f0.1:\"y\":/a0.2f0.1:.\"\":WSne -m -K", r)
        if(NR > 1) next
}
{ print | cmd
}' p.dat

```

If you wanted to run this on a Solaris/SunOS system, you'd need to use `/usr/xpg4/bin/awk` or `nawk` instead of `awk` .

Note that this script produces files `p001.ps` , `p002.ps` , etc. If you have less than 100 sections or more than 1000 sections in `p.dat` , you could change the `%03d` in the `sprintf()` format string to specify a different number of digits in the output file names.

Hope this helps,  
Don

---

<div class="post-metadata">

**Author:** ![kristinu](https://community.unix.com/letter_avatar/kristinu/32/6_22734026db12803ebb3e059622ac3534.png) [@kristinu](https://community.unix.com/u/kristinu)\
**Post date:** [February 28, 2013, 7:45am UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/9 "2013-02-28T07:45:51Z")

</div>

I want to append the psxy to the same ps file

---

<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:** [February 28, 2013, 10:49am UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/10 "2013-02-28T10:49:17Z")

</div>

> [@kristinu](#):
>
> I want to append the psxy to the same ps file

OK. So try:

```plaintext
rm p.ps 2> /dev/null
awk 'NR == 1 || $1 == ">" {
        if(cmd != "") close(cmd)
        cmd = sprintf("%s %s >> p.ps", "psxy -JX4/4 -R0.3/0.6/0/1.2",
                "-B0.1f0.05:\"\":/a0.2f0.1:\"y\":/a0.2f0.1:.\"\":WSne -m -K")
        if(NR > 1) next
}
{ print | cmd
}' p.dat

```

Delete the rm command if you want this to add to an existing `p.ps` file rather than creating a new one for each `p.dat` file.

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_22734026db12803ebb3e059622ac3534.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [February 28, 2013, 6:30pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/11 "2013-02-28T18:30:27Z")

</div>

> [@don cragun](#):
>
> ```plaintext
> rm p.ps 2> /dev/null
> awk 'NR == 1 || $1 == ">" {
> if(cmd != "") close(cmd)
> cmd = sprintf("%s %s >> p.ps", "psxy -JX4/4 -R0.3/0.6/0/1.2",
> "-B0.1f0.05:\"\":/a0.2f0.1:\"y\":/a0.2f0.1:.\"\":WSne -m -K")
> if(NR > 1) next
> }
> { print | cmd
> }' p.dat
> 
> ```

I have not tested it, but it looks like that code will send a "\>" into the pipe if the first "dataset" is empty. Also, if a "dataset" is empty, should psxy be invoked and immediately read EOF? If so, then the above is incorrect in that respect as well.

The problem statement was insufficiently precise to make those determinations, so neither case may be of concern. Just an observation.

Regards,  
Alister

---

<div class="post-metadata">

**Author:** ![kristinu](https://community.unix.com/letter_avatar/kristinu/32/6_22734026db12803ebb3e059622ac3534.png) [@kristinu](https://community.unix.com/u/kristinu)\
**Post date:** [February 28, 2013, 7:42pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/12 "2013-02-28T19:42:58Z")

</div>

I am going to go with a simple loop in bash, moving by record number

---

<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:** [February 28, 2013, 7:50pm UTC](https://community.unix.com/t/reading-data-from-file-using-awk/324962/13 "2013-02-28T19:50:00Z")

</div>

> [@alister](#):
>
> I have not tested it, but it looks like that code will send a "\>" into the pipe if the first "dataset" is empty. Also, if a "dataset" is empty, should psxy be invoked and immediately read EOF? If so, then the above is incorrect in that respect as well.
> 
> The problem statement was insufficiently precise to make those determinations, so neither case may be of concern. Just an observation.
> 
> Regards,  
> Alister

Thanks Alister,  
I considered both of these issues, but the idea of graphing zero points didn't make any sense to me. So, I didn't worry about those cases. However, I should have stated my assumptions when I posted my proposal.

- Don
