Combined sed+awk for lookup csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combined sed+awk for lookup csv file
# 1  
Old 06-24-2014
Combined sed+awk for lookup csv file

have written a combined sed+awk to perform a lookup operation which works but looking to enhance it.

looking to match a record using any of the comma separated values + return selected fields from the record - including the field header. so:

Code:
cat foo
make,model,engine,trim,value
bmw,530,3.0,sport,20000
merk,E180,1.8,amg,16000

current code & result (working):

sed -n '1p;/merk/p' foo|awk -F, 'NR==1{for(i=1;i<=NF;i++)if($i==v)c=i};NR>1{print $1" "$c}' v=value
merk 16000

desired result:

make, value
merk, 16000

note the fields i want to return will not always be the same so i want the ability to pass them in the command
# 2  
Old 06-24-2014
How about:
Code:
awk -v s="merk" 'NR==1 || $0~s{print $1, $NF}' FS=, OFS=, foo

---
If you required an exact match of an entire field, perhaps:
Code:
awk -v s="merk" 'NR==1{f=1} {for(i=1; i<=NF; i++) if($i==s) f=1} f{print $1, $NF; f=0}' FS=, OFS=, foo


Last edited by Scrutinizer; 06-24-2014 at 08:53 AM..
# 3  
Old 06-24-2014
thanks but this wont return the headers "make, value"
# 4  
Old 06-24-2014
Not sure .. I guess you want something like this right ? sorry If I misunderstood..

Code:
[akshay@nio tmp]$ cat header 
make
value

Code:
[akshay@nio tmp]$ cat file
make,model,engine,trim,value
bmw,530,3.0,sport,20000
merk,E180,1.8,amg,16000

Code:
[akshay@nio tmp]$ cat parse.awk 
NR==FNR{
                Cols = Cols (length(Cols)?"|":"")$1
                next
       }
 FNR==1{
                for (i=1;i<=NF;i++) 
                if (match($i,Cols)) 
                Ar[++n]=i
       }

       {
               for (i=1;i<=n;i++) 
               printf (i<n)? $(Ar[i])  OFS : $(Ar[i])
               printf "\n"
       }

Code:
[akshay@nio tmp]$ awk -vFS="," -vOFS="," -f parse.awk  header  file
make,value
bmw,20000
merk,16000

# 5  
Old 06-24-2014
Yes, it will. I had made a small adaptation in the mean time.
# 6  
Old 06-24-2014
Quote:
Originally Posted by Scrutinizer
How about:
Code:
awk -v s="merk" 'NR==1 || $0~s{print $1, $NF}' FS=, OFS=, foo

---
If you required an exact match of an entire field, perhaps:
Code:
awk -v s="merk" 'NR==1{f=1} {for(i=1; i<=NF; i++) if($i==s) f=1} f{print $1, $NF; f=0}' FS=, OFS=, foo

second one works. only thing missing is ability to tell it what field you want to return by supplying the field name not the field position i.e

match merk, return make model
or
match merk, return make engine
# 7  
Old 06-24-2014
The first one should also work (that was the one I made the adaptation to), but it will return the last field only..

To specify the field, you can try this adaptation to your own code:
Code:
awk -F, 'NR==1{for(i=1;i<=NF;i++)if($i==v)c=i};NR==1 || $0~s{print $1, $c}' FS=, OFS=, s=merk v=engine foo

or
Code:
awk 'NR==1{split($0,F); for(i in F) N[F[i]]=i} NR==1 ||$0~s{print $1, $N[v]}' FS=, OFS=, s=merk v=engine foo

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk and sed script to create one output CSV file

Hi All , I would require your help to generate one output file after post processing of one CSV file as stated below This file is just a small cut from a big file . Big file is having 20000 lines PATTERN,pat0,pat1,pat2,pat3,pat4,pat5,pat6,pat7,pat8,pat9... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

Datestamp format 2nd change in csv file (awk or sed)

I have a csv file formatted like this: 2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07and I'm trying to change it to MM/DD/YYYY HH:MM for both occurances. I have got this: awk -F, 'NR <=1 {print;next}{"date +%d/%m/%Y\" \"%H:%m -d\""$1 "\""| getline dte;$1=dte}1' OFS="," test.csvThis... (6 Replies)
Discussion started by: say170
6 Replies

3. Shell Programming and Scripting

Replacing FQDN by hostnames in a CSV file with sed & awk

Hello, Beginning with shell scipting, I'm trying to find in a csv file, the lines where the field related to hostname is displayed as an FQDN intead the hostname. (some lines are correct) and the to correct that inside the file: Novell,11.0,UNIX Server,bscpsiws02,TxffnX1tX1HiDoyBerrzWA==... (2 Replies)
Discussion started by: Wonto
2 Replies

4. Shell Programming and Scripting

Converting txt file into CSV using awk or sed

Hello folks I have a txt file of information about journal articles from different fields. I need to convert this information into a format that is easier for computers to manipulate for some research that I'm doing on how articles are cited. The file has some header information and then details... (8 Replies)
Discussion started by: ksk
8 Replies

5. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

6. Shell Programming and Scripting

Combined Two CSV Lines

I have two CSV lines, I.e.: Line 1 = the,quick,brown,fox, ,jumps, ,the, ,dog Line 2 = the,quick,brown,fox, , ,over, ,lazy,dog Literally, columns missing from line 1 exist in line 2. Any suggestions on quick ways to combined these two lines into one line: New line:... (2 Replies)
Discussion started by: msf004
2 Replies

7. Shell Programming and Scripting

awk/sed/something else for csv file

Hi, I have a filename.csv in which there are 3 colums, ie: Name ; prefixnumber ; number root ; 020 ; 1234567 user1,2,3 ; 070 ; 7654321 What I want is to merge colum 2 and 3 that it becomes 0201234567 or even better +31201234567 so the country number is used and drop the leading 0.... (9 Replies)
Discussion started by: necron
9 Replies

8. Shell Programming and Scripting

Using awk/sed in handling csv file.

Please study the below script and the output Script: echo "Minimum ${host} ${process} response time=${min} ms" >> ${OUTDIR}/${OUTFILE}; echo "Maximum ${host} ${process} response time=${max} ms" >> ${OUTDIR}/${OUTFILE}; echo "Average ${host} ${process} response time=${avg} ms" >>... (0 Replies)
Discussion started by: ajincoep
0 Replies

9. Shell Programming and Scripting

Need help with a script to process a CSV file using SED and AWK

I get a CSV file every day with 2 columns and multiple rows ex: date1,date2 ( both the fields are varchar fields) This data has to be updated in a table which is being done manually and i want to automate that. 1. I have to select all the data from the prod table( 2 columns { date1,date2}) into... (4 Replies)
Discussion started by: kkb
4 Replies

10. Shell Programming and Scripting

Updating a line in a large csv file, with sed/awk?

I have an extremely large csv file that I need to search the second field, and upon matches update the last field... I can pull the line with awk.. but apparently you cant use awk to directly update the file? So im curious if I can use sed to do this... The good news is the field I want to... (5 Replies)
Discussion started by: trey85stang
5 Replies
Login or Register to Ask a Question