Print a field from the previous line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print a field from the previous line
# 15  
Old 04-27-2011
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083

If $4 == 0 ;replace that value with previous line's ($4value+500).Thats what is needed!
Ur script is quite fine but I dont know about the confusion it has with my system.
PLease avoid 1' in the script.

The O/p shud be like
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 6785 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083
Cheers!

---------- Post updated at 05:44 AM ---------- Previous update was at 05:44 AM ----------

0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083

If $4 == 0 ;replace that value with previous line's ($4value+500).Thats what is needed!
Ur script is quite fine but I dont know about the confusion it has with my system.
PLease avoid 1' in the script.

The O/p shud be like
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 6785 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083
Cheers!
# 16  
Old 04-27-2011
You can also try

Code:
awk '$4{x=$4}!$4{x+=500;$4=x;$1=$1}1' tst

or

If you want to avoid the 1 you can also give a try to one of those and see if it works as you expect:

Code:
awk '$4{x=$4}!$4{x+=500;$4=x}{print}' tst

or
Code:
awk '$4{x=$4}!$4{x+=500;$4=x}{print $0}' tst

or

Code:
awk '$4{x=$4}!$4{x+=500;$4=x;$1=$1}{print}' tst

or
Code:
awk '$4{x=$4}!$4{x+=500;$4=x;$1=$1}{print $0}' tst

or

Code:
awk '$4{x=$4}!$4{x+=500;sub($4,x,$0)}{print}' tst

or
Code:
awk '$4{x=$4}!$4{x+=500;sub($4,x,$0)}{print $0}' tst

For me, it works withe the 1 :

Code:
# cat tst
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 4677 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 4678 0 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083
0 0 4567 0 151401.65 9486388.65 SY10068003 3524 3083

Code:
# awk '$4{x=$4}!$4{x+=500;$4=x}1' tst
0 0 3796 6035 151401.65 9486388.65 SY10068003 3444 3083
0 0 420 6285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4676 6785 151401.65 9486388.65 SY10068003 3444 3083
0 0 4677 7285 151401.65 9486388.65 SY10068003 3444 3083
0 0 4678 7785 151401.65 9486388.65 SY10068003 3444 3083
0 0 65 1564 151401.65 9486388.65 SY10068003 3524 3083
0 0 213 2100 151401.65 9486388.65 SY10068003 3524 3083
0 0 4567 2600 151401.65 9486388.65 SY10068003 3524 3083


Last edited by ctsgnb; 04-27-2011 at 09:19 AM..
# 17  
Old 04-27-2011
Hi,
It looks like whatever script I type on the command line it takes the previous command on the !$ part of the script.That need to be sorted out first.
What cud be the reason?
I am using fedora13
Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Print nth previous line after match

Please help me print nth line after match awk or sed one line command. (3 Replies)
Discussion started by: sushma123
3 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

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

4. Shell Programming and Scripting

Replace first field of a line with previous filed of the line

Hi Everyone, I have a file as below: IM2345638,sherfvf,usha,30 IM384940374,deiufbd,usha,30 IM323763822,cdejdkdnbds,theju,15 0,dhejdncbfd,us,20 IM398202038,dhekjdkdld,tj,30 0,foifsjd,u2,40 The output i need is as below IM2345638,sherfvf,usha,30... (4 Replies)
Discussion started by: usha rao
4 Replies

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

6. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

7. UNIX for Dummies Questions & Answers

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... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

8. Shell Programming and Scripting

Print the previous line

My requirement is that when ever search criteria matchs in log file, the previous line just above the search word as well as search word should be print. sample log file --03-19T11:26 xxx create version "a.sh@@/main/6" "104157 " --03-18T16:01 xxx create version "a.sh@@/main/5" ... (6 Replies)
Discussion started by: jadoo_c2
6 Replies

9. Shell Programming and Scripting

how Print previous line ..........

HELLO...I wanted to ask you, than sure know unix more than me, as I can obtain the following solution: I have a file with rows of the type: CIAO COME STAI PERCHE COME STAI CIAO COME VA ALLO CHE FACCIAMO ................. I would that if in a line is present the word (for example) " CHE... (9 Replies)
Discussion started by: fabi20
9 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