Reading a csv file using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a csv file using shell script
# 1  
Old 09-20-2012
Reading a csv file using shell script

Hello All,
I have a csv file that looks like below

Code:
ProdId_A,3.3.3,some text,some/text,sometext_1.2.3
ProdId_B,3.3.3,some text,some/text,sometext_1.2.3
ProdId_C,3.3.3,some text,some/text,sometext_1.2.3
ProdId_A,6.6.6,some text,some/text,sometext_9.9.9

I will get ProdId from user.I need to write a script that will find a match from entries above.Search should start from last entry and go upward.

For example if I get "ProdId_A" from user I should be able to get other values like 6.6.6 , 9.9.9.

Thanks in advance !!!

Last edited by Scrutinizer; 09-20-2012 at 07:22 AM.. Reason: code tags
# 2  
Old 09-20-2012
Code:
prod='ProdId_A';
awk -F, -v what="$prod" '$1==what{split($5,b,"_");a=$2 FS b[2]}END{print a}' file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-21-2012
Dear
Can you please elaborate your answer ???? If I want to hold other column values of same row,How can I do it ???E.g. In above csv file, if I want to hold 6.6.6. and sometext_9.9.9, HOw can I do it ????
# 4  
Old 09-21-2012
The awk command searches for a record whose first field ($1) matches the variable value supplied (variable what containing ProdId_A). For each such record, the split function is used to get, e.g., 9.9.9 from sometext_9.9.9. This combination (field 2 and the value from the last operation) is stored in the variable a. This is printed at the end of the file to emulate searching the first record from the bottom of the file.
This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 09-21-2012
Hey friend,
Thank u very much !!! the problem is I am not able to assign the value of "a" to any variable neither "a" is accessible outside the END block.
# 6  
Old 09-21-2012
Quote:
Originally Posted by anand.shah
Hello All,
I have a csv file that looks like below

Code:
ProdId_A,3.3.3,some text,some/text,sometext_1.2.3
ProdId_B,3.3.3,some text,some/text,sometext_1.2.3
ProdId_C,3.3.3,some text,some/text,sometext_1.2.3
ProdId_A,6.6.6,some text,some/text,sometext_9.9.9

I will get ProdId from user.I need to write a script that will find a match from entries above.Search should start from last entry and go upward.

For example if I get "ProdId_A" from user I should be able to get other values like 6.6.6 , 9.9.9.

Thanks in advance !!!
Using the file text.txt with the above content I used these commands :

Code:
prod=ProdId_A
cat text.txt | grep $prod | cut -d "," -f2 | sort -r
6.6.6
3.3.3

And got this output. If I understood correctly what you want, otherwise you'll have to explain it better. However if the file is very large these shell commands will be very slow, and in this case I suggest to use/adapt the awk approach presented earlier in this thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

2. UNIX for Dummies Questions & Answers

C-Shell script help reading from txt file

I need to write a C-Shell script with these properties: It should accept two arguments on the command line. The first argument is the name of a file which contains a list of names, and the second argument is the name of a directory. For each file in the directory, the script should print the... (1 Reply)
Discussion started by: cerce
1 Replies

3. Shell Programming and Scripting

Reading from a CSV and writing in same CSV file

Hi, I am tryng to read from a csv file and based on some grep command output I will modify one of the column in the same csv. Example:- Input CSV:- 20120829001415,noneAA,google.com 20120829001415,dfsafds,google.com 20120829001415,noneAA,google.com Intermediate Step:- If 2nd column of... (3 Replies)
Discussion started by: kmajumder
3 Replies

4. Shell Programming and Scripting

Reading a property file through shell script???

Hi! i need a script that can read a property file. i.e., A script to read a "property" from property file. Read the property value and based on value of property, decide whether to start the some dataload activity or not. Its urngent. Can anyone help me out???:( (7 Replies)
Discussion started by: sukhdip
7 Replies

5. Shell Programming and Scripting

Reading the data from CSV and performing search through shell script

Hello, I am working on building a script that does the below actions together in my Linux server. 1) First, have to read the list of strings mentioned in CSV and store it in the shell script 2) Second, pick one by one from the string list, and search a particular folder for files that... (2 Replies)
Discussion started by: vikrams
2 Replies

6. Shell Programming and Scripting

Reading the Properties File From Shell script

Hi, I am new to the shell script please I need help for following question. I have properties file name called "com.test.properties" I have No of key values in this properties. com.person.name = xyz com.person.age = 55 com.person.address = hello I want read this properties but i... (1 Reply)
Discussion started by: venukjs
1 Replies

7. Shell Programming and Scripting

File reading problem via shell script

Hi, Data file named parameter contains : DB=y Alter_def.sql Create_abc.sql SQL=y database.sql my_data.sql To read this file I use var_sql=$(awk -F= '$1 == "SQL" { print $2 }' parameter.txt) if then sql_f_name=`grep "\.sql" parameter.txt` echo $sql_f_name fi (2 Replies)
Discussion started by: Dip
2 Replies

8. Shell Programming and Scripting

file reading through shell script

For reading a file through shell script I am using yhe code : while read line do echo $line done<data.txt It reads all the line of that file data.txt. Content of data.txt looks like: code=y sql=y total no of sql files=4 a.sql b.sql c.sql d.sql cpp=n c=y total no of c files=1 (4 Replies)
Discussion started by: Dip
4 Replies

9. Shell Programming and Scripting

Script for reading .csv file

Can someone please help me to write script for following scenario : 1> script should read a input .csv file of format : EmpName, PF, Leave, Basic ,HRA 2> another config file ( may be again a .csv file ) has format EmpName and EmpID 3> script should read another config file for each EmpName in... (3 Replies)
Discussion started by: creativeworld
3 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question