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


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

Code:
SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw
/home/intannf/nu/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92
/home/intannf/up/folder/new/IMAG0399.JPG,1.14,-7.78805,110.37457,30.53,997.44,139.93,186.60,175.61,184.11
/home/intannf/me/DSC122.jpg,0.80,-7.78805,110.37457,30.64,998.83,127.60,171.19,227.02,7.66

Output required
Code:
2015_0313_090651_219.JPG
IMAG0399.JPG
DSC122.jpg

When i tried to print them using this code:
Code:
awk -F "[/,]" 'NR>1 {print $5}' $file

I get the wrong result like this:
Code:
2015_0313_090651_219.JPG
new
0.80

because it just print the fifth column, and each of them has different column.
Would you mind to help me to solve it? I really need your help. Thanks in advance.

Regards,
Intan
# 2  
Old 04-23-2015
Try
Code:
awk -F, 'NR>1 {sub(/.*\//,"", $1); print $1}' file2
2015_0313_090651_219.JPG
IMAG0399.JPG
DSC122.jpg

This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-23-2015
Hi RudiC,

thanks for your help!
But i don't know yet about this code
Code:
sub(/.*\//,"", $1)

Would you mind to tell me about it? Thanks in advance
# 4  
Old 04-23-2015
From GNU awk manual:

Quote:
sub(regexp, replacement [, target])

Search target, which is treated as a string, for the leftmost, longest substring matched by the regular expression regexp.
Modify the entire string by replacing the matched text with replacement. The modified string becomes the new value of target.
Return the number of substitutions made (zero or one).
# 5  
Old 04-24-2015
Hi balajesuri,
I am still getting confused about this code. What does it mean?
Code:
/.*\//,""

# 6  
Old 04-24-2015
Unless you don't have gawk
read file ; drop first line ; print everything before first , ; print last field after /
Code:
cat inputfile | tail +2 | awk -F,  '{ print $1}'  | awk -F/  '{ print $NF}'

This User Gave Thanks to garydeena For This Post:
# 7  
Old 04-24-2015
Quote:
Originally Posted by refrain
Hi balajesuri,
I am still getting confused about this code. What does it mean?
Code:
/.*\//,""

It means replace the longest string of characters (.*) that ends with a / (\/) and replace it with nothing (""), i.e. delete it...

The other two slashes are used to demarcate the extended regular expression .*\/
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

2. Shell Programming and Scripting

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

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

Print part of string

I have a file called file.txt It contains strings: ALT=someone@acme.com TO=whoever@lalalulu.com How could find and print the actual address after the = sign for any given instance? I need the command to print one of them - for example someone@acme.com But have in mind that this... (3 Replies)
Discussion started by: svetoslav_sj
3 Replies

6. Shell Programming and Scripting

Remove part of a string from second field

I have a file in below format (pipe delimited): 1234__abc|John__abc|xyz 3345__abc|Kate__abc|xyz 55344|Linda__abc|xyz 33434|Murray|xyz I want to remove any occurence of "__abc" in the second field of this file. I did some research and found a way to replace the entire second field with... (5 Replies)
Discussion started by: rajv
5 Replies

7. Shell Programming and Scripting

Select a specific part of the string and print it

Hi all, I have a string that looks like: #!/bin/sh options="arguments: --user=alpha --group=beta --prefix=/usr/share --proxy-path=/proxy --proxy-tmp=/tmp --conf-path=/etc" My goal is to transform the string into an array, then for each key, if it starts with "--proxy" to print the string... (2 Replies)
Discussion started by: TECK
2 Replies

8. UNIX for Dummies Questions & Answers

cut and print part of a string

I have a file that contains: yahoo.com.23456 web.log.common.us.gov.8675 192.168.1.55.34443 john-doe.about.com.22233 64.222.3.4.120 sunny.ca.4442 how can i remove the strings after the last dot (.) and reprint the file? Thanks. (3 Replies)
Discussion started by: apalex
3 Replies

9. Shell Programming and Scripting

Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any... (4 Replies)
Discussion started by: stakuri
4 Replies
Login or Register to Ask a Question