59465

Output the result of each loop in different columns

Question:

price.txt file has two columns: (name and value)

Mary 134 Lucy 56 Jack 88

range.txt file has three columns: (fruit and min_value and max_value)

apple 57 136 banana 62 258 orange 88 99 blueberry 98 121

My aim is to test whether the value in price.txt file is between the min_value and max_value in range.txt. If yes, putout 1, If not, output "x".

I tried:

awk 'FNR == NR { name=$1; price[name]=$2; next} { for (name in price) { if ($2<=price[name] && $3>=price[name]) {print 1} else {print "x"} } }' price.txt range.txt

But my results are all in one column, just like follows:

1 1 x x x x x x 1 1 1 x

Actually, I want my result to be like: (Each name has one column)

1 x 1 1 x 1 x x 1 x x x

Because I need to use paste to add the output file and range.txt file together. The final result should be like:

apple 57 136 1 x 1 banana 62 258 1 x 1 orange 88 99 x x 1 blueberry 98 121 x x x

So, how can I get the result of each loop in different columns? And is there anyway to output the final result without paste based on my current code? Thank you.

Answer1:

This builds on what you provided,

# load prices by index to maintain read order FNR == NR { price[names++]=$2 next } # save max index to avoid using non-standard length(array) END { names=NR } { l = $1 " " $2 " " $3 for (i=0; i < names; i++) { if ($2 <= price[i] && $3 >= price[i]) { l = l " 1" } else { l = l " x" } } print l }

and generates output,

apple 57 136 1 x 1 banana 62 258 1 x 1 orange 88 99 x x 1 blueberry 98 121 x x x

However, you don't have the person name for the score (anonymous results) - maybe that's intentional?

The change here is to explicitly index array populated in first block to maintain order.

Recommend

  • How can I delete specific lines using awk/sed based on the contents of another file
  • line and string position of grep match
  • How to search by variable in awk
  • Is it possible to have different behavior for first and second input files to awk?
  • Extract audio frames from AMR-NB file
  • Is there a way to clone native functions in javascript like window.alert or document.write
  • MySQL: Difference between `… ADD INDEX(a); … ADD INDEX(b);` and `… ADD INDEX(a,b);`?
  • Python Tkinter after event OOPS implementation
  • Is it better to have roles as a column on my users table, or do it through join tables (Roles &
  • Dynamic ranges again - once more, with text strings
  • Double-click autofill - dynamic based on adjacent cell
  • Caliburn Micro, How to use ContentControl (or display 'sub' ViewModel) using ViewModel Fir
  • Python pandas melting data to multiple columns and coulmn names in another column
  • What's the syntax to inherit documentation from another indexer?
  • Drawing a Path in XAML (with binding) vs. C#. One works, one doesn't
  • Error processing multiple files
  • Hash Code in SQL Server?
  • In-place sed command not working
  • Ember.js model to be organised as a tree structure
  • Get specific string
  • Groovy: Unexpected token “:”
  • Replace value with Factor in r data.table
  • Checking free space on FTP server
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Deselecting radio buttons while keeping the View Model in synch
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Transpose CSV data with awk (pivot transformation)
  • Linker errors when using intrinsic function via function pointer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • python draw pie shapes with colour filled
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal