2

I have a list of points coordinates in 2 column format first column x-axis, second column y-axis, and I'd like to make a script to rescale the axis.

input file example
4 3
3 6

output file example (rescale to 10 10)
10 5
7.5 10

To do that, I first get the biggest coordinate on both column (using sort, head and cut), and then I interpolate all lines using a perl substitute regexp.

So I scripted that :

#!/bin/sh
# run:  $ ./normalize x y input_file output_file
X=$1
Y=$2
INPUT=$3
OUTPUT=$4

BIGGEST_FIRST_COLUMN=$(sort -nrk1 $INPUT | head -n1 | cut -d ' ' -f 1)
BIGGEST_SECOND_COLUMN=$(sort -nrk2 $INPUT | head -n1 | cut -d ' ' -f 2)

PERL_CMD="'s!([\d.]+)\s([\d.]+)!(\$1/$BIGGEST_FIRST_COLUMN*$X).\" \".(\$2/$BIGGEST_SECOND_COLUMN*$Y)!e'"

CMD="perl -pe $PERL_CMD $INPUT > $OUTPUT"
echo "Run the following cmd : "
echo $CMD

$CMD

This code output the perl onliner I want to run and then try to run it. At this point, the perl oneliner is working : I can copy-paste it and running it by hand and it gives the correct output. Nevertheless, I got a bug when the script run it automatically :

Can't find string terminator "'" anywhere before EOF at -e line 1.

I suppose this is due to some problem in my comprehension of " and ' in shell, but at this point, I have no idea... So if you have any tips, welcome

2 Answers 2

1

You should change the last line:

#!/bin/sh
# run:  $ ./normalize x y input_file output_file
X=$1
Y=$2
INPUT=$3
OUTPUT=$4

BIGGEST_FIRST_COLUMN=$(sort -nrk1 $INPUT | head -n1 | cut -d ' ' -f 1)
BIGGEST_SECOND_COLUMN=$(sort -nrk2 $INPUT | head -n1 | cut -d ' ' -f 2)

PERL_CMD="'s!([\d.]+)\s([\d.]+)!(\$1/$BIGGEST_FIRST_COLUMN*$X).\" \".(\$2/$BIGGEST_SECOND_COLUMN*$Y)!e'"

CMD="perl -pe $PERL_CMD $INPUT > $OUTPUT"
echo "Run the following cmd : "
echo $CMD

# $CMD  < no
eval $CMD
Sign up to request clarification or add additional context in comments.

Comments

0

Here's a Perl one-liner that will do as you ask without needing the shell to preprocess the data

perl -MList::Util=max -E'@d=map[split],<>;for $i(0,1){$m=max map $$_[$i],@d; $$_[$i]*=10/$m for @d} say qq{@$_} for @d' data.txt

output

10 5
7.5 10

1 Comment

Ahah, nice ! Not comfortable enough to write one-liner like that yet :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.