Compare two text files in Powershell and if a name is found in both files output content from file 2 to a 3rd text file

Is it possible using PowerShell to compare the contents of two text files line by line and if a line is found output that line to a third text file?
Lets say hypothetically someone asks us to search a text file named names1.txt and when a name is found in names1.txt we then pair that with the same name in the second text file called names2.txt
lets say the names shown below are in names1.txt
Bob
Mike
George
Lets say the names and contents shown below are in names2.txt
Lisa
Jordan
Mike 1112222
Bob 8675309
Don
Joe
Lets say we want names3.txt to contain the data shown below
Mike 1112222
Bob 8675309
In vbscript I used search and replace commands to get part of the way there like this
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("testing.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Mike ", "Mike 1112222")
Set objFile = objFSO.OpenTextFile("testing.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
That script works great when you know the name you are looking for and the correct values. Lets say someone gives you a list of 1000 employees and says import these names into a list in the correct format and one sheet has the correct names only and
the other sheet has lots of extra names say 200000 and you only need the 1000 you are looking for in the format from names2.txt.

Sure,
Here's a simple one:
$names1 = "C:\names1.txt"
$names2 = "C:\names2.txt"
$names3 = "C:\names3.txt"
Get-Content $names1 | ForEach-Object {
$names1_Line = $_
Get-Content $names2 | Where-Object {$_.Contains($names1_Line)} | Out-File -FilePath $names3 -Append
This basically just reads $names1 file, line by line, and then read $names2 file line by line as well.
If the line being evaluated from $names2 file contains the line being evaluated from $names1 file, then the line from $names2 file gets output to $names3 file, appending to what's already there.
This might need a few more tinkering to get it to perform faster etc depending on your requirements. For example:
- If either $names1 or $names2 contain a lot of entries (in the region of hundreds) then it will be faster to load the whole content of $names2 into memory rather than opening the file, reading line by line, closing and then doing the same for every single
line in $names1 (which is how it is currently works)
- Make sure that your comparison is behaving as expected. The .Contains method always does a case sensitive comparison, this might not be what you are after.
- You might want to put a condition to ignore blank lines or lines with spaces, else they'll also be brought over to $names3
Hopefully this will get you started though and ask if you have further questions.
Fausto

Similar Messages

  • Get these two error messages when I run the iTunes exe file:  Error 2 and Error 2 (Windows error 2).  Both say Apple Application Support is required.  Uninstalled and tried to install again several times.  Using Windows 7 64 bit on laptop.

    Trying to install iTunes on my Dell laptop with Windows 7 - 64 bit.  Get these two error messages when I run the iTunes exe file:  Error 2 and Error 2 (Windows error 2).  Both say Apple Application Support was not found.  Can anyone tell me why this is happening?

    See Troubleshooting issues with iTunes for Windows updates.
    tt2

  • Comparing Two tables with 300k records and update one table

    Could you let me know how to compare two tables having 300k records and update one table.below is the scenario.
    Table Tabl_1 has columns A,B and Tabl_2 has columns B,new_column.
    Column B has same data in both the tables.
    I need to update Tabl_2 in new_column with Tabl_1 A column data by comparing B column in both tables.
    I m trying to do using PLSQL Tables.
    Any suggestion?
    Thanks.

    Hi,
    Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved, so that the people who want to help you can re-create the problem and test their ideas.
    Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
    If you're asking about a DML statement, such as UPDATE, the CREATE TABLE and INSERT statements should re-create the tables as they are before the DML, and the results  will be the contents of the changed table(s) when everything is finished.
    Always say which version of Oracle you're using (for example, 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002
    ef2019c7-080c-4475-9cf4-2cf1b1057a41 wrote:
    Could you let me know how to compare two tables having 300k records and update one table.below is the scenario.
    Table Tabl_1 has columns A,B and Tabl_2 has columns B,new_column.
    Column B has same data in both the tables.
    I need to update Tabl_2 in new_column with Tabl_1 A column data by comparing B column in both tables.
    I m trying to do using PLSQL Tables.
    Any suggestion?
    Thanks.
    Why are you trying to use PL/SQL tables?  If tabl_1 and tabl_2 are regular database tables, it will be much simpler and faster just to use them.
    Depending on your requirements, you can do an UPDATE or MERGE, either in SQL or in PL/SQL.

  • I get a message saying some of the itunes files cannot be copied to my ipod b/c i can only have content from 5 itune accounts. what can i do to put the content i bought?

    i get a message saying some of the itunes files cannot be copied to my ipod b/c i can only have content from 5 itune accounts. what can i do to put the content i bought?

    Obviously that's your problem.  As the message states, five is the maximum supported.

  • Compare two cells in a row and group of rows

    Tuesdays 1st 2nd 3rd 4th 5th MB
    Jan 4, 2011 7 14 21 28 36 32
    Jan 11, 2011 5 15 25 28 38 16
    Jan 18, 2011 2 17 25 28 38 25
    Okay, say the above is my table, how do I compare two cells in a row for two numbers? Say I wanted to find out if row A2 contained a 7 and a 28?
    I thought an IF formula might do it, but can't figure it out.
    Thanks in advance!
    Jim

    Head Crab wrote:
    Tuesdays 1st 2nd 3rd 4th 5th MB
    Jan 4, 2011 7 14 21 28 36 32
    Jan 11, 2011 5 15 25 28 38 16
    Jan 18, 2011 2 17 25 28 38 25
    Okay, say the above is my table, how do I compare two cells in a row for two numbers? Say I wanted to find out if row A2 contained a 7 and a 28?
    "A2" is an address for a single cell, not a row. Its current content, assuming no empty rows above or empty columns left of what's shown, is "Jan 4, 2011". Perhaps you mean Row 2, or the range B2:G2.
    Assuming that you want to know 'if' (or 'how many times') two specific numbers occur in the range of cells from column B to column G in a single row, COUNTIF is the function you want.
    Place the two target numbers into cell I1 and J1.
    Enter the formula below into I2:
    =COUNTIF($B2:$G2,I$1)
    Fill the formula right into J2, then fill both down to row 4.
    You'll get a count of the occurrences of each number.
    If you want only a "Yes" (both numbers appear in the range) or "No" (neither of the numbers appear, or one appears but not the other), use this variation (in I2 or J2):
    =IF(AND(COUNTIF($B2:$G2,I$1)>0,COUNTIF($B2:$G2,J$1)>0),"Yes","No")
    Regards,
    Barry

  • How to get text of itab header and structure field name dynamically?

    HI.
    I'm just trying display itab to web.
    of cause I can use <htmlb:tableView> but I need display by loop itable  for my special reason.
    the table is dynamic structure that means header line should be changes depends on structure description.
    '<htmlb:tableView>' certainly can display header line dynamically with just itable only.
    but don't know how to get these text and structure field name without 'tableView'.
    help me please.
    Thank you.
    <TABLE border=1>
    <TR style="background-color:#FFFF99" >
         <TD>header 1</TD>
         <TD>header 2</TD>
         <TD>header 3</TD>
    </TR>
    <%
    DATA: WA_STR TYPE FOPT_BSP_UI_REPORT.
    LOOP AT TT_itab INTO WA_STRUC.
    %>
    <TR>
         <TD nowrap><%=WA_STRUC-ORGUNIT_T%></TD>
         <TD nowrap><%=WA_STRUC-PRGROUP_T%></TD>
         <TD nowrap><%=WA_STRUC-PROCESS_T%></TD>
    </TR>
    <%ENDLOOP.%>
    </TABLE>

    DDIC structure means struture made from SE11?
    if yes,that's what I'm trying to display table.
    and my sepcial reason is this:
    my final purpose is download dynamic table to excel file.
    but when I download table comes form "<htmlb:tableView>" with changing http response header that shows some link error and useless icon and no table grid and etc...
    "<htmlb:tableView>" resulting lot of useless tags and script.
    think iterator has no function removing extra tags
    so  I'am trying to make simple html table.
    but problem is that itable is not just one structure can be more than 100 kinds of structure.
    in this situation is there any way to display dynamic itab into simple html table?

  • I have two Canon Printers, an MP970 and a MX710, I want to use both to scan, but the Canon Solution

    I have two Canon printers an MP970 and an MX710, when I installed the MX710 it disabled my ability to access the MP970 SCANNER from the Canon Solution Menu.
    I have tried to re-install the drivers and the MP970 Solutions but even that only allows the scanner in the MX710?
    IS there any way to use the scanners for both? AND what do I need to do.

    Hi bburtjr,
    The MP970 and MX710 use two seperate versions of the Solution Menu, so I think what is happening is that it copied over your desktop shortcut with the latest version which nly shows the 710.  If both versions of the Solution Menu are still on the computer, they should be accessible under your Start Menu if you are using Windows or your Applications Menu if you are on a Mac.  Under the Start Menu you would go to All Programs and Canon Utilities and it should list two seperate versions of Solution Menu.  The newwer version being for the 710 and the older being for the 970.  On Mac, if you click Go from Finder, and click Applications, you should have the same Canon Utilities folder and the same applies as it does in Windows.  You should have two versions of Solution Menu, one for each printer.
    Did this answer your question? Please click the Accept as Solution button so that others may find the answer as well.

  • When browsing documente library contents from client application, cannot see all files

    We are currently running SharePoint 2010 Enterprise in a small environment. On one of my sites, I have a document library with various documents uploaded (docx, xlsx, pdf). I am unable to see all the documents when browsing the library from Word/Excel/Adobe
    Acrobat. For example:
    - At the root level of the library, I have 5 folders
    - In Folder 1, I have 10 PDF files
    - If I go into Adobe Acrobat 11, for example, and paste the address of my site in, I can see the library and I can click on it and see the 5 folders.  If I click on Folder 1, there are no PDFs listed.  Even changing File Type to "All Files"
    still returns 0 results
    Things I have tested:
    If I view the library in the browser, I can see the 10 PDFs in Folder 1.  They are not checked out and they are visible to everyone
    If I click Library>Open w/ Explorer, I can see all 10 PDFs in Folder 1
    If I try using other client applications (Word, Excel, etc..), I cannot see any files in Folder 1, even when I change the File Type to "All Files"
    If I click in some of the other Folders in the same library from within a client application, sometimes I can see the files but most times I cannot
    What could be causing this?  I do not see any errors in any of the event logs.

    Hi Sephiroth,
    I think my observation is when you browse from word/Excel/Adobe it is taking your local system credentials and so  not showing files when you access.
    local system account means the account which you used to login to the system.
    For example,you logged into the system as contosomo\xyz and you are trying to connect to the site using Contosomo\setup account,what my point is though you logged in to the site using the setup account site is taking your xyz account credentials and so it
    not showing any files as xyz obviously will not have access.
    To confirm this, you can try by giving atleast read access to the local system account (in this case xyz) to the library and then test again whether you are able to see the files or not.
    Please remember to mark your question as answered &Vote helpful,if this solves/helps your problem.
    s p kumar

  • Comparing two PDF documents | Acrobat Tips and Tricks | Adobe TV

    This video will get you started with using the improved Compare Documents command in Acrobat 9 to identify what has changed between two versions of a PDF file.
    http://adobe.ly/xqvrc7

    Hi there. Thanks for the video. But is there a feature to move among the found differences? Or to show ONLY the differences? In a large document, it's not enough to just "look for" the color-coded differences.
    I have not found any such features on viewing a compared doc. I've also looked in the help and now am looking to see if other resources discussing PDF compare might show it. Thanks for any thoughts.

  • How to compare two rows in PL/SQL and retrieve the values?

    Hello,
    I have two tables which have identical schemas, one table (tbl_store) is used to hold the latest version, and the other table (tbl_store_audit) holds previous versions. When the latest record is updated, it is inserted into the tbl_store_audit table as a revision, and the updated details are used as the latest record.
    For example: The latest version is held in tbl_store, however the tbl_store_audit may hold 5 records which are the past records used before changes were made - these are seen as revisions.
    I want to be able to compare what has changed between each revision in the tbl_store_audit table. For example: Out of the 10 columns, the change between revision 1 and revision 2 was the size from XL to XXL. The change between revision 3 and revision 4 was the size XS to M and price 4.99 to 10.99, and so on.
    Eventually i will create an APEX report that will show the user the revision number and what was changed from and to.
    I seen in a previous post i need to note my oracle version: Oracle version 10.2.0.4.0

    Hi,
    Like suggested already you should give some sample data and output.
    Maybe you would like to have something like this:
    -- Sample data
    -- Note PK is the primairy key of the source table and rev are the revisions
    with tbl_store_audit as
    select 1 pk, 1 rev , 1 price  , 'XXL' unit_size from dual union all
    select 1 pk, 2 rev , 1 price,   'XL'  unit_size from dual union all
    select 1 pk, 3 rev , 1.4 price, 'XXL' unit_size from dual union all
    select 2 pk, 1 rev , 1.4 price, 'XL'  unit_size from dual union all
    select 2 pk, 2 rev , 1.4 price, 'XL'  unit_size from dual union all
    select 2 pk, 3 rev , 1.4 price, 'XL'  unit_size from dual union all
    select 1 pk, 4 rev , 1 price  , 'XL'  unit_size from dual union all
    select 1 pk, 5 rev , 1 price  , 'XL'  unit_size from dual union all
    select 3 pk, 1 rev , 1.2 price, 'XL'  unit_size from dual union all
    select 3 pk, 2 rev , 1.2 price, 'XXL' unit_size from dual union all
    select 4 pk, 1 rev , 1 price  , 'XL'  unit_size from dual
    -- end of sample data
    ,tbl_store_audit_tmp as
    select
      pk
      ,rev
      ,'PRICE'          field_name
      ,to_char(price)   field_value
      ,to_char(lag(price,1) over (partition by pk order by rev) ) old_field_value
    from
      tbl_store_audit
    union all
    select
      pk
      ,rev
      ,'UNIT_SIZE'           field_name
      ,to_char(UNIT_SIZE)    field_value
      ,to_char(lag(UNIT_SIZE,1) over (partition by pk order by rev) ) old_field_value
    from
      tbl_store_audit
    -- include all other fields from the table here with it's own union all select ...
    select
    from
      tbl_store_audit_tmp
    where
      field_value != old_field_value
    PK REV FIELD_NAME FIELD_VALUE                              OLD_FIELD_VALUE                       
    1   3 PRICE      1.4                                      1                                       
    1   4 PRICE      1                                        1.4                                     
    1   2 UNIT_SIZE  XL                                       XXL                                     
    1   3 UNIT_SIZE  XXL                                      XL                                      
    1   4 UNIT_SIZE  XL                                       XXL                                     
    3   2 UNIT_SIZE  XXL                                      XL                                      
    6 rows selected If you realy want to keep track of all the changes I think it would be better if you make a "after update trigger" on the base table that checks what changed and put that directly in the uadit table.
    Regards,
    Peter
    Edited by: Peter vd Zwan on Aug 16, 2012 8:25 AM

  • I just bought a new samsung 22" monitor and after talking to them found out it needs higher resolution then displayed in my settings,text resolution is bad. do I take back or can this be fixed?

    I just bought a new Samsung 22" monitor. The text resolution is really bad. After talking to Samsung, I found that the needed resolution is higher then what my Mini-Mac shows available. Can this be fixed or do I return the monitor?

    Let's try again...
    my cable goes from the "Thunderball" port on my mini with an adaptor to convert it to the regular cable
    You mean the Thunderbolt port.
    What Mac mini do you have? Apple menu > About This Mac.
    What Samsung monitor do you have?
    What cables are you using?
    Have you tried holding Option and click the Scaled resolutions button in System prefs > Displays?

  • Where the alarm file is saved and by which name

    dear all
     I m setting alarm by using following steps:-
    Tools -> DSC Module -> Configure Tags
    On Clicking Alarms tab setting alarms.
    then press ok.
    Then File -> Save to save changes.
    i just want to know where these changes are saved and where the alarmfile is created .
    is its extension  .ALR.
    thanks in advance answer me as early as possible for u.
    regards,
    Dhanwanti

    Assuming you are using a version prior to 8.0, the alarm configuration is part of the tag configuration and is stored in a file with a .scf extension.  This extension will not show up on windows as Microsoft co-opted it.  In 8.0 and later, the alarm configuration is part of the variable configuration and is stored in a file with a .lvlib extension.
    Message Edited by REM1 on 03-27-2006 02:09 PM
    Regards,
    Robert

  • I have two itunes accounts one old and a new one. I want to merge the songs from the old account to the new one. How is that accomplished?

    I have have two itunes accounts. I want to merge the two accounts. How is that accomplished?

    You cannot merge accounts.
    Sorry

  • Excel output issue from a semi colon separated sql file script

    Hi,
    I am executing a simple SQL script with back to back Select queries resulting in ; separated text output.
    I save this output as a text file and open in excel specifying delimited with ;.
    For example -
    SQL script is as follows-
    SET lines 5000
    SELECT 'Error Code'||';'||'Error Message'
    FROM dual
    SELECT lookup_code||';'||description
    FROM fnd_lookup_values
    WHERE lookup_type = 'NLUS_AR_INT_ERROR_MSG'
    ORDER BY TO_NUMBER(LTRIM(lookup_code,'E'))
    The output retrieved has the following 2 issues -
    1. A blank row is inserted after the "Error code Error message" row. I need to eliminate this blank row.
    2. The 2nd issue I am facing is an empty row is inserted after every 62 rows in the excel.
    Please let me know how I can eliminate these blank rows that are appearing after every 62 rows and in between each query's results.
    Thanks,
    Aditi

    For the first issue you can try the "prompt" command.
    For the second issue you can add a "set pagesize 0" command in your sqlplus-file.
    You can use "set trimspool on" to elimate blanks at the end of each line in your outputfile (if you want).
    set termout off
    set feedback off
    set heading off
    set echo off
    set linesize 5000
    set trimspool on
    set pagesize 0
    spool c:\temp\abc.txt
    Prompt Error Code;Error Message
    SELECT lookup_code||';'||description
    FROM fnd_lookup_values
    WHERE lookup_type = 'NLUS_AR_INT_ERROR_MSG'
    ORDER BY TO_NUMBER(LTRIM(lookup_code,'E'));
    spool offEdited by: hm on 31.03.2011 04:41

  • I have two ipads under the same name/account. How do I airplay content from one to the other if there is not a distinguishing difference between the two or use face chat?

    I gave my 2yr old my 16gb ipad 2 when I upgraded to a 64gb ipad 3. They are on the same account so that I can share certain apps between them, but because they consider themselves one in the same I can't use airplay or face chat with my daughters ipad. How do I do these things?

    Airplay doesn't work between two iPads so you don't need to worry about that, however, you can change the name of an iPad in Settings > General > About > Name.

Maybe you are looking for

  • ActionTec router reboots on it's own?

    I originally posted this on Aug 4, but it seems that a whole bunch of posts from that day were lost in the maintenance that night so here goes again!  My MI424WR Rev I router several times a day just shuts down and reboots.  When it's finished reboot

  • ITunes 11.1 issues in Windows 8 Pro 64bit

    Hey there, just recognized some issues with iTunes 11.1 (which was needed to upgrade to iOS 7 on my 5). First of all the drag and drop option for dropping songs into iTunes and iTunes playlists from folders is missing. I had this a while ago with an

  • Posting Back

    I am new to java and in the past I have used asp.net with c#. I have previously used the postback method, anyways I have a page where when you first look at it you can see all these radio button options for all states, then there is a scroll box with

  • Syncing Problem... Help please!

    I have more music on my computer than can fit on my iPod, so of course I made a playlist to fit all of the songs, the problem now is that I've added songs to the playlist, removed others, and it's under the limit of the iPods capacity, yet it still w

  • How to customize standard BCT InfoCubes?

    Hi gurus,              I am new to BI.I got one scenario like adding 2 new user defined InfoObjects into the cube along with standard business content infoobjects which i delivered with BCT InfoCube (eg:0PAPA_C01).Is it possible to use the same infoc