?
Solved

delete all negative numbers in a file using shell or perl

Posted on 2011-09-02
5
Medium Priority
?
270 Views
Last Modified: 2012-05-12
ok. I have a file like below. I want to keep all rows and columns just eliminate the values that are negative. Also, the values may sometimes be rounded to the nearest hundreth (i.e. 00.00 format) instead of just whole numbers.

02-03-2011 00:00:00 13 16 19 22 21 14 12 10 11 15 14 23 25 24 23 17 20 26 23 27 26 23 15 10
02-04-2011 00:00:00 12 18 18 14 19 4 6 6 6 8 10 12 19 17 12 7 3 3 2 0 3 3 1 1
02-05-2011 00:00:00 3 3 5 11 8 19 5 5 4 5 6 7 7 5 6 4 0 -2 -1 0 1 0 0 3
02-06-2011 00:00:00 4 4 6 4 2 2 3 3 0 0 6 8 4 5 4 3 3 0 -4 -4 1 3 3 5
02-07-2011 00:00:00 4 1 2 3 3 2 4 7 8 11 13 20 36 12 13 19 24 21 22 13 16 19 16 17
02-08-2011 00:00:00 17 15 23 27 24 19 21 13 28 22 25 25 22 23 17 4 2 3 6 21 8 10 11 12
02-09-2011 00:00:00 20 17 14 13 13 12 9 9 11 16 14 22 15 14 15 17
02-10-2011 00:00:00 15 15 22 18 13 17 9 9 9 10 7 5 7 8 8 19
02-11-2011 00:00:00 17 22 25 29 24 22 21 22 32 22 32 22 15 21 26 14 9 9 14 15 21 19 14 20
02-12-2011 00:00:00 32 30 29 24 18 23 17 14 28 27 23 31 28 27 23 23 21 19 23 21 21 28 30 28
02-13-2011 00:00:00 31 35 32 15 13 8 8 9 7 26 25 21 24 6 3 2 10 23 33 17 7 23 21 24
02-14-2011 00:00:00 23 28 28 1 0 -1 1 2 4 4 9 14 14 23 29 13 10 10 9 33 12 20 14 26
02-15-2011 00:00:00 24 17 4 8 17 11 12 12 9 9 10 10 9 7 5 4 6 5 8 16 12 22 20 19
02-16-2011 00:00:00 21 18 19 9 10 10 10 12 11 8 4 9 10 6 0 -1 -3 -3 1 2 1 -1
02-17-2011 00:00:00 0 0 0 3 2 4 9 11 12 11 12 18 19 13 10 9 17 9 12 14 13 11 11 14
02-18-2011 00:00:00 11 10 10 8 7 8 8 5 9 10 4 3 7 8 9 10 8 6 7 11 9 7 8 6
02-19-2011 00:00:00 7 10 11 13 13 13 13 11 16 17 13 22 17 16 6 8 10 11 12 13 9 6 5 2
02-20-2011 00:00:00 5 9 10 11 10 10 8 8 12 12 15 19 17 17 14 13 14 14 13 19 19 15 18 18
02-21-2011 00:00:00 12 13 6 6 11 12 18 14 15 17 27 23 18 13 9 5 2 3 4 6 9 5 4 7
02-22-2011 00:00:00 7 9 8 5 5 4 6 8 8 4 6 15 16 17 16 14 14 13 13 14 11 17 15 12
02-23-2011 00:00:00 15 7 19 8 10 10 9 7 9 13 10 10 12 12 12 10 8 5 5 11 13 12 12 9
02-24-2011 00:00:00 8 10 7 7 6 5 7 3 0 1 5 9 11 6 1 1 2 0 -2 -3 -3 -2 -1 -1
02-25-2011 00:00:00 2 3 0 1 1 -3 0 4 3 5 5 6 8 8 16 11 6 1 -3 -3 2 6 7 9
02-26-2011 00:00:00 6 3 16 3 3 4 6 8 9 10 10 8 17 8 11 9 17 8 7 4 3 6 8 10
02-27-2011 00:00:00 10 16 13 12 11 10 11 10 19 16 17 22 20 20 16 13 9 4 0 -1 1 4 8 6
02-28-2011 00:00:00 5 9 13 6 2 6 11 18 13 16 29 27 21 21 25 16 18 35 30 29 25 13 15 20
03-01-2011 00:00:00 13 17 13 18 15 13 11 12 17 21 28 27 23 21 18 21 20 19 18 7 16 22 15 15
03-02-2011 00:00:00 19 12 20 15 13 10 11 14 17 15 17 19 21 22 14 11 4 5 22 21 1 2
03-03-2011 00:00:00 19 9 8 15 10 7 3 5 10 9 9 9 2 -2 -4 -5 -5 -3 -1 -1 -5 -5 -1 -1
03-04-2011 00:00:00 -1 2 0 -3 -3 -3 -5 -4 -2 -1 -1 1 1 -1 -2 -3 -3 -1 -2 -4 -7 -7 -4 1
03-05-2011 00:00:00 4 2 2 0 -1 2 1 -5 -8 -4 -4 -1 2 5 8 6 4 3 -1 2 7 8 2 1
03-06-2011 00:00:00 2 1 -1 -3 -2 1 1 -2 -2 2 5 10 13 20 22 16 12 13 18 18 18 10 17 17
03-07-2011 00:00:00 0 2 3 21 8 9 9 9 11 10 12 18 16 12 25 19 10 7 2 3 8 12 11 17
03-08-2011 00:00:00 15 11 9 6 7 10 9 8 8 7 10 16 17 15 20 25 17 26 21 37 14 17 21 16
03-09-2011 00:00:00 25 12 12 7 9 14 17 14 14 17 12 16 21 20 25 33 29 33 42 28 18 19 30 28
03-10-2011 00:00:00 21 29 34 28 27 28 12 20 17 25 17 10 11 12 22 11 10 12 13 12 13 19 14 14
03-11-2011 00:00:00 18 13 17 4 4 17 17 11 5 5 12 11 8 9 9 9 11 16 5 8 12 10 10 11
03-12-2011 00:00:00 10 12 11 5 3 7 10 11 9 6 6 9 11 12 10 9 11 15 14 11 9 10 10 16
03-13-2011 00:00:00 15 15 7 7 8 5 3 4 6 8 7 7 7 9 9 6 0 -3 5 7 3 2 5 7
03-14-2011 00:00:00 5 2 2 7 7 5 2 2 6 6 6 8 8 7 8 9 5 0 1 3 6 6 6 9
03-15-2011 00:00:00 8 4 2 4 4 4 5 6 6 5 4 6 4 5 9 6 7 7 5 8 11 9 6 5
03-16-2011 00:00:00 7 7 6 3 0 1 3 4 5 4 4 6 9 12 11 10 11 10 4 2 2 3 1 -1
03-17-2011 00:00:00 1 0 -3 -5 -3 -3 -4 -3 1 3 2 6 7 7 8 8 8 7 3 3 5 2 -1 -1
03-18-2011 00:00:00 -1 -1 -1 1 2 3 2 1 2 1 1 6 8 10 11 8 6 7 6 2 -2 -1 1 3
03-19-2011 00:00:00 6 8 9 5 4 8 7 3 2 5 7 5 6 6 4 7 7 3 0 -1 4 5 1 2
03-20-2011 00:00:00 4 3 1 1 3 1 1 4 2 4 16 21 25 22 14 19 10 5 -2 -2 1 3 6 6
0
Comment
Question by:libertyforall2
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:jeromee
ID: 36476541
Try this:

