Powershell Header Record

I have a scenario like below: I have a .dat file where header field name which is coming as below example:

2_a 2_b 2_c 2_d 2_e - Header
a b c d e - Data
f g h I j - Trailer

Next time

1_a 1_b 1_c 1_d 1_e -Header
c d e f g -data
b d f j k - trailer

So I want to achieve like my header record number is dynamically changing. Is there any way that I can achieve it so that I will just put the value and it will pick that value before that...like if I will input value 3 the header record will become 3_a 3_b like that.... After that my data will come and then trailer...Please suggest me the process as I am new to powershell...

1 answer

  • answered 2018-03-13 20:54 kim

    If you want to create a line like

    2_a 2_b 2_c 2_d 2_e


    1_a 1_b 1_c 1_d 1_e

    dynamically, you could use the string format operator, -f, like this

    $index = 2
    $header = "{0}_a {0}_b {0}_c {0}_d {0}_e" -f $index

    this will create the first header and save it to a variable. Change the $index variable to create another string with some other number instead.

    See this link for more info on its usage.