Removing zero values from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing zero values from text file
# 1  
Old 10-05-2011
Removing zero values from text file

Hi all,
I wrote the following code to remove the value which are 0 in the input file (a columns if numbers).

Code:
awk 'BEGIN {
  for (i=1; i<=NF; i++)
    if ($i)
        printf("%13.6e\n",$i)
}' $1 >> $2

The script works if the zeros are written as
Code:
0.0000

but not as
Code:
0.000000e+00

In that case it creates an empty file, as all the values are zeros !?
Is there a way to make awk read the numbers in any format (integer, floating, scientific)?

What am I doing wrong?
Thank you,
Sarah
# 2  
Old 10-05-2011
Remove "BEGIN"
# 3  
Old 10-05-2011
I did remove BEGIN but it does not change the result
# 4  
Old 10-05-2011
can you write this your input file?
# 5  
Old 10-05-2011
Please post your input file and the desired output.
# 6  
Old 10-05-2011
input1
Code:
0.0000
0.0000
0.0000
0.0000
701.93
0.0000
356.39
0.0000
0.0000
0.0000

output1
Code:
701.93
356.39


input2
Code:
0.000000e+00
1.000000e+02
4.701600e+01
1.696800e+01
3.558200e+01

output2
Code:
1.000000e+02
4.701600e+01
1.696800e+01
3.558200e+01

# 7  
Old 10-05-2011
Something like this should work IMHO:
Code:
awk '$0{printf("%13.6e\n",$i)}' file

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Removing \n from a text file

Hi All, I have a question regarding text substitution. I have a file that contains a lot of text. Some of the text is separated with a \n like: TEST\nTEST2\nTEST3 BLA\nBLA2\nBLA3 So there are both actual newlines and 'used to be newlines' in the text. using tr tr "\n" "," or... (5 Replies)
Discussion started by: JaapSchuurman
5 Replies

2. Shell Programming and Scripting

Egrep or awk for removing values within CSV file?

Hello, I have a large CSV file that contains values all on the same column, and in one very long row (e.g. no line breaks till end, with all data values separated by a comma). The file has two types of data for the values. One begins with the letters rs and some numbers. The other begins... (4 Replies)
Discussion started by: macurdy
4 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

removing lines with similar values from file

Hello, got a file with this structure: 33274 171030 02/29/2012 37897 P_GEH 2012-02-29 10:31:26 33275 171049 02/29/2012 38132 P_GEH 2012-02-29 10:35:27 33276 171058 02/29/2012 38515 P_GEH 2012-02-29 10:43:26 33277 170748 02/29/2012 40685 P_KOM ... (3 Replies)
Discussion started by: krecik28
3 Replies

5. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. UNIX for Dummies Questions & Answers

Removing a string of text from a file - help please

Hey Folks, I have a file that contains data that I am working with, sometimes this file has a very long string of text that messes with an awk command in a script i am trying to build. I would like to cut this string of text out of a file and then redirect everything except that string to a new... (5 Replies)
Discussion started by: deepslp
5 Replies

7. UNIX for Dummies Questions & Answers

Help removing strings from one file that match any of the values in a second file.

Hello, I have a file that lists a few hundred values. Example: abca abcb abcc abcd I have a 2nd file with a few thousand lines. I need to remove every line from the 2nd file that contains any of the values listed in first file. Example of strings to delete: line1 *abca* end of... (1 Reply)
Discussion started by: upstate_boy
1 Replies

8. Shell Programming and Scripting

Removing text from a line in a file

Hi All, I would like to know how to remove text from a line in a file. eg to The 4 sets of numbers are not static ie they change on each line in each different file so if anyone can help that would be great. Jeremy (10 Replies)
Discussion started by: outthere_3
10 Replies

9. Shell Programming and Scripting

Removing lines in a text file.

Here is my problem I'm hoping you guru's can help me figure out. I have a text file that contains comma delimited columns. What I'm looking to do is see if the 24th column on each row in the file contains a value (not null), and then write/append that line to a different file. I've been... (4 Replies)
Discussion started by: WABonnett
4 Replies

10. UNIX for Dummies Questions & Answers

removing commas from text file

Dear all I have a file which looks like this xxxxxxxxxxxxxx,xxx,xxxxxxxxxx xxxxxxxxxxxxxx,xxx,xxxxxxxxxx etc basically 14 characters then a comma, three characters, then a comma then 10 characters. We are uploading this file to our mainframe and they want the commas removed, so it... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question