Visit Our UNIX and Linux User Community


Print previous (non zero)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print previous (non zero)
# 1  
Old 08-22-2013
Print previous (non zero)

Dear All,

I have file like this,

Code:
13819       32.07   1877.65
    0      481.81   2100.86
    0      789.35   2274.05
    0     4627.61   4421.36
    0     5831.52   4855.50
20844       38.68   1902.15
    0      291.02   1945.88
    0      452.57   2013.94
    0     2194.28   3191.02
    0     2680.83   3548.06
    0     3367.51   4140.27
    0     4627.61   4421.36
    0     5831.52   4855.50
27869       41.99   1942.75
    0      420.06   1999.14
    0      730.32   2073.40
    0     1014.72   2379.12
..... 
.....
.....

Desired output : if first column is 0 print previous value (not zero)

Code:
13819       32.07   1877.65
13819      481.81   2100.86
13819      789.35   2274.05
13819     4627.61   4421.36
13819     5831.52   4855.50
20844       38.68   1902.15
20844      291.02   1945.88
20844      452.57   2013.94
20844     2194.28   3191.02
20844     2680.83   3548.06
20844     3367.51   4140.27
20844     4627.61   4421.36
20844     5831.52   4855.50
27869       41.99   1942.75
27869      420.06   1999.14
27869      730.32   2073.40
27869     1014.72   2379.12
....
....

Thanks for advance.

Attila
# 2  
Old 08-22-2013
Code:
awk '$1{v=$1}!/$1/{$1=v}1' infile

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 08-22-2013
Good one ahamed. I guess it can further be shortened as

Code:
awk '$1{v=$1}{$1=v}1' infile

# 4  
Old 08-22-2013
Quote:
Originally Posted by krishmaths
Good one ahamed. I guess it can further be shortened as

Code:
awk '$1{v=$1}{$1=v}1' infile

We can still remove 3 characters from it Smilie

Code:
awk '$1{v=$1}$1=v' infile

--ahamed
# 5  
Old 08-22-2013
so nice Smilie
# 6  
Old 08-22-2013
Quote:
Code:
awk '$1{v=$1}!/$1/{$1=v}1' infile

--ahamed
Hello Ahamad,

Could you please explain the command here.



Thanks,
R. Singh
# 7  
Old 08-22-2013
There you go...

Code:
#!/bin/bash
awk '
        $1{v=$1}                #if there is a value in the first column ie $1, then store that value to the varable "v"
        !/$1/{$1=v}             #if the first column value is 0, then assign the first column with the value stored in "v" which is 
                                #basically from the previous record
        1                       #Print everything
' infile

--ahamed
This User Gave Thanks to ahamed101 For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #803
Difficulty: Medium
The CSS counter-increment property increases or decreases the value of one or more CSS counters.
True or False?

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. UNIX for Dummies Questions & Answers

Command to print previous year in UNIX

hi all, I use date +%Y which gives Current year. Requirement: I want previous year to be printed. Please help me. Note: I tried date +%d/%m/%Y -d "-1 years" which is not working. (10 Replies)
Discussion started by: wasim999
10 Replies

3. Shell Programming and Scripting

How to grep and print the next and previous N lines after the hit

Hello, I know that gnu grep has option of -A and -B to extra previous and next lines. But I'm using HP UX and its grep does not support these options. BID="0/0/6/1/1.145.17.255.0.0.0" I need to search a file using $BID and get next 5 lines and previous 5 lines separately. Please... (1 Reply)
Discussion started by: reddyr
1 Replies

4. Shell Programming and Scripting

Print a field from the previous line

plz help me!! I have this file , 3408 5600 3796 6035 4200 6285 4676 0 40 1554 200 1998 652 2451 864 2728 1200 0 I want it like if $2==0,replace it with field from the previous line+500 say here the o/p would be like 3408 5600 3796 6035 4200 6285... (16 Replies)
Discussion started by: Indra2011
16 Replies

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

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

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

8. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

9. Shell Programming and Scripting

Referring from the print of the previous command

Hi, I am a newbie in SHell Programming. I want to ask something about referring the result of the previous command in Shell-Prog. For example : bnm@dsds~> ifconfig eth0 Link encap:Ethernet HWaddr 00:0B:CD:85:A5:8A inet addr:192.168.0.2 Bcast:192.168.0.225 Mask... (2 Replies)
Discussion started by: bobb
2 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

Featured Tech Videos