Reading data from file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading data from file using awk
# 1  
Old 02-27-2013
Reading data from file using awk

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

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


Code:
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

# 2  
Old 02-27-2013
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

Last edited by alister; 02-27-2013 at 01:16 PM..
# 3  
Old 02-27-2013
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

Code:
cat p1.dat
1 0.1
2 1.6
3 1.3
4 1.5

Code:
cat p2.dat

5 0.1
6 1.6
7 1.3
8 1.5

Then calling psxy in this way

Code:
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 >.
# 4  
Old 02-27-2013
Replace r with the record number to be extracted:
Code:
awk 'BEGIN {n=1}; n>r {exit}; $0==">" {++n; next}; n==r' r=2 file

For modern AWK's which support RS regular expressions:
Code:
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
# 5  
Old 02-27-2013
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.

Last edited by Don Cragun; 02-27-2013 at 05:56 PM.. Reason: explain why I asked the question.
# 6  
Old 02-27-2013
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.
# 7  
Old 02-27-2013
Quote:
Originally Posted by 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
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