Even numbers from text file


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

Hello, i have started learning scripting with linux commands. So far I am having probelms with one task:

To make a script with from file with the text, prints even numbers used in that text.

maybe any could script that?

Thank You.
# 2  
Old 01-05-2010
Hexes,

What is the format of the file. ?
What have you tried so far? Can we have a look at that.
# 3  
Old 01-05-2010
Code:
grep '[02468]$' file_in

Code:
nawk '$1 ~ "[02468]$"'

modify per needs.
# 4  
Old 01-05-2010
Code:
#!/bin/bash
echo "input n"
read n
echo "input m"
read m
m=`expr $m + 1`
x=`expr $m - $n`
head -$m text > text2
tail text2 > text3
cat text3
rm text2

well the script should look something like that

---------- Post updated at 04:08 PM ---------- Previous update was at 01:34 PM ----------

So far still need help, guys so maybe someone really can write BASH script with from text file(text.txt) finds numbers that are even and echo them on the screen.

I will be very thankful.

Last edited by Hexes; 01-05-2010 at 02:38 PM.. Reason: Please use code tags instead of PHP tags!
# 5  
Old 01-05-2010
Tools Can you post a sample file

Can you post a section of the sample file and desired output? That would make coding a little easier to understand.
# 6  
Old 01-05-2010
To match even numbers (not just digits), I think this would do it:
Code:
grep -o '\b[0-9]*[02468]\b'

But you are after a shell script. I would read every word in a file and see if it matches the pattern in the grep command. If it does, then print the word.

S.

Last edited by Scrutinizer; 01-05-2010 at 05:31 PM..
# 7  
Old 01-05-2010
for example we have text.txt file:
Code:
shell 6 3 32 programming 2 5 7 9 6 scripting 4 3 1

after we run BASH script we should see result as
Code:
6 32 2 6 4

only even numbers of the text
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