0

I'm trying to go through a CSV file (technically a .gz file) and read the second column. Came across this question but am having some difficulty with using the first answer.

Command I'm using is grep ${VALUE} inputfile.csv | cut -d, -f${INDEX}.

For my shell script, I think it should be grep ${apples} inputfile.csv | cut -d, -f${2}.

However, I'm getting an error of

cut: option requires an argument -- 'f'
Try 'cut --help' for more information

The comma in the command didn't seem right, so I edited it to

grep ${apples} inputfile.csv | cut -d -f${INDEX}

which gave me an error of

The delimiter must be a single character
Try 'cut --help' for more information

What is the right syntax for grabbing the field after apples? CSV file header looks like this - very simple:

fruits | fruitcount

Community
  • 1
  • 1
simplycoding
  • 2,770
  • 9
  • 46
  • 91

2 Answers2

4

The answer to the question you linked used variables to show where you would place the values you want to use, you don't need the ${} syntax:

grep apples inputfile.csv | cut -d, -f2
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
0

Equivalently can be replaced with this awk script

awk -F, '/apples/{print $2}' inputfile.csv
karakfa
  • 66,216
  • 7
  • 41
  • 56