Find and extract lines from csv file based on a list Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Find and extract lines from csv file based on a list

27 Aug 2022, 03:14

I searched everywhere and i tried everything but i can't do it myself so please help.
The files that i want to use are very big. For example the csv has 1.500.000 lines

I have a text file (with many other words) this is just an example;
un
mar
word
I have a csv with 1.500.000 lines this is just an example:
un;test0
un;othertest0
University;test1
United;test2
mar;test3
marketing;test4
marketing;test5
marvelous;test
margarita;othertest1
damar;justtest
ora;justtest
ora;nothing
tors;something
word;justtest1
word;testfour
word;othertestzero
wordread;dontfind
born;letit
What i want to do is:

Find/match exactly the words from the text file in the csv file and export them to a new csv file like:
un;test0
un;othertest0
mar;test3
word;justtest1
word;testfour
word;othertestzero
And not those:
University;test1
United;test2
marketing;test4
marketing;test5
marvelous;test
margarita;othertest1
damar;justtest
ora;justtest
ora;nothing
tors;something
wordread;dontfind
born;letit
So far i have this:

Code: Select all

#Persistent

fileread, AAAA, test1.csv
FileRead, BBBB, Temp.txt
GGGG := StrSplit(BBBB, [A_Space, "`r`n"], "`-")
For Key, Value in GGGG

;Value = Elias

Loop, parse, AAAA, `r, `n
{
  ifinstring, a_loopfield, % Value
  {
    result .= a_loopfield "`r`n"
  }
}
;MsgBox % result

FileDelete, All words found.csv
FileAppend %result%, All words found.csv, UTF-8
ExitApp
But it returns this:
un;test0
un;othertest0
University;test1 ; I don't want this
United;test2 ; I don't want this
mar;test3
marketing;test4 ; I don't want this
marketing;test5 ; I don't want this
marvelous;test ; I don't want this
margarita;othertest1 ; I don't want this
damar;justtest ; I don't want this
word;justtest1
word;testfour
word;othertestzero
wordread;dontfind ; I don't want this
Can someone improve the script;
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Find and extract lines from csv file based on a list  Topic is solved

27 Aug 2022, 05:12

replace your search-loop by

Code: Select all

;Value = Elias

Loop, parse, AAAA, `n, `r
{
  First := StrSplit(a_loopfield, ";")
  if (First.1 = Value)
  {
    result .= a_loopfield "`r`n"
  }
}
;MsgBox % result
Hubert
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Find and extract lines from csv file based on a list

27 Aug 2022, 05:46

From a quick test i thing it works fine hd0202. Thank you.
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find and extract lines from csv file based on a list

27 Aug 2022, 07:50

Hi.

The csv.ahk function has a language and level of understanding closer to everyone than fileread. https://github.com/hi5/CSV, But maybe it will be more complicated to implement it inside loop, parse.
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Find and extract lines from csv file based on a list

27 Aug 2022, 08:44

Yes i came across it as i was searching but i didn't manage to accomplish anything, not because it is bad, but because i don't know how to use it with autohotkey language. But i was very close in my attempt and it seems easy to understand it. I thing in the future it is going to be in a lot of use.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: arrondark and 159 guests