Even numbers from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Even numbers from text file
# 15  
Old 01-11-2010
MySQL

hello ,
Code:
perl -wlne 'print if !($_ % 2)' infile

or
Code:
 perl -we 'print grep {$_ % 2 == 0} <>' infile

Regards.
Gaurav.
# 16  
Old 01-11-2010
Quote:
Originally Posted by gaurav1086
hello ,
Code:
perl -wlne 'print if !($_ % 2)' infile

or
Code:
 perl -we 'print grep {$_ % 2 == 0} <>' infile

Regards.
Gaurav.
will this test fail if you have alphanumaric field? ...in my test it will not.
# 17  
Old 01-11-2010
Tools

Quote:
Originally Posted by ahmad.diab
will this test fail if you have alphanumaric field? ...in my test it will not.
The file is supposed to have only numeric values. Again if it doesnt comply then
Image
Code:
perl -wlne 'print if ((/[0-9]+/)&&!($_ % 2))' infile

or
Code:
perl -we 'print grep {/[0-9]+/ && $_ % 2 ==0 } <>' even

Piece of Cake. Smilie

Regards.
# 18  
Old 01-12-2010
gaurav1086:- your perl codes will fail if the input is as below...in my test it will not. Smilie


Code:
shell 6 3 32 programming23  2 5 7 9 6 scripting 4 3 1

# 19  
Old 01-12-2010
Lightbulb

[QUOTE=ahmad.diab;302386264]gaurav1086:- your perl codes will fail if the input is as below...in my test it will not. Smilie


Hello ahmad.diab
Modified Image
Code:
perl -awlne 'foreach (@F) {print if ((/[0-9]+/)&&!($_ % 2))}' infile

OR
Code:
 perl -nlwe 'print grep {/[0-9]+/ && $_ % 2 ==0 } split(/\s+/,$_)' infile

Output
Code:
gaurav@localhost:~$ cat even
2 4 1 6 5
3 6 2 8 3
gaurav@localhost:~$ perl -nlwe 'print grep {/[0-9]+/ && $_ % 2 ==0 } split(/\s+/,$_)' even
246
628
gaurav@localhost:~$

Another ->
Code:
gaurav@localhost:~$ perl -awne 'foreach (@F) {print if ((/[0-9]+/)&&!($_ % 2));}END{print "\n";}' even
246628
gaurav@localhost:~$

Regards.
Gaurav

Regards.
# 20  
Old 01-12-2010
still have a problem in filed number 5:-


Code:
cat infile:-

shell 6 3 32 programming23  2 5 7 9 6 scripting 4 3 1

Code:
o/p
Argument "programming23" isn't numeric in modulus (%) at -e line 1, <> line 1.
632programming23264

# 21  
Old 01-12-2010
Quote:
Originally Posted by ahmad.diab
still have a problem in filed number 5:-


Code:
cat infile:-

shell 6 3 32 programming23  2 5 7 9 6 scripting 4 3 1

Code:
o/p
Argument "programming23" isn't numeric in modulus (%) at -e line 1, <> line 1.
632programming23264

hello ,

Code:
gaurav@localhost:~$ cat even
2 4 1 6 5
3 6 2 8 3
shell 6 3 32 programming23 2 5 8 9 6 scripting 4 3 1
gaurav@localhost:~$ perl -awne 'foreach (@F) {print if ((/^[0-9]+$/)&&!($_ % 2));}END{print "\n";}' even
2466286322864
gaurav@localhost:~$

OR
Code:
gaurav@localhost:~$ perl -nlwe 'print grep {/^[0-9]+$/ && $_ % 2 ==0 } split(/\s+/,$_)' even
246
628
6322864
gaurav@localhost:~$

Modified the regex from /[0-9]+/ to /^[0-9]+$/ . Piece of Cake.
Regards,
Gaurav.

P.S. any more to come. lol Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add line numbers (multiples of 5: 0,5,10,15,20) to a text file?

Hi, I need to number the lines in my text file. I know how to do this with standard numbering (1,2,3,4, etc) but I need to count in multiples of 5, beginning 0,5,10,15... example existing file: abcd efg hijklm nopqrs desired output 0 abcd 5 efg 10 hijklm 15 ... (11 Replies)
Discussion started by: livbaddeley
11 Replies

2. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

3. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

How to find numbers in text file?

Hi I have a text file with rows like this: 7 Herman ASI-40 Jungle (L) Blueprint (L) Weapon Herman ASI-40 Jungle (L) 215.00 57 65.21 114.41 and 9 Herman CAP-505 (L) Blueprint (L) Weapon Herman CAP-505 (L) 220.00 46.84 49.1 104.82 and 2 ClericDagger 1C blueprint Melee - Shortblade... (2 Replies)
Discussion started by: pesa
2 Replies

5. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

6. Shell Programming and Scripting

Extract numbers from text file work out average

Just wondering if someone could assist me with shell script I'm trying to write. I need to read the final column of a text file (shown below) and workout what the average number is. The text file will have a variable number of lines, I just want the script to pull out the values in the final field... (14 Replies)
Discussion started by: rich@ardz
14 Replies

7. Shell Programming and Scripting

Inserting a range of consecutive numbers into a text file

I have a text file in the following format .... START 1,1 2,1 3,1 .. .. 9,1 10,1 END .... I want to change to the output to .... START 1,1 2,1 3,1 .. (4 Replies)
Discussion started by: VNR
4 Replies

8. Shell Programming and Scripting

how to add line numbers in text file

Hi all How to add line numbers in text file.. ex abcd cdef result 1. abcd 2. cdef thx in advance (4 Replies)
Discussion started by: suryanarayana
4 Replies

9. UNIX for Dummies Questions & Answers

Retrieving random numbers out of a text file

Hi one and all, I'm working on a Bash script that is designed to calculate how much IP traffic has passed through a port to determine traffic volume over a given amount of time. I've currently been able to use the netstat -s command coupled with grep to write to a file the total packets... (13 Replies)
Discussion started by: nistleloy
13 Replies

10. Shell Programming and Scripting

how to print out line numbers of a text file?

i have this text file name test.txt which contain : aaaaa bbb iiiiiiiiiiiii ccf ddaaa ddd and i need a script that can print out the line numbers using a while loop.. so when the script is run..it will have this: 1 2 3 any ideas? :) thanks guys (4 Replies)
Discussion started by: forevercalz
4 Replies
Login or Register to Ask a Question