0

I am trying to print the second part of a comma delimited string 28213. What am I doing wrong?

#!/bin/bash
file="4646484726,28213,93951656,Jameson Booker,10000043,Domain,60008494,Amarr VIII (Oris) - Emperor Family Academy,10,True,583000.0,100,71.0,2016-10-04 13:49:51.000,0,1,60618962,90,False,30002187,Amarr,7688312.5,"
echo "$file"|sed s/,/\\n/g | awk '{print $2}'

I understand that sed is splitting the string into chunks by comma, and that awk is then returning only strings with spaces in them so Jameson Booker returns Booker, but I do not know how to split and return values for the entire line, not just the comma separated values.

Rilcon42
  • 9,584
  • 18
  • 83
  • 167

1 Answers1

0

Awk column matching doesn't work across multiple lines, which you're creating by inserting a line break. It works on whitespace based columns by default. Replace the comma with a space, not a line break.

echo "$file"|sed 's/,/ /g' | awk '{print $2}'

Or split on a comma by specifying the delimiter:

echo "$file"| awk -F',' '{print $2}'
Andy Ray
  • 30,372
  • 14
  • 101
  • 138