0

I'm trying to print out

for i in 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40; do
awk -v a="$i" '{printf "%10.2f %10.2f\n", a, ($8*627.509)}' e1.txt > e2.txt
done

But when I open this file,

  1.40 -12111939.85
  1.40 -12112479.17
  1.40 -12112817.98
  1.40 -12112997.55
  1.40 -12113047.39
  1.40 -12112998.93
  1.40 -12112873.57
  1.40 -12112695.74
  1.40 -12112504.02
  1.40 -12112346.74
  1.40 -12112316.49
  1.40 -12112204.51
  1.40 -12112149.56

Ignore the second column as it reads the value and operates from other txt file, e1.txt.

As it is shown, only the last for-loop index variable is used in this case. But I wish to print the for-loop values of 0.80 ~ 1.40 accordingly to each line.

2
  • 4
    Your output redirection overwrites the output file for every iteration of the loop. Remove the output redirection from the "awk" line and add it to the "done" line: done > e2.txt Commented Jan 2, 2020 at 22:04
  • 2
    change > to >> and you are good to go. Commented Jan 2, 2020 at 22:06

1 Answer 1

3

For efficiency, I would avoid processing the same file 13 times.

The BEGIN block looks awkward because awk can't declare an array literal.

awk '
  BEGIN {
    a = "0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40"
    n = split(a, as)
  }
  {
    for (i=1; i <= n; i++)
      printf "%10.2f %10.2f\n", as[i], ($8 * 627.509)
  }
' e1.txt > e2.txt

If you want all the 0.80 first and all the 1.40 last, you can:

awk '...' e1.txt | sort -g > e2.txt
Sign up to request clarification or add additional context in comments.

1 Comment

you could just do split("0.80 0.85 ...", as) though, no need for a.

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.