0

I am trying to retrieve the values in a .csv file (chimefrbcat1.csv) column #4 by nesting it inside a for-loop. However when I run the script, it simply outputs the numbers 2 to 599. I am trying to retrieve the values in column 4 from row 2 to row 599. My code is the following:

#!/bin/bash
for i in {2..599..1}
do
  echo -n "$i"
  currentchimera=$(awk 'NR == $i {print $4}' chimefrbcat1.csv)
  echo "$currentchimera"
done
Ethan Kao
  • 35
  • 3
  • 1
    Please add sample input (with field separators, no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Jun 29 '21 at 05:50
  • The `for` loop is quite superfluous here. You can replace the entire script with `awk -F , 'NR >= 2 && NR <= 599 { print NR " " $4 }' chimefrbcat1.csv` (or well you need `print NR $4` to print the line number and the value without a space between them). Notice also the `-F ,` to specify comma-delimited. – tripleee Jun 29 '21 at 06:02
  • A sample of the .csv file is: name,previous_name,repeater_name,ra,ra_err,ra_notes,dec,dec_err,dec_notes FRB20180725A,180725.J0613+67,-9999,93.42,0.04,-9999,67.1,0.2,-9999 FRB20180727A,180727.J1311+26,-9999,197.7,0.1,-9999,26.4,0.3,-9999 ... I am trying to get the variable currentchimera to contain the values 93.42 and then 197.7 and so on. the full .csv file can be found here https://www.chime-frb.ca/catalog – Ethan Kao Jun 29 '21 at 06:06
  • @tripleee is there a way to make the output a variable because I will need the value for calculations later – Ethan Kao Jun 29 '21 at 06:08
  • `awk -F , 'NR >= 2 && NR <= 599 { print NR " " $4 }' chimefrbcat1.csv | while read -r lineno chimera; do echo "chimera on line $lineno is $chimera"; done` – tripleee Jun 29 '21 at 06:08
  • @tripleee is it possible to modify it so that calculations can be made for each value in the variable ```chimera```. I need to be able to do calculations with the variable instead of listing all the values out. thanks for your help! – Ethan Kao Jun 29 '21 at 07:05
  • That's rather vague. Probably post a new question with your actual requirements and a [mre]. – tripleee Jun 29 '21 at 07:06

0 Answers0