Print particular string in a field of csv file

Hi, all

I need your help and suggestions.
I want to print particular strings in a field of a csv file and show them in terminal. Here is an example of the csv file.

SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw
/home/intannf/foto3/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 
/home/intannf/foto3/2015_0313_090323_155.JPG,0.,-7.77224,110.37312,30.73,996.46,148.76,181.01,181.92,63.82 
/home/intannf/foto3/2015_0313_090142_124.JPG,0.,-7.77224,110.37312,30.73,996.46,148.75,181.13,182.06,63.87 
/home/intannf/foto3/2015_0313_085929_083.JPG,0.,-7.77224,110.37312,30.72,996.46,148.74,180.93,182.12,63.64 
/home/intannf/foto3/2015_0313_090710_225.JPG,0.,-7.77224,110.37312,30.72,996.46,148.77,181.09,181.86,63.78 
/home/intannf/foto3/2015_0313_090628_212.JPG,0.,-7.77223,110.37310,30.72,996.47,148.67,181.09,181.91,63.87 
/home/intannf/foto3/2015_0313_085942_087.JPG,0.,-7.77219,110.37317,30.76,996.47,148.71,181.12,182.17,63.78 
/home/intannf/foto3/2015_0313_090717_227.JPG,0.,-7.77217,110.37315,30.77,996.48,148.66,181.06,182.21,63.87

Output required

2015_0313_090651_219.JPG
2015_0313_090323_155.JPG
2015_0313_090142_124.JPG
2015_0313_085929_083.JPG
2015_0313_090710_225.JPG
2015_0313_090628_212.JPG
2015_0313_085942_087.JPG
2015_0313_090717_227.JPG

When i tried to print the first column, like this

awk '{ print $1 }' hasil.csv

it prints all the columns, not just the 1st field.

Would you mind to help me to solve it? I really need your help. Thanks in advance.

Regards,
Intan

awk -F "[/,]" 'NR>1 {print $5}' file
1 Like

Other way

$ awk -F, 'FNR>1{n=split($1,A,/\//); print A[n]}' infile
2015_0313_090651_219.JPG
2015_0313_090323_155.JPG
2015_0313_090142_124.JPG
2015_0313_085929_083.JPG
2015_0313_090710_225.JPG
2015_0313_090628_212.JPG
2015_0313_085942_087.JPG
2015_0313_090717_227.JPG
1 Like

Wow, thank you protocomm and Akshay Hegde for your help!:smiley:

But I don't understand yet about the script. Would you mind to explain it to me? Thank you.

Regards,
Intan

awk -F, 'FNR>1{n=split($1,A,/\//); print A[n]}' infile

-F,
Set input field separator comma

FNR>1
FNR is number of records relative to the current input file, since we are interested to skip header so used FNR >1 .

split(string, array, fieldsep)
This divides string into pieces separated by fieldsep, and stores the pieces in array. The first piece is stored in array[1], the second piece in array[2], and so forth. The string value of the third argument, fieldsep, is a regexp describing where to split string (much as FS can be a regexp describing where to split input records). If the fieldsep is omitted, the value of FS is used. split returns the number of elements created.

n=split($1,A,/\//)
From above, you can see here variable n holds number of elements created, where first argument column1 $1 is string, A is array and /\// is field separator

print A[n]
Finally print last element in array A that is your filename.

1 Like

Thank you so much, Akshay Hegde!:smiley:

awk -F "[/,]" 'NR>1 {print $5}' file
-F "[/,]"

Set Field Input Separator to /,

NR>1

skip the first line

{print $5}

print the fifth field

1 Like

Thank you so much, protocomm!:smiley:

There is something I want to ask you, why does the script print the fifth field? I dont understand it yet. Thank you.

Regards,
Intan

first field/secondhome/thirthintannf/fourthfoto3/and fifth2015_0313_090651_219.JPG,
-F"[/,]"

has 2 field separator

If i have the field separator

/

the fifth print

2015_0313_090651_219.JPG,

and you want

2015_0313_090651_219.JPG

without comma

1 Like

Okay, i got it. Thank you so much, protocomm! :smiley: