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
# 8  
Old 06-11-2015
They can be run as it from the command line interface.
# 9  
Old 06-11-2015
Quote:
Originally Posted by Aia
They can be run as it from the command line interface.
According the cygwin bash shell, i get an unknown syntax at done, with the first one.

Not sure what the error is. -_-
# 10  
Old 06-11-2015
I am assuming you are executing this:
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
        fi
done

If so, that contains an error. Add the red part.
# 11  
Old 06-11-2015
Quote:
Originally Posted by Aia
I am assuming you are executing this:
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
        fi
done

If so, that contains an error. Add the red part.
Just tried it with the addition. It took all the numbers from numbers.txt and put it into odd.numbers.txt Smilie
# 12  
Old 06-11-2015
The first script requires to have the program `bc' installed.
Try any of the awk solutions, they both do the same with different syntax.

---------- Post updated at 11:39 AM ---------- Previous update was at 11:28 AM ----------

Quote:
Originally Posted by TranceC
Just tried it with the addition. It took all the numbers from numbers.txt and put it into odd.numbers.txt Smilie
That means this part:
Code:
if [ `echo "${num} % 2" | bc` -eq 0 ]

is evaluating always to 0, which could be an indication that read is not getting the right information and num does not translate to a number.

Could you post a few lines of your number.txt file?
# 13  
Old 06-11-2015
Quote:
Originally Posted by Aia
The first script requires to have the program `bc' installed.
Try any of the awk solutions, they both do the same with different syntax.

---------- Post updated at 11:39 AM ---------- Previous update was at 11:28 AM ----------



That means this part:
Code:
if [ `echo "${num} % 2" | bc` -eq 0 ]

is evaluating always to 0, which could be an indication that read is not getting the right information an num is does not translate to a number.

Could you post a few lines of your number.txt file?
Just updated the cygwin install with BC, and it worked like a charm Smilie


Thank you everyone for your awesome and quick help Smilie)
# 14  
Old 06-11-2015
Quote:
Originally Posted by TranceC
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



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".
OK Smilie ... FILENAME is variable that you should not change. You would need to change file into your actual input filename.. The script then creates two new files with the name of that input file plus the extensions .odd and .even
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