powershell - Seach for a word in a .txt files and write few lines into a new .txt file -


please me create script perform task per described below.

i have 2 files, a.txt , b.txt. content of a.txt below

 item name ticky title nice coffe drink type drink item name apple title sweet tasty apple type fruit item name juice title nice tasty drink type drink item name orang title niice nice orange type fruit item name chery title nutritious rich fruit type fruit 

now need search in a.txt word "fruit" , copy 2nd line on top of "fruit" new file named list.txt.

but need name of fruit, list.txt should looks below.

 apple orang chery 

this coding (powershell) this...

$source = "c:\temp\a.txt" $destination = "c:\temp\list.txt" $hits = select-string -path $source -simplematch "type fruit" -casesensitive $filecontents = get-content $source foreach($hit in $hits) {     $filecontents[$hit.linenumber-3]| out-file -append $destination     "" |out-file -append $destination } 

this extract 2nd top line per below

 name apple name orang name chery 

and below coding (.bat) remove word "name"

@echo off setlocal enabledelayedexpansion  del list2.txt /f "tokens=*" %%a in (c:\temp\list.txt) (   set line=%%a   set chars=!line:~-13,13!   echo !chars! >> list2.txt ) 

as 2nd phase need search word in list.txt file (apple,orang,chery) in b.txt, below.

 item p_date 10/03/15 pt_time 11:29:40:00 title nice coffe drink name ticky stock yes end item p_date 10/03/15 pt_time 11:29:40:00 title sweet tasty apple name apple stock yes end item p_date 10/03/15 pt_time 11:29:40:00 title nice tasty drink name juice stock yes end item p_date 10/03/15 pt_time 11:29:40:00 title niice nice orange name orang stock yes end item p_date 10/03/15 pt_time 11:29:40:00 title nutritious rich fruit name chery stock yes end 

i must search words list.txt in b.txt , extract 3 top line , write accordingly in new file named done.txt. below coding (powershell).

$source = "c:\temp\b.txt" $destination = "c:\temp\done.txt" $patterns = get-content c:\temp\list2.txt | where-object{$_} $results = select-string c:\temp\done.txt -pattern $patterns -simplematch $results.line | foreach-object{"$_`r`n"} | set-content c:\temp\done.txt foreach($hit in $hits) {     $filecontents[$hit.linenumber-4]| out-file -append $destination     $filecontents[$hit.linenumber-3]| out-file -append $destination     $filecontents[$hit.linenumber-2]| out-file -append $destination     $filecontents[$hit.linenumber-1]| out-file -append $destination     "" |out-file -append $destination } 

i managed develop coding this. need 3 script file (2 powershell , 1 batch) complete operation.

kindly assist me complete task in 1 single script. best if it's in .vbs or .bat.

here go. had bit of creative work make fit together. instance, instead of 2 scripts, first search b.txt fruit names match have in a.txt, , build array of custom objects, make easier search, seen here:

    #mark word item start of each record $bhits = select-string -path $bsource -simplematch "item" -casesensitive -context 5  #make empty array hold our powershell objects $fruitmatches = @() $output = @()  #make $fruitmatches contain our purchase records foreach ($bhit in $bhits){     $fruitmatches += [pscustomobject]@{fruit=$bhit.context.postcontext[3].replace("name ",'');         date=$bhit.context.postcontext[0].replace('p_date ','');         time=$bhit.context.postcontext[1].replace('pt_time ','')} } 

why bother doing that? well, first, wanted remove word name select-string output. turns out, can pretty using -context tell powershell scrape amount of lines along match, , selecting particular line want easy indexing object this:

#in 1 line, find word type fruit, , remove word 'name' ($hit.context.precontext[0].replace('name','').trim())  >cherry 

know when see long string of $hit.context... resolve name of fruit. well, reason bothered making custom objects, can search through matching objects this:

foreach ($hit in $hits){      $fruitmatches | ? fruit -eq ($hit.context.precontext[0].replace('name','').trim())      } 

which give following output:

fruit                                  date                                   time                                  -----                                  ----                                   ----                                  apple                                  10/03/15                               11:29:40:00                           orang                                  10/03/15                               11:29:40:00                           chery                                  10/03/15                               11:29:40:00  

from point, create few empty arrays hold results, , end whole thing dumping output using redirect character >.

this answers whole premise 1 script. please let me know if have questions how works.

completed answer

$source = "t:\a.txt" $bsource = "t:\b.txt" $destination = "t:\done.txt"   #mark word item start of each record $bhits = select-string -path $bsource -simplematch "item" -casesensitive -context 5  #make empty array hold our powershell objects $fruitmatches = @() $output = @()  #make $fruitmatches contain our purchase records foreach ($bhit in $bhits){     $fruitmatches += [pscustomobject]@{fruit=$bhit.context.postcontext[3].replace("name ",'');         date=$bhit.context.postcontext[0].replace('p_date ','');         time=$bhit.context.postcontext[1].replace('pt_time ','')} }  #resolve our hits, looking in file a.txt line type fruit $hits = select-string -path $source -simplematch "type fruit" -casesensitive -context 2 foreach ($hit in $hits){      $output += $fruitmatches | ? fruit -eq ($hit.context.precontext[0].replace('name','').trim())      }   $output > $destination write-output "checking $destination matches" get-content $destination 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -