Listing non zero values from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Listing non zero values from file
# 1  
Old 08-28-2014
Listing non zero values from file

Legends,

I have following contents in the file and i want to list out non-zero values only out of it.

Code:
OPge1 03
OPge10 121
OPge11 3
OPCge12 0
OPCge13 0
OPge14 25
OPC15 0

I am using following loop; but not getting desired results

Code:
for line in `cat /tmp/raw`
do
Name=`echo $line | awk -F' ' '{print $1}'`;
Count=`echo $line | awk -F' ' '{print $2}'`;
if [ "${Count}" != 0 ];
then
echo "$Name and $Count"
fi
done

Please help if
Code:
awk

or any other will be good way to do it
# 2  
Old 08-28-2014
Hello sdosanjh,

With awk here is one of the solution.

Code:
awk '$2' filename
OR
awk '($2!=0) {print}' filename

Output will be as follows.
Code:
OPge1 03
OPge10 121
OPge11 3
OPge14 25

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-28-2014 at 02:14 AM.. Reason: Adding one more solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-28-2014
@Ravinder Smilie
thanks
# 4  
Old 08-28-2014
If the text string in column 2 that you wish to exclude will always be described as a space followed by a single zero and then the end of line, that would give the regular expression 0$ that you could use in grep like this, which may be quicker:-
Code:
grep " 0$" filename

  • The leading space is important to make sure you don't exclude records that have values such as 30, 40, 50 etc.
  • The trailing $ marks the end of record so you don't exclude 101, 1024 etc.

If you have to evaluate the column in case there may be leading zeros too, then awk is the way unless it's a single leading zero, so you might need a single or double zero in which case:-
Code:
egrep -v " 0$| 00$" filename


I hope that this gives you an option to consider.


Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 08-28-2014
@rbatte, that works too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 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. UNIX for Dummies Questions & Answers

[Solved] How to remove listing of current user cmd from ps -ef listing?

Hi All, Could you please help to resolve my following issues: Problem Description: Suppose my user name is "MI90". i.e. $USER = MI90 when i run below command, i get all the processes running on the system containing name MQ. ps -ef | grep MQ But sometimes it lists... (8 Replies)
Discussion started by: KDMishra
8 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

7. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

8. Shell Programming and Scripting

How do I get only the file name from a listing?

Hi, I am trying to get a file name only. Could anyone help me on the same. Meaning I have a file say list.out which holds below output ./xyz/abc.txt ./xyz/hij.txt I want an output as below abc.txt hij.txt i.e I want to delete everything before abc.txt Please help me out if any one has... (4 Replies)
Discussion started by: spark
4 Replies

9. Solaris

file listing ...

Hi experts, I have several hundred files for everyday each month (below example is September)- PP023149200709010546.......PP028023200709012300 PP023150200709020023.......PP026096200709022134 .. .. PP021256200709201920.......PP025576200709202218 .. ..... (3 Replies)
Discussion started by: thepurple
3 Replies

10. UNIX for Dummies Questions & Answers

Recursive directory listing without listing files

Does any one know how to get a recursive directory listing in long format (showing owner, group, permission etc) without listing the files contained in the directories. The following command also shows the files but I only want to see the directories. ls -lrtR * (4 Replies)
Discussion started by: psingh
4 Replies
Login or Register to Ask a Question