Seperate Odd and Even numbers from 1 file to 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Seperate Odd and Even numbers from 1 file to 2 files
# 1  
Old 06-11-2015
Wrench Seperate Odd and Even numbers from 1 file to 2 files

Hey guys.

I have been trying to figure out an easy way to seperate a liste of 150k numbers (10 digits) in a .txt file into odd and even numbers with each of their own files, for a project at work.

I've tried Excel, but it was too much for it and it wasnt very simple. So i gave up after struggling with it for days.

Im new to scripting and was told that this place would be able to assist me in my endevour, so im asking all the gurus in here, if you would please help me so that I can atleast try and look a bit good, for my new boss. Smilie

Thank you in advance, for any tips/hints/scripts you might have, that can aid me.
# 2  
Old 06-11-2015
Code:
cat numberfile.txt | while read num
do

        if [ `echo "${num} % 2" | bc` -eq 0 ] 
        then  
                echo ${num} >> even.numbers.txt
        else
                echo ${num} >> odd.numbers.txt
done

# 3  
Old 06-11-2015
Hi, try something like:
Code:
awk '{print>(FILENAME "." ($1%2?"odd":"even"))}' file

# 4  
Old 06-11-2015
Try, untested


Code:
awk '{if ($0%2){print > "even"}else{print > "odd"}}' file

# 5  
Old 06-11-2015
Quote:
Originally Posted by blackrageous
Code:
cat numberfile.txt | while read num
do

        if [ `echo "${num} % 2" | bc` -eq 0 ] 
        then  
                echo ${num} >> even.numbers.txt
        else
                echo ${num} >> odd.numbers.txt
done

Do i have to make a seperate file for this one, so it opens the numbersfile.txt or? Not sure what im getting myself into Smilie

Quote:
Originally Posted by Scrutinizer
Hi, try something like:
Code:
awk '{print>(FILENAME "." ($1%2?"odd":"even"))}' file

As i said, im new to this scripting and unix, so go easy on me Smilie

im guessing that FILENAME is the name of my file with numbers in it. And the file is the output? I seem to be getting an error saying something about it not being able to open "file".
# 6  
Old 06-11-2015
Quote:
Do i have to make a seperate file for this one, so it opens the numbersfile.txt or? Not sure what im getting myself into Linux
You did not supply the name of your real .txt, so the helper used the name numbersfile.txt as a surrogate of yours. Just substitute this for yours.

Quote:
im guessing that FILENAME is the name of my file with numbers in it. And the file is the output? I seem to be getting an error saying something about it not being able to open "file".
Apply the same that above. Substitute file for your .txt
# 7  
Old 06-11-2015
Quote:
Originally Posted by Aia
You did not supply the name of your real .txt, so the helper used the name numbersfile.txt as a surrogate of yours. Just substitute this for yours.



Apply the same that above. Substitute file for your .txt
I think that i misformulated myself. Do i have to put that code into a file of its own and then run that? like javascript?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

For loop for seperate files

For shell script. If I had two separate files, file.txt and file1.txt and each has just a list of names from the who command. How would I create an if loop to compare each name? (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. UNIX for Dummies Questions & Answers

looping through file and creating seperate files

Hi, My first post!! I have a files with header, something like this Header_Row AMC|D1|D2|D2 AAO|D3|D4|D5 AMC|D6|D7|D8 AAO|D9|D10|D11 . . . . . and millions fo records thereafter like this. I want to read the above file in a loop and write the lines having AMC into another... (1 Reply)
Discussion started by: amitbakre
1 Replies

3. Shell Programming and Scripting

Help with command to Move files by X number to seperate directories

Hello, I need help finding a script that will allow me to move files from one directory to another directory 10k files at a time. I have a directory that has 100 K files in it. I need to have those 100k files broken apart to separate directories each with 10k files in them. Here is the... (8 Replies)
Discussion started by: Geo_Bean
8 Replies

4. UNIX for Dummies Questions & Answers

finding files with numbers in the file name???

Hello all, New to this forum! I got a Q: i want to find all files with numbers in the file name. e.g. blabla234.pm or fool654.pl action i took: ls | egrep '+' ls | egrep ls | egrep + ls | egrep '' ls | egrep '(+)' ls | egrep '()' ls | egrep '(.*.*)' ls | egrep '.*.*' ls | grep... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

5. UNIX for Dummies Questions & Answers

extract only the "numbers" that are present in this file to a seperate file..

It may sounds too easy but appreciate any help on this.. i have a file with the below details: ****************************************** Please find the locked pernr details for the Time evaluation Personnel number could not be locked Person rejected: 04552737 Personnel number could not... (4 Replies)
Discussion started by: rohit.shetty84
4 Replies

6. UNIX for Dummies Questions & Answers

comparing strings in seperate files

Hello, I am comparing files with for mismatches using fgrep but I've run into a problem. fgrep -vf $file1 $file2 > mismatches.dat file1 and file2 both contain file names on each line file1 has filenames which are up to 92 characters long and contain the "$" char. example file name:... (2 Replies)
Discussion started by: orahi001
2 Replies

7. Shell Programming and Scripting

Split File into seperate files

Hi, So I have a text file which I want to separate into separate text files. I would use the split command but the problem here is that the text file is separated by delimiters. For example: blah blah blah ------ more text ----- and some more text So basically the first part should be... (4 Replies)
Discussion started by: eltinator
4 Replies

8. Shell Programming and Scripting

Email like files in seperate emails

My goal is to send multiple files to a person based on their input. The files have similar names like: file1-egress-filter file2-ingress-filter stuff1-egress-filter stuff2-ingress-filter ... The script is run with the filename given as arguments, such as: ./mail.sh file stuff would... (6 Replies)
Discussion started by: earnstaf
6 Replies

9. Shell Programming and Scripting

how to remove files with only numbers as file names?

Hi all, I have a bunch of files that are named like 12543, 467249877, etc all over some directories.These files are named only with numbers, they dont have any letters or special characters in their file names. Could you please help me out and give me some command/script to remove only those... (6 Replies)
Discussion started by: praveen_indramo
6 Replies

10. Shell Programming and Scripting

How do I stop printf output from going into seperate txt files

I am using printf "%-75s%+10s %5s %1s \n" $s $z $x $y > status It works really well, however, when I email status it is sending 10 emails when I would like it to be in one. Is there a way to make all the output to go into one instance of a txt file. Yet still keep it 1 on each... (12 Replies)
Discussion started by: chrchcol
12 Replies
Login or Register to Ask a Question