Awk to print data from current and previous line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk to print data from current and previous line
# 1  
Old 03-28-2011
Awk to print data from current and previous line

Hi guys,

I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr).

I am trying to process my data so I can use it to create a step graph of the convergence rate of one continent to another. Here is a sample of my raw input.

105.0 125.2
100.0 128.5
95.0 114.8
90.0 128.1

I want to use awk to get my output to look like this:

105.0 125.2
100.0 125.2
100.0 128.5
95.0 128.5
95.0 114.8
90.0 114.8
90.0 128.1

So I print the first line as is. Then I print the first column from the next record, and the second column from the first line. Then I print both columns from the second line in the raw data, and I repeat that pattern.

I would really REALLY appreciate any help on this. It's really doing my head in - been looking online everywhere but haven't been able to figure it out... Thanks guys :-) If you guys need more info, let me know! Thanks again!
# 2  
Old 03-28-2011
Code:
echo "105.0 125.2
100.0 128.5
95.0 114.8
90.0 128.1" |awk '{if(v)print $1,v;print $1,$2;v=$2}'
105.0 125.2
100.0 125.2
100.0 128.5
95.0 128.5
95.0 114.8
90.0 114.8
90.0 128.1

# 3  
Old 03-28-2011
Thankyou SOOOOOOO much yinyuemi!

I REALLY APPRECIATE IT! It worked like a charm.

I have hooked up the output to Generic Mapping Tools to create a graph.

Image

I don't know if this will be useful to others, but here is the shell script extract that I used to create the graph above.

Code:
awk '{if(v)print $1,v;print $1,$2;v=$2}' age_rate.xy > age_rate_step.xy

region=0/100/0/200

psxy age_rate_step.xy -R$region -JX-10/10 -Bf20a20:"Age (Ma)":/f20a20:"Convergence Rate (mm/yr)":WSne -P -V > outfile.ps

ps2raster outfile.ps -Tj -A

Thankyou again! I owe you big time! :-)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

4. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

5. Shell Programming and Scripting

Perl: Conditional replace based on previous and current value in a line

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies

6. Shell Programming and Scripting

AWK Compare previous value with current.

Hi, I have one small doubt how to go ahead and process the below requirement. File Content 1,abc,10 2,xyz,11 3,pqr,12 4,pqr,13 5,pqr,14 Output file expected: 1,mnq,1 1,ddd,2 1,qqq,3 1,sss,4 1,ddd,5 1,eee,6 1,fff,7 1,ddr,8 1,rrd,9 (3 Replies)
Discussion started by: dikesm
3 Replies

7. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

8. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

9. Shell Programming and Scripting

awk print the next line on the current line

Ok I have a file with hundreds of lines, four columns, space delimited, TESTB.TXT for example TESTB.TXT --- AA ZZ 12 34 BB YY 56 78 CC XX 91 23 DD VV 45 67 --- I want a new file that has 7 columns, the first four are identical, and the next 3 are the last three of the next line...so... (5 Replies)
Discussion started by: ajp7701
5 Replies

10. Shell Programming and Scripting

Print previous, current and next line using sed

Hi, how can i print the previous, current and next line using sed? current line is the matching line. The following prints all lines containing 'Failure' and also the immediate next line cat $file | sed -n -e '/Failure/{N;p;}' Now, i also want to print the previous line too. Thanks,... (8 Replies)
Discussion started by: ysrinu
8 Replies
Login or Register to Ask a Question