Reading data from file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading data from file using awk
# 8  
Old 02-28-2013
Quote:
Originally Posted by kristinu
I have done like and works good

Code:
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.
Code:
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
# 9  
Old 02-28-2013
I want to append the psxy to the same ps file
# 10  
Old 02-28-2013
Quote:
Originally Posted by kristinu
I want to append the psxy to the same ps file
OK. So try:
Code:
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.
# 11  
Old 02-28-2013
Quote:
Originally Posted by Don Cragun
Code:
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
# 12  
Old 02-28-2013
I am going to go with a simple loop in bash, moving by record number
# 13  
Old 02-28-2013
Quote:
Originally Posted by 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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

2. Shell Programming and Scripting

awk - sed / reading from a data file and doing algebraic operations

Hi everyone, I am trying to write a bash script which reads a data file and does some algebraic operations. here is the structure of data.xml file that I have; 1 <data> 2 . 3 . 4 . 5 </data> 6 <data> 7 . 8 . 9 . 10</data> etc. Each data block contains same number of lines (say... (4 Replies)
Discussion started by: hayreter
4 Replies

3. Shell Programming and Scripting

reading data from a file to an array

I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print... (1 Reply)
Discussion started by: gavin_L
1 Replies

4. Shell Programming and Scripting

Reading XML data in a FLAT FILE

I have a requirement to read the xml file and split the files into two diffrent files in Unix shell script. Could anyone please help me out with this requirement. Sample file --------------- 0,<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Information... (3 Replies)
Discussion started by: kmanivan82
3 Replies

5. Shell Programming and Scripting

Reading and analysing data in a text file

Hi, I have below type of data in a text file in unix. Emp_Name Emp_Dept Raj 101 Amruta 100 Shilpa 100 Rohit 123 Amol 198 Rosh 101 Gaurav 198 Number of employees can be even more. Need a command or a... (2 Replies)
Discussion started by: rajneesh_kapoor
2 Replies

6. Shell Programming and Scripting

Perl: Reading data from other file

Hi, I am writting some perl scripts for daily backup process. In which I want to pass some data/referance from another txt file. Text file contains only one column and multiple rows. I want to pass this data to variables of another perl script. e.g. Refdoc.txt file contains data as: perl1... (3 Replies)
Discussion started by: n.dba
3 Replies

7. Shell Programming and Scripting

Reading data from a file through shell script

There is one Text file data.txt. Data within this file looks like: a.sql b.sql c.sql d.sql ..... ..... want to write a shell script which will access these values within a loop, access one value at a time and store into a variable. can anyone plz help me. (2 Replies)
Discussion started by: Dip
2 Replies

8. Shell Programming and Scripting

reading data from .ini file

Hi, I have a sample data file that contains name-value pairs in it. For example: name1=value1 name2=value2 name3=value3 ... ... ... My concern is 1. How to get values of name1, name2, name3 using korn shell script? 2. I don't want to access each varible using $name1,$name2, $name3... (2 Replies)
Discussion started by: vinna
2 Replies

9. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies
Login or Register to Ask a Question