none
Append info into a text file RRS feed

  • Question

  • Hello! need some help with PS and adding text to a file that is already created. 

    Text document has no header and is just numbers, but I'd like to add different text to each line. 

    the text file looks like such: 

    1
    2
    105
    2

    I'd like for it to look like this: 

    1 Add
    2 Delete
    105 Delete
    2 Add

    so far I've used:

    $text=(get-content C:\temp\testcount.txt)
    $text[0]=+ "add"
    $text | Out-file C:\Temp\testcount.txt -append

    But this will only add the text "add" to the first line....

    Friday, October 10, 2014 6:47 PM

Answers

  • Example:


    $numbers = @(
      "1"
      "2"
      "105"
      "2"
    )
    
    $labels = @(
      "Add"
      "Delete"
      "Delete"
      "Add"
    )
    
    0..$numbers.Count | foreach-object {
      "{0} {1}" -f $numbers[$_],$labels[$_]
    }
    
    # Output:
    # 1 Add
    # 2 Delete
    # 105 Delete
    # 2 Add
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by NateTur Friday, October 10, 2014 11:38 PM
    Friday, October 10, 2014 8:50 PM
    Moderator

All replies

  • What's your criteria for which strings get appended to which numbers?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, October 10, 2014 7:03 PM
    Moderator
  • Here is one example:


    $x = get-content "x.txt"
    $y = get-content "y.txt"
    0..$x.Count | foreach-object {
      "{0} {1}" -f $x[$_],$y[$_]
    }
    



    -- Bill Stewart [Bill_Stewart]

    Friday, October 10, 2014 7:04 PM
    Moderator
  • Sorry, what I should have mentioned is that the text file will always have 5 lines, or rows. And the text that needs to go beside the numbers will not change either, but it should always be: 

    example:

    1 Add

    2 Delete

    3 Add

    213 Delete

    I have another script that runs that outputs the counts of a csv file into the text file, I was just looking to add the text next to the numbers once it's done generating. 

    • Edited by NateTur Friday, October 10, 2014 8:01 PM clarification
    Friday, October 10, 2014 7:58 PM
  • There are only 4 rows in the example (not 5), and the text doesn't match the example in the original post......

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, October 10, 2014 8:09 PM
    Moderator
  • Example:


    $numbers = @(
      "1"
      "2"
      "105"
      "2"
    )
    
    $labels = @(
      "Add"
      "Delete"
      "Delete"
      "Add"
    )
    
    0..$numbers.Count | foreach-object {
      "{0} {1}" -f $numbers[$_],$labels[$_]
    }
    
    # Output:
    # 1 Add
    # 2 Delete
    # 105 Delete
    # 2 Add
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by NateTur Friday, October 10, 2014 11:38 PM
    Friday, October 10, 2014 8:50 PM
    Moderator
  • Thanks Bill!

    This is exactly what I needed. 

    Again, sorry for the confusion, I was just using that as an example of what I needed. 

    Friday, October 10, 2014 11:39 PM