perl -ane 'print "@F[0,1]".join("", map{$_>=0 ? " ".($_+0) : ""} @F[2..25])."\n"' /your/file
0
 

Author Comment

by:libertyforall2
ID: 36476566
This creates zero values where no values are present. This will mess up my data. Is there a way to do without creating the extra zero's?
0
 
LVL 10

Accepted Solution

by:
jeromee earned 2000 total points
ID: 36476744
Why do you say that replaced negative numbers with zero?
Look at the 3rd line... the series
4 0 -2 -1 0 1
turned into:
4 0 0 1

% head -3 /tmp/abc
02-03-2011 00:00:00 13 16 19 22 21 14 12 10 11 15 14 23 25 24 23 17 20 26 23 27 26 23 15 10
02-04-2011 00:00:00 12 18 18 14 19 4 6 6 6 8 10 12 19 17 12 7 3 3 2 0 3 3 1 1
02-05-2011 00:00:00 3 3 5 11 8 19 5 5 4 5 6 7 7 5 6 4 0 -2 -1 0 1 0 0 3
% perl -ane 'print "@F[0,1]".join("", map{$_>=0 ? " ".($_+0) : ""} @F[2..25])."\n"' /tmp/abc | head -3
02-03-2011 00:00:00 13 16 19 22 21 14 12 10 11 15 14 23 25 24 23 17 20 26 23 27 26 23 15 10
02-04-2011 00:00:00 12 18 18 14 19 4 6 6 6 8 10 12 19 17 12 7 3 3 2 0 3 3 1 1
02-05-2011 00:00:00 3 3 5 11 8 19 5 5 4 5 6 7 7 5 6 4 0 0 1 0 0 3

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 36477043
I was able to incorporate it in a top line script so this is no longer needed.
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36478552
Glad I was able to help.

Happy Perl'ing!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Introduction to Processes

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question