This question already has answers here:
Inner join on two text files
(5 answers)
Closed 2 years ago.
I have 2 big .csv file. I want to extract content of file 2 that there in file 1.
for example:
file1:
A1BG 1
NAT1 9
NAT2 10
SERPINA3 12
AAMP 14
AANAT 15
AARS1 16
file 2:
1 10422
1 10549
1 2232
1 23198
1 23352
1 284403
1 368
1 51035
1 80854
1 9923
2 10053
2 10376
2 10724
2 2026
2 2193
2 22976
2 23154
2 24138
2 2639
2 284207
2 285203
2 3337
2 3437
2 348
2 348
2 348
2 351
4 7689
output:
1 10422
1 10549
1 2232
1 23198
1 23352
1 284403
1 368
1 51035
1 80854
1 9923
it is my code:
awk 'NR==FNR{FS=" ";a[$2];next}{FS=" ";if ($1 in a) print $0}' <file1.csv <file2.csv >output.csv
but I have no output.
I believe you are simply looking for this solution in awk.
awk 'FNR==NR{a[$2];next} ($1 in a)' Input_file1 Input_file2
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
a[$2] ##Creating array a with index of 2nd field of current line.
next ##next will skip all further statements from here.
}
($1 in a) ##Checking condition if 1st field is present in array a then print that line from Input_file2
' Input_file1 Input_file2 ##Mentioning Input_file names here.
Let's look at your code:
awk 'NR==FNR{FS=" ";a[$2];next}{FS=" ";if ($1 in a) print $0}' <file1.csv <file2.csv >output.csv
You are redirecting input from 2 files. The shell can only have a single source of data for each file descriptor: the shell processes redirections from left to right as they are seen on the command line.
Try this:
awk 'NR==FNR' <file1.csv <file2.csv
and you'll probably be surprised at what awk considers the "first file".
awk is fully capable of reading files, you don't need the shell to do that:
awk 'NR==FNR' file1.csv file2.csv
I am uploading a huge csv file with the following code:
LOAD DATA LOCAL INFILE 'file.csv'
INTO TABLE signal_vv.Action
CHARACTER SET latin1
FIELDS TERMINATED BY ','
-- ESCAPED BY '\b'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(#visitdatetime,usersessionid,probability,#spottime,network,reqion,creative,origin,Region,
t_usersessions_cuserref,t_usersessions_useraddressid,country,isp,UserId,SessionRefID,
source_,t_usersessions_userrefid,postcode,appsessionid,UserIP,ServerAddress,Medium_,device,AdSpotID)
SET
visitdatetime= STR_TO_DATE(#visitdatetime,'%Y%m%d-%H:%i:%s'),
spottime=STR_TO_DATE(#spottime,'%Y%m%d-%H:%i:%s')
;
when I run it I got this error :
Error Code: 1054. Unknown column 'probability' in 'field list' 0.032 sec
the first 5 rows looks are:
visitdatetime,usersessionid,probability,spottime,network,reqion,creative,origin,Region,t_usersessions_cuserref,t_usersessions_useraddressid,country,isp,UserId,SessionRefID,source_,t_usersessions_userrefid,postcode,appsessionid,UserIP,ServerAddress,Medium_,device,AdSpotID
01/10/2016 06:14,13403176,0.009460106,01/10/2016 06:14,Movies 4 Men 1,national,VCCOOSI990030,None,GB/london/london,3.11137E+13,None,GB,TELEFONICAO2UK,None,2744429,None,None,None,None,82.132.238.96,None,web,None,41510
01/10/2016 06:14,13406873,0.009460106,01/10/2016 06:14,Movies 4 Men 1,national,VCCOOSI990030,None,GB/london/london,31148fc9500c58,None,GB,BT,None,2901890,None,None,SE10,None,109.147.90.149,None,web,None,41510
01/10/2016 06:14,13618866,0.009460106,01/10/2016 06:14,Movies 4 Men 1,national,VCCOOSI990030,None,GB/london/london,3191b1407c367e,None,GB,TELEFONICAO2UK,None,3063053,None,None,None,None,82.132.241.240,None,web,None,41510
01/10/2016 06:14,13407385,0.009460106,01/10/2016 06:14,Movies 4 Men 1,national,VCCOOSI990030,None,GB/london/london,3136d33c60e4c6,None,GB,TELEFONICAO2UK,None,2622421,None,None,None,None,82.132.222.151,None,web,None,41510
01/10/2016 06:14,13361612,0.009460106,01/10/2016 06:14,Movies 4 Men 1,national,VCCOOSI990030,None,GB/london/london,307808800c066f,None,GB,TELEFONICAO2UK,None,2805769,None,None,None,None,82.132.222.11,None,web,None,41510
The table has the field probability (double).
Could anybody help me with that?
thanks
Check the column name of your table.
Your LOAD DATA could be considering a .CSV that has a column with name "probability" and your table in MySQL could have a column with name "probabillity".
I have trying to build a table from a condition existing in another table. If the condition exists I want to pull data from a different table to populate the new table. Here is the SQL statements I am using, however it is populating the new table with the wrong data.
INSERT INTO 2014TranstarPriceTnum (tran_no)
SELECT Tran_no
FROM Trans_Types_Updated
WHERE 2014TranstarPriceTtype.`tran_type`=Trans_Types_Updated.`Tran_Type`;
I am looking in 2014TranstarPriceTtype for a Trans Type that exists for the and pull all the associated tran_no's from Trans_Types_Updated and populate 2014TranstarTnum. However, my script resulted in the Tran_Type populated into the tran_no field.
2014TranstarPriceTtype
Tprice tran_type make core note Updated
650 125C BUICK 250 (P.H.B.) 2014-01-07
650 200C BUICK 250 (P.H.B.) 2014-01-07
850 2004R BUICK 350 (P.H.B.) 2014-01-07
650 350 Chev 250 (P.H.B.) 2014-01-07
Trans_Type_Updated
Tran_No Tran_Type Make Eng_size
T1014AA 125C BUICK 2.5
T1006AA 125C BUICK 2.5
T1363AA 2004R BUICK 5.0
T1365AA 2004R BUICK 5.0
T1310AA 200C BUICK 3.8 (231)
T1318AA 200C BUICK 5.0
T1427CA 350C CHEVROLET 5.0
T1427AA 350C CHEVROLET 5.0
Results in 2014TranstarPriceTnum
tran_no Tprice tran_type make eng_size core_tote note updated
125C \N \N \N \N \N \N \N
200C \N \N \N \N \N \N \N
2004R \N \N \N \N \N \N \N
350C \N \N \N \N \N \N \N
I would appreciated any help.
Thanks,
Tony Cripps
Hmm, not that clear, but I think you want something like that (not sure of your table names, you've got different versions).
INSERT INTO 2014TranstarPriceTnum (tran_no)
SELECT ttu.Tran_no
FROM Trans_Type_Updated ttu
INNER JOIN 2014TranstarPriceTtype tpt on tpt.tran_type = ttu.tran_type
see SqlFiddle
I have a table named 'table1' with the columns:
CSMembers,BCID,Total,Email.
The excel sheet contains the data for this table in this format:
CSMembers BCID Total Email
abc 2,5,7,9,12,17,22,32 10,000 abc#gmail.com
xyz 1,3,5,7,9,12,17,20,22,33 12,500 xyz#gmail.com
pqr 2,5,7,9,12,17,22,32 11,000 pqr#gmail.com
ttt 2,5,7,9,12,17,22 9,800 ttt#gmail.com
the .csv file of this is :
CSMembers,BCID,Total,Email
abc,"2,5,7,9,12,17,22,32","10,000",abc#gmail.com
xyz,"1,3,5,7,9,12,17,20,22,33","12,500",xyz#gmail.com
pqr,"2,5,7,9,12,17,22,32","11,000",pqr#gmail.com
ttt,"2,5,7,9,12,17,22","9,800",ttt#gmail.com
I have used the following code:
load data local infile 'H:/abc.csv' into table table1
fields terminated by ','
optionally enclosed by '"'
lines terminated by '\n' ignore 1 lines
(CSMembers,BCID,Total,Email);
I am getting the following output:
CSMember BCID Total Email
abc 2 10 abc#gmail.com
xyz 1 12 xyz#gmail.com
pqr 2 11 pqr#gmail.com
ttt 2 9 ttt#gmail.com
But i need this output:
CSMembers BCID Total Email
abc 2,5,7,9,12,17,22,32 10,000 abc#gmail.com
xyz 1,3,5,7,9,12,17,20,22,33 12,500 xyz#gmail.com
pqr 2,5,7,9,12,17,22,32 11,000 pqr#gmail.com
ttt 2,5,7,9,12,17,22 9,800 ttt#gmail.com
Can anyone please tell me what is wrong?
if I should change the code or the csv file content or both?
plese help.
Your schema for the table is possibly wrong bcid and total is probably defined as an int of some kind instead of a string. Numeric fields won't know whether a comma is a separator to another field or simply dividing the number up so it is easily readable. Also input to numeric fields typically accepts the value up to the first non-numeric character i.e. the comma
I have a simple query in MySQL
select AGE, NAME from Members;
which returns,
20 ABC
11 PQR
21 XYZ
16 REW
I need to conditionally select NAME from Members, i.e if AGE is less then 18 I need to print MINOR. So my output will be
20 ABC
11 MINOR
21 XYZ
16 MINOR
I know I should do this programmatically, but I am dumping the output directly to a file in CSV format using
INTO OUTFILE '$random_file_name' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'
What are my options in MySQL query itself to achieve this?
select AGE,
case when AGE < 18
then 'MINOR'
else NAME
end as NAME
from Members