« first day (4234 days earlier)      last day (722 days later) » 

12:47 AM
@FaheemMitha Same reason I dislike systemd. There are many problems with his philosophy.
 
 
7 hours later…
7:27 AM
@forest Systemd certainly seems to be controversial.
 
 
15 hours later…
10:45 PM
I have a file containing (x,y1) values sorted numerically according to the second column
0 0.0
8 0.1
4 0.2
. .
which is the reference order.

Another file contains (x,y2) sorted numerically
0 0.0
4 0.6
8 0.8

I want to re-sort the last file values so that the output looks like

0 0.0
8 0.8
4 0.6
The pattern is: this order matches the reference order in the sense of the second column.
 
Not quite sure what you mean (but I'm also a little out of it today).
 
Looks like you might need to write some code to do this. And given that the reference order isn't sorted, you're stuck with O(n^2)
 
But fwiw, a useful way to sort things in bash is while IFS= read -r x y; do <code goes here>; done
Which reads from stdin.
 
@dbush The reference order is sorted numerically according to the second field
 
But you'll be looking at the first field to figure out where things go.
So the second column doesn't really matter
 
10:54 PM
In my case the second fields represents energies which are always sorted numerically, and the first field are labels to those values
 
This can be done with nested loops in bash, with the outer loop reading the reference file and the inner loop reading the file to be resorted
Outer loop reads a line and saves the value of the first column. Then the inner loop reads lines until it finds one that has the matching value for the first column and writes that line to the output file
 
Good hint, I may have to first learn about shell loops...
 
```
> outfile
while read r1 r2
do
while read in1 in2
do
if [ "$r1" = "$in1" ]
then
echo $in1 $in2 >> outfile
break
fi
done < infile
done < ref_file
```
 
11:11 PM
I will give it a try..
 
11:29 PM
@EnthusiastiC Not sure I'm getting it, but maybe you are looking fore something like this:
$ cat file1
0 0.0
8 0.1
4 0.2
$ cat file2
0 0.0
4 0.6
8 0.8
$ awk 'NR == FNR { seen[$1] = $2 } NR != FNR { print $1, seen[$1] }' ./file2 ./file1
0 0.0
8 0.8
4 0.6
 
@fra-san Absolutely true!
could you explain a bit the logic
 
This is a commonly seen solution to problems like the one in this question: unix.stackexchange.com/q/610303/315749
I'm pretty sure I've seen it several times on the site, but I'm unable to find a Q/A with a good explanation right now.
 
ok
No problem, though; it was a big help.
 
11:46 PM
@EnthusiastiC In short, that AWK script saves an associative array of file2's second column values, indexed by the first column values; then, for each line of file1, it prints its first column value and uses it to look up the corresponding value for the second column from the associative array.
 
I see.
 

« first day (4234 days earlier)      last day (722 days later) »