Find group of words in a text file and extract the line to new text file
my file ref.txt contains word "hi" "hello" "aloha" as per below:
hi
hello
aloha
And I have one more file abc.txt which contains many words including the above 3 words.
Now I developed a powershell batch to search the worlds in abc.txt and extract the line containing the words to a new file done.txt. i use -match command to find the word.
How to use the file ref.txt which contains the words for the finding, instead of declare the words in coding? Its fine if its in cmd.exe or powershell coding. Kindly enlighten me.
$source = "C:tempabc.txt"
$destination = "C:tempdone.txt"
$hits = select-string -Path $source -SimpleMatch "hi","hello","aloha"
$filecontents = get-content $source
foreach($hit in $hits) {
$filecontents[$hit.linenumber-1]| out-file -append
$destination "" |out-file -append $destination
}
powershell batch-file
add a comment |
my file ref.txt contains word "hi" "hello" "aloha" as per below:
hi
hello
aloha
And I have one more file abc.txt which contains many words including the above 3 words.
Now I developed a powershell batch to search the worlds in abc.txt and extract the line containing the words to a new file done.txt. i use -match command to find the word.
How to use the file ref.txt which contains the words for the finding, instead of declare the words in coding? Its fine if its in cmd.exe or powershell coding. Kindly enlighten me.
$source = "C:tempabc.txt"
$destination = "C:tempdone.txt"
$hits = select-string -Path $source -SimpleMatch "hi","hello","aloha"
$filecontents = get-content $source
foreach($hit in $hits) {
$filecontents[$hit.linenumber-1]| out-file -append
$destination "" |out-file -append $destination
}
powershell batch-file
I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
– dbenham
Mar 20 '15 at 14:19
add a comment |
my file ref.txt contains word "hi" "hello" "aloha" as per below:
hi
hello
aloha
And I have one more file abc.txt which contains many words including the above 3 words.
Now I developed a powershell batch to search the worlds in abc.txt and extract the line containing the words to a new file done.txt. i use -match command to find the word.
How to use the file ref.txt which contains the words for the finding, instead of declare the words in coding? Its fine if its in cmd.exe or powershell coding. Kindly enlighten me.
$source = "C:tempabc.txt"
$destination = "C:tempdone.txt"
$hits = select-string -Path $source -SimpleMatch "hi","hello","aloha"
$filecontents = get-content $source
foreach($hit in $hits) {
$filecontents[$hit.linenumber-1]| out-file -append
$destination "" |out-file -append $destination
}
powershell batch-file
my file ref.txt contains word "hi" "hello" "aloha" as per below:
hi
hello
aloha
And I have one more file abc.txt which contains many words including the above 3 words.
Now I developed a powershell batch to search the worlds in abc.txt and extract the line containing the words to a new file done.txt. i use -match command to find the word.
How to use the file ref.txt which contains the words for the finding, instead of declare the words in coding? Its fine if its in cmd.exe or powershell coding. Kindly enlighten me.
$source = "C:tempabc.txt"
$destination = "C:tempdone.txt"
$hits = select-string -Path $source -SimpleMatch "hi","hello","aloha"
$filecontents = get-content $source
foreach($hit in $hits) {
$filecontents[$hit.linenumber-1]| out-file -append
$destination "" |out-file -append $destination
}
powershell batch-file
powershell batch-file
edited Mar 20 '15 at 14:11
asked Mar 20 '15 at 13:56
Poobalan
1115
1115
I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
– dbenham
Mar 20 '15 at 14:19
add a comment |
I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
– dbenham
Mar 20 '15 at 14:19
I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
– dbenham
Mar 20 '15 at 14:19
I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
– dbenham
Mar 20 '15 at 14:19
add a comment |
2 Answers
2
active
oldest
votes
From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.
findstr /g:ref.txt abc.txt > done.txt
The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.
/R - Use regex
/I - Case insensitive search
/B - Match at the beginning of the line only
/E - Match at the end of the line only
So if you only wanted to match words in the file and not match a word in the sentence, you could do
findstr /BE /g:ref.txt abc.txt > done.txt
add a comment |
You can use powershell command get-content
It will get you a string that you can split on n
to get each line separately.
PS C:> get-content c:ref.txt
hi
hello
aloha
Split it and save in an array. Then perform action of your choice for each item by using
foreach ($element in $array)
{
/your code here/
}
You can find more information here.
If you want to use all three words together for search match, just replace the n
with ,
and place in your command after -SimpleMatch
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f891914%2ffind-group-of-words-in-a-text-file-and-extract-the-line-to-new-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.
findstr /g:ref.txt abc.txt > done.txt
The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.
/R - Use regex
/I - Case insensitive search
/B - Match at the beginning of the line only
/E - Match at the end of the line only
So if you only wanted to match words in the file and not match a word in the sentence, you could do
findstr /BE /g:ref.txt abc.txt > done.txt
add a comment |
From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.
findstr /g:ref.txt abc.txt > done.txt
The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.
/R - Use regex
/I - Case insensitive search
/B - Match at the beginning of the line only
/E - Match at the end of the line only
So if you only wanted to match words in the file and not match a word in the sentence, you could do
findstr /BE /g:ref.txt abc.txt > done.txt
add a comment |
From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.
findstr /g:ref.txt abc.txt > done.txt
The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.
/R - Use regex
/I - Case insensitive search
/B - Match at the beginning of the line only
/E - Match at the end of the line only
So if you only wanted to match words in the file and not match a word in the sentence, you could do
findstr /BE /g:ref.txt abc.txt > done.txt
From what I understand in your question, you want to use the words in ref.txt as search criteria, so that if you have a file abc.txt that contains these words, the lines containing the words will be returned in a file done.txt. This can be accomplished simply in cmd.exe in one of a few ways.
findstr /g:ref.txt abc.txt > done.txt
The above solution would return any line that contains any of the words (even if they are in a sentence or part of another word, such as hi and high), but would not be case insensitive.
/R - Use regex
/I - Case insensitive search
/B - Match at the beginning of the line only
/E - Match at the end of the line only
So if you only wanted to match words in the file and not match a word in the sentence, you could do
findstr /BE /g:ref.txt abc.txt > done.txt
answered Mar 20 '15 at 14:21
Wolvendeer
1064
1064
add a comment |
add a comment |
You can use powershell command get-content
It will get you a string that you can split on n
to get each line separately.
PS C:> get-content c:ref.txt
hi
hello
aloha
Split it and save in an array. Then perform action of your choice for each item by using
foreach ($element in $array)
{
/your code here/
}
You can find more information here.
If you want to use all three words together for search match, just replace the n
with ,
and place in your command after -SimpleMatch
add a comment |
You can use powershell command get-content
It will get you a string that you can split on n
to get each line separately.
PS C:> get-content c:ref.txt
hi
hello
aloha
Split it and save in an array. Then perform action of your choice for each item by using
foreach ($element in $array)
{
/your code here/
}
You can find more information here.
If you want to use all three words together for search match, just replace the n
with ,
and place in your command after -SimpleMatch
add a comment |
You can use powershell command get-content
It will get you a string that you can split on n
to get each line separately.
PS C:> get-content c:ref.txt
hi
hello
aloha
Split it and save in an array. Then perform action of your choice for each item by using
foreach ($element in $array)
{
/your code here/
}
You can find more information here.
If you want to use all three words together for search match, just replace the n
with ,
and place in your command after -SimpleMatch
You can use powershell command get-content
It will get you a string that you can split on n
to get each line separately.
PS C:> get-content c:ref.txt
hi
hello
aloha
Split it and save in an array. Then perform action of your choice for each item by using
foreach ($element in $array)
{
/your code here/
}
You can find more information here.
If you want to use all three words together for search match, just replace the n
with ,
and place in your command after -SimpleMatch
answered Mar 20 '15 at 14:24
mnmnc
3,31311224
3,31311224
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f891914%2ffind-group-of-words-in-a-text-file-and-extract-the-line-to-new-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't see how a SimpleMatch helps you. I don't think you want "hi" to match "hill". I suspect you only want to match whole words.
– dbenham
Mar 20 '15 at 14:19