Value of previous row using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Value of previous row using awk
# 1  
Old 02-18-2005
Value of previous row using awk

I would like to know value for previous row, in addition to current row.

How would I will get value for previous row? How can I perform this with awk?
# 2  
Old 02-18-2005
what is "value for previous row"?
if you mean "the previous row", just keep saving "rows" in a variable like so:
previous=$0
# 3  
Old 02-18-2005
i prefer grep and sed to solve this problem.

Code:
a=`grep -n "bhargav" file1 | cut -d":" -f 1`
((b=a-1))
if test $b -ne 0
then
sed -n ""$b","$a"p" file1
else
sed -n '1 p' file1
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fill column from previous row

Hi, I have the following content in file ABBR DESC COL3 COL4 COL5 COL6 AAA text desc aaa text text text text text text text text text text text text BBB text desc bbb text text text text text text text text CCC ... (10 Replies)
Discussion started by: bobbygsk
10 Replies

2. Shell Programming and Scripting

Extract row grater than 3 from previous value of a field

Hi, I am trying to extract data where first field unique value and 4th field of next row is grater than first row 4th field. input data is a below: 7035719974,20-jul-2016 07:42:51,07:42:51,074251,1 7035719974,20-jul-2016 07:43:57,07:43:57,074357,2 7399206761,20-jul-2016... (2 Replies)
Discussion started by: rramkrishnas
2 Replies

3. Shell Programming and Scripting

Any solution to get previous line in awk

Hi all... Is there any solution to do the reverse process of getline ? Thanks in advance. (5 Replies)
Discussion started by: Akshay Hegde
5 Replies

4. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Insert row without deleting previous data using sed

Hello, I want to add a new row to a file to insert data without deleting the previous data there. Example: file a b c d Output a b newtext c (6 Replies)
Discussion started by: joseamck
6 Replies

7. 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

8. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

9. Shell Programming and Scripting

remove row if string is same as previous row

I have data like: Blue Apple 6 Red Apple 7 Yellow Apple 8 Green Banana 2 Purple Banana 8 Orange Pear 11 What I want to do is if $2 in a row is the same as $2 in the previous row remove that row. An identical $2 may exist more than one time. So the out file would look like: Blue... (4 Replies)
Discussion started by: dcfargo
4 Replies

10. Shell Programming and Scripting

awk - testing a field for a previous value

Hi, Is their anyway to compare the value of a field in one line with the previous line? I would like to if previous volume name is the same then subtract the current value of $4 with the previous value of $4 data file host date volume mb... (1 Reply)
Discussion started by: jmd2004
1 Replies
Login or Register to Ask a Question