Extract Values from CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract Values from CSV
# 1  
Old 04-08-2009
Extract Values from CSV

Hi,

I need to extract values from a CSV file based on some conditions as explained below:

File format details:

1. each set starts with AAA only
2. number of columns is fixed
3. number of rows per set may vary (as they are having different CCC rows)

Now, i need to extract 3rd column of AAA and 4th columns of the lines starting with CCC of that set. These values of 1 dataset to form one line.

For. e.g, i have data for 3 sets and file has the below:

AAA,1,a,b,c,d
BBB,1,j,k,l,m
CCC,1,p,q,r,s
CCC,1,w,x,y,z
AAA,2,j,k,l,m
BBB,2,a,b,c,d
CCC,2,p,q,r,s
AAA,3,w,x,y,z
BBB,3,a,b,c,d
CCC,3,p,q,r,s
CCC,3,m,n,o,p
CCC,3,i,j,k,l

then the output must be (3 lines only as 3 data sets exist)

a,q,x
j,q
w,q,n,j


Please advise.

Thanks
Prvn
# 2  
Old 04-08-2009
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk -F, 'END { print _ }
/^(A|C)/ { 
  if (/^A/ && _) { print _; _ = "" }  
  _ = _ ? _ FS $4 : $3 
  }' infile

# 3  
Old 04-08-2009
something to start with:
nawk -F, -f prv.awk OFS=, myFile

prv.awk:
Code:
$1=="AAA" { printf("%c%s",(FNR==1)?"":ORS, $3); next }
$1=="CCC" { printf("%c%s",OFS, $4)}
END{print}

Ah, radoulov's solution is much nicer - congrats!!!
# 4  
Old 04-08-2009
Thank you radoulov and vgersh99,

I tested radoulov's solution and worked great. Thank you very much!!


Prvn
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract last cell of csv file

How do I extract the last cell in a column of a csv file using linux shell scripting? Or alternatively, how do I get the number of cells of a csv file? (2 Replies)
Discussion started by: locoroco
2 Replies

2. UNIX for Dummies Questions & Answers

Extract a certain field from a CSV?

EDIT: This problem has been solved thanks to the help of scottn. Okay, so I have a CSV. Let's say it has the following entries in it: Jackie Chan,1954,M Chuck Norris,1930,M Bruce Lee,1940,M How would I, for example, extract the gender out of a certain person, maybe based on the year of... (12 Replies)
Discussion started by: chickeneaterguy
12 Replies

3. Shell Programming and Scripting

How to extract data from csv file

Hello everybody, Here is my problem, I don't know anything about shell programming and my boss is actually asking me to develop a shell script in order to get values in a csv file from a specific date. Here is a sample of the csv file : Date;Enchaînement;Titre;Libellé ;calendrier;Heure début;Heure... (11 Replies)
Discussion started by: freyr
11 Replies

4. Shell Programming and Scripting

extract pattern from csv file

hi, i have a situation where i have a csv file in the format date,name eg: 1284631889869,a 1284631889879,b 1284631889459,c . . . . . . . now i take a time and an interval from user. (5 Replies)
Discussion started by: niteesh_!7
5 Replies

5. Shell Programming and Scripting

Extract CSV records using NAWK?

Example CSV: $ cat myfile HDR COL_A,COL_B,COL_C X,Y,Z Z,Y,X ... X,W,Z In this example, I know that column names are on the second line. I also know that I would like to print lines where COL_A="X" and COL_C="Z". In this simple example, I know that COL_A = $1 and COL_C = $3, and hence... (6 Replies)
Discussion started by: cs03dmj
6 Replies

6. Shell Programming and Scripting

extract .csv file

Hi all I am new to unix . I need to write a script that extracts some data from oracle into a .csv file with heading of the columns in the file SO i created the following two scripts but they are not working ac.sql (this is the sql file that i will call inside the shell script when i run... (1 Reply)
Discussion started by: rajesh_tns
1 Replies

7. Shell Programming and Scripting

extract csv based on column value

Hi I have a csv file which is below A,5 B,6 C,10 D,7 I want the values who's second column is greater than 7 say C,10 D,7 Help me please... Thanks, Maruth (3 Replies)
Discussion started by: maruthavanan
3 Replies

8. Shell Programming and Scripting

Need to compare values on two CSV files

:( Hello, Having a problem with reading two files using awk/nawk, am new to both them. I need to compare field values between two csv files and arrange for an appropriate output if both the values are equal or not for each feild. $cat File1.csv... (4 Replies)
Discussion started by: pgop
4 Replies

9. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

10. Shell Programming and Scripting

Extract info from csv

I have some input file, which contains some lines which are comma separated. Eg. a,b,id=999],d d,f,g,id=345],x x,y,x,s,id=677],y I run a loop to read the lines one by one. What i want is to extract the value on the right of id=. I cannot do it by Awk, since the column number is not fixed.... (5 Replies)
Discussion started by: indianjassi
5 Replies
Login or Register to Ask a Question