Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I don't know how to read input array number in input.txt and write result in output.txt. Example:

input.txt have array 7 8 9 2 
write result sort in output.txt 2 7 8 9  

How can I do it?

share|improve this question
" input.txt have array 7 8 9 2" is no useful information. What is that to be, different lines? Data within the same line? – Hauke Laging Apr 22 at 3:17

marked as duplicate by jasonwryan, Stephane Chazelas, rahmu, manatwork, vonbrand Apr 22 at 9:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

You could use awk to put the variables into seperate lines in one file.

awk '$1 { print $1 }' < input.txt >> input_seperatelines.txt
awk '$2 { print $2 }' < input.txt >> input_seperatelines.txt
awk '$3 { print $3 }' < input.txt >> input_seperatelines.txt
awk '$4 { print $4 }' < input.txt >> input_seperatelines.txt

And then use sort like this:

sort input_seperatelines.txt >> output.txt

Finally delete the temporary file input_seperatelines.txt

rm input_seperatelines.txt
share|improve this answer

Suppose input is in input.txt and you want output in output.txt. Make a python script and name it sort.py like this:

l=map(int,raw_input("").strip().split())
l.sort() 
print l  # It will store it as a list

# or more precisely your answer can be
k=""
for i in l:
    k+=str(i)+" "
print k       #same output as you want

Run it in the terminal :

python sort.py < input.txt > output.txt
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.