Visit Our UNIX and Linux User Community


Print particular string in a field of csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print particular string in a field of csv file
# 1  
Old 04-08-2015
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.

Code:
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
Code:
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
Code:
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
# 2  
Old 04-08-2015
Code:
awk -F "[/,]" 'NR>1 {print $5}' file

This User Gave Thanks to protocomm For This Post:
# 3  
Old 04-08-2015
Other way

Code:
$ 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

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 04-08-2015
Wow, thank you protocomm and Akshay Hegde for your help!Smilie

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

Regards,
Intan
# 5  
Old 04-08-2015
Quote:
Originally Posted by refrain
Wow, thank you protocomm and Akshay Hegde for your help!Smilie

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

Regards,
Intan

Code:
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.
This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 04-08-2015
Thank you so much, Akshay Hegde!Smilie
# 7  
Old 04-08-2015
Quote:
Originally Posted by refrain
Wow, thank you protocomm and Akshay Hegde for your help!Smilie

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

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

Code:
-F "[/,]"

Set Field Input Separator to /,

Code:
NR>1

skip the first line

Code:
{print $5}

print the fifth field
This User Gave Thanks to protocomm For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #892
Difficulty: Medium
sed scripts cannot have comments.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing string from CSV file by provide removal string from other file

What I need is to remove the text from Location_file.txt from each line matching all entries from Remove_location.txt Location_file.txt FlowPrePaid, h3nmg1cm2,Jamaica_MTAImageFileFlowPrePaid,h0nmg1cm1, Flow_BeatTest,FlowRockTest FlowNewTest,FlowNewTest,h0nmg1cm1 PartiallySubscribed,... (3 Replies)
Discussion started by: ketanraut
3 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Shell Programming and Scripting

Print particular string in a field of csv file - part 2

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... (7 Replies)
Discussion started by: refrain
7 Replies

4. Shell Programming and Scripting

awk - how to print specific field if a string is matched

hi gurus, I would like to be able to use awk to process 1 file as such: abc 1 2 3 4 5 6 7 8 9 10 flags 1 2 4 flags 1 2 5 abc 2 3 4 5 6 7 8 9 10 11 flags 1 2 3 abc 4 5 6 7 8 9 6 7 78 89 flags 1 2 3 flags 1 2 4 flags 1 2 3 4 I would like to be able to print field 1 and 5 when the... (4 Replies)
Discussion started by: revaroo
4 Replies

5. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

6. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

7. Shell Programming and Scripting

Update field value on a csv file

Hi I have a job status csv file. I want to update the status of the job in the file. Below is the csv file 1,jobname1,in_progress,starttime,somthing,somthing 2,jobname2,completed,starttime,somthing,somthing 3,jobname3,failed,starttime,somthing,somthing... (8 Replies)
Discussion started by: midhun19
8 Replies

8. Shell Programming and Scripting

How to add text to a field within a csv file

I have a csv file which has three columns mem no. name surname 1234 John Smith 12345 John Doe I want to change the mem no. to add TF to the mem no. field i.e. mem no. name surname 1234TF John Smith 12345TF John Doe How do you do this for all records in the file? (3 Replies)
Discussion started by: Pablo_beezo
3 Replies

9. Shell Programming and Scripting

replace a field in a CSV file

Hello all, I've a CSV file and need to replace 5th field if its value is "X". The exact requirement is to replace 5th field (column) with "Y" if a. it's value is "X" AND b. the line must start with ABC string i guess this can be done with awk. Pl help. For security reasons, the... (2 Replies)
Discussion started by: prvnrk
2 Replies

10. UNIX for Dummies Questions & Answers

How to insert ' in a field in CSV file

print "count,pub,prodline,group,sector,date,source" > $fname cat sp_log.summary.$firstday-$lastday.ProdlineSector | sed "s/^ *//g;s/ *$//g" >sp_log.summary.$firstday-$lastday cat sp_log.summary.$firstday-$lastday | sed "s/$/ $lastyy-$lastmm cache/;s/ /,/g" >> $fname cat $fname | sed... (1 Reply)
Discussion started by: shikhakaul
1 Replies

Featured Tech Videos