I want to replace the row with zero value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers I want to replace the row with zero value
# 1  
Old 04-12-2014
I want to replace the row with zero value

I have this file

Code:
5
9
7
23
0
5
7
78
0
3
7
0
5
44
9
0

i want to remove the row with zero value but i want to replace it with the above + under over 2 like this (the second zero) (78+3)/2

or simply by the above value like this replace the first zero with 23
Code:
5
9
7
23
23
5
7
78
78
3
7
7
5
44
9
9

thanks

Last edited by Scrutinizer; 04-13-2014 at 05:56 AM.. Reason: code tags
# 2  
Old 04-12-2014
Longhand using _builtins_, OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash
# no-zero.sh
> /tmp/zeros
> /tmp/nozeros
number=""
echo "5
9
7
23
0
5
7
78
0
3
7
0
5
44
9
0" > /tmp/zeros
text=$(cat < /tmp/zeros)
echo "$text"
# Start of conversion proper...
while read line
do
	if [ "$line" != "0" ]
	then
		printf "$line\n" >> /tmp/nozeros
	else
		printf "$number\n" >> /tmp/nozeros
	fi
	number="$line"
done < /tmp/zeros
# End of conversion...
echo "Results:-"
cat < /tmp/nozeros

Results:-
Code:
Last login: Sat Apr 12 21:44:35 on ttys000
AMIGA:barrywalker~> chmod 755 zeros.sh
AMIGA:barrywalker~> ./zeros.sh
5
9
7
23
0
5
7
78
0
3
7
0
5
44
9
0
Results:-
5
9
7
23
23
5
7
78
78
3
7
7
5
44
9
9
AMIGA:barrywalker~> _

# 3  
Old 04-12-2014
Try this for your first request. Note that this of course does not work for the last line being 0:
Code:
awk     '               {C= NR   %3
                         B=(NR-1)%3
                         A=(NR-2)%3
                         T[C]=$0}
         !(T[B])        {T[B]=(T[A]+T[C])/2}
         NR>2           {print T[A]}
         END            {print T[B]
                         print T[C]}
        ' file
5
9
7
23
14
5
7
78
40.5
3
7
6
5
44
9
0

# 4  
Old 04-12-2014
For my amusement, I composed this snippet which replaces a zero with the average of its surrounding values (a zero on the first/last line is treated as if it were preceded/followed by another 0):
Code:
paste <(echo; sed '$d' file) file <(tail -n +2 file) | awk -F'\t' '{print ($2 ? $2 : ($1+$3)/2)}'

Regards,
Alister
# 5  
Old 04-13-2014
To get the sum of prevous and following number by 2, with float values
Code:
awk '{a[NR] = $0} END {for(i = 1; i <= NR; i++) {if(a[i] == 0) {a[i] = (a[(i-1)] + a[(i+1)]) / 2}; print a[i]}}' file

To get only decimal values for above
Code:
awk '{a[NR] = $0} END {for(i = 1; i <= NR; i++) {if(a[i] == 0) {a[i] = (a[(i-1)] + a[(i+1)]) / 2}; printf "%d\n", a[i]}}' file

To replace '0' with the number above it
Code:
awk '{a[NR] = $0} END {for(i = 1; i <= NR; i++) {if(a[i] == 0) {a[i] = a[(i-1)]}; print a[i]}}' file

or
Code:
awk 'NR == 1 {p = $0; next} {print p; if($0 != 0) {p = $0}} END {print p}' file

# 6  
Old 04-13-2014
Replace 0 with above + under over 2:
Code:
awk 'function pr(){print p?p:(q+$1)/2} NR>1{pr()} {q=p; p=$1} END{pr()}' file

Replace 0 with value above it:
Code:
awk '!$1{$1+=p}{p=$1}1' file


Last edited by Scrutinizer; 04-13-2014 at 06:26 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

3. Shell Programming and Scripting

Replace First Column and First Row Data

HI Guys, I just want to replace data for First Column and Row Cell(1,1) Input :- Hello A B C X 1 2 3 Y 4 5 6 Z 7 8 9 Output:- Byee A B C X 1 2 3 Y 4 5 6 Z 7 8 9 From Hello to Byee .....And The Each file have Different String. (3 Replies)
Discussion started by: pareshkp
3 Replies

4. Shell Programming and Scripting

Replace missing row with 0

Hi all, i have a file which contains something as below: INPUT 00:00 0 01:00 0 02:00 0 03:00 0 04:00 0 05:00 0 06:00 0 07:00 0 08:00 0 09:00 0 10:00 5 11:00 0 13:00 5 14:00 4 (5 Replies)
Discussion started by: ernesto
5 Replies

5. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

6. Shell Programming and Scripting

In a row, replace negative sign and find minimum value among four columns

Hi Friends, I have an input file like this chr1 100 200 1 2 3 4 chr1 150 200 4 5 6 7 chr2 300 400 9 6 7 1 chr2 300 410 -10 21 -11 13 chr3 700 900 -21 -22 130 165 Now, my output file is chr1 100 200 1 chr1 150 200 4 chr2 300 400 1 chr2 300 410 10 chr3 700 900 21 Remove... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

7. Shell Programming and Scripting

Replace Second row with First row in a File

Hi, I need to replace first row to second row: Input: A JACK WAS_${HH}_JACK .... Output should be: JACK WAS_A_JACK .... I tried below code.. awk '{TEMP= (NR==1)}; {sub(/${HH}/$TEMP/)}' (2 Replies)
Discussion started by: kmsekhar
2 Replies

8. Shell Programming and Scripting

replace row separated by tab

Dear users, I have this problem, this is the example: 123 (tab) A (tab) B (tab) C (tab) 456 where the (tab) is actually the \t delimiter. I need to replace the A B and C for D E and F, this is: 123 (tab) D (tab) E (tab) F (tab) 456 The thing is that my file is quite long and this... (2 Replies)
Discussion started by: Gery
2 Replies

9. Shell Programming and Scripting

Replace last row of a column in bash/awk/sed

Hi, I've got a file with 3 columns which ends like this: ... 1234 345 1400 5287 733 1400 8472 874 1400 9317 726 1400 I want to replace the last row of the last column with the value 0. So my new file will end: ... 1234 345 1400 5287 733 1400 8472 874 1400 9317 726 ... (5 Replies)
Discussion started by: jhunter87
5 Replies

10. Shell Programming and Scripting

Find and replace duplicate column values in a row

I have file which as 12 columns and values like this 1,2,3,4,5 a,b,c,d,e b,c,a,e,f a,b,e,a,h if you see the first column has duplicate values, I need to identify (print it to console) the duplicate value (which is 'a') and also remove duplicate values like below. I could be in two... (5 Replies)
Discussion started by: nuthalapati
5 Replies
Login or Register to Ask a Question