Reputation: 586
How can I sort the input from cat
like that explained the following code (?) :
function sort_input {
output_filename=${1}
# How can I sort the input (What that this function get from `cat`) into `output_filename` ?
}
cat ${1} | sort_input ${1} # I don't want to change this line!
The script get as argument: name of a file.
Clarification: How can I write sort_input
function, so that, this function will sort the input that sended by cat ${1}
[And will insert the result of the sorting to the file output_filename
] ?
Upvotes: 0
Views: 149
Reputation: 531908
The function you want is pretty simple:
sort_input () {
sort -o "$1"
}
sort
will inherit its standard input from the function, so sort_input foo < foo
is identical to sort -o foo < foo
. -o
involves a temporary file internally so that it works even if you are using the same file for input and output.
Upvotes: 1
Reputation: 586
More option: (without a temp file)
After that I see the answer of @PesaThe , I think that we can do it without a temp file, like that:
#!/usr/bin/env bash
sort_input() {
sort /dev/stdin > "$1"
}
cat "$1" | sort_input "$1"
Upvotes: 0