Solved

Powershell to edit multiple strings in text file

Posted on 2013-01-28
10
1,045 Views
Last Modified: 2013-04-17
Hi,

I am looking for a powershell script to edit multiple lines in a text file where value of the string is with in quotes "value"

Save changes with value_filename

Is this something that can be done to change multiple strings/lines values in txt file using powershell

Thanks
0
Comment
Question by:chandru_sol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:coraxal
ID: 38829999
Can you provide an example of what you're trying to do?
0
 
LVL 12

Author Comment

by:chandru_sol
ID: 38830021
for example i have a file with below entry

deviceManagement[1]["securePort"] = "443"
deviceManagement[2]["ifName"] = "bdg2"
radiusAttributes[1]["nasIdentifier"] = "100"

to
deviceManagement[1]["securePort"] = "80"
deviceManagement[2]["ifName"] = "dgp2"
radiusAttributes[1]["nasIdentifier"] = "25"

There will be 50 + lines for which changes needs to be done either input or from text file

hope it is clear
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38830115
create txt file with all changes need to be applies, for example:

securePort 80
nasIdentifier 25

the powershell script will look for all entries ["securePort"] and change value to "80"
same thing goes to nasIdentifier.

is that would be ok?
0
What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 250 total points
ID: 38830170
so here's the script.
c:\temp\values.txt - contains the values need to be changed in the following format:
securePort 80
nasIdentifier 25

name of the property (without quotes) + space + new value to applied

c:\temp\source.txt - is the source txt file with the lines to be edited.
c:\temp\output.txt - is the output file (you can overwrite the source file if you wish).

$map = @{}
gc c:\temp\values.txt | %{$map.Add($_.split(' ')[0], $_.split(' ')[1])}

$output=''
gc c:\temp\source.txt | %{
	$line = $_
	$map.Keys | %{
		if($line.contains($_)){
			$value = 
			$line = ($line.split('=')[0] + "=`"$value`"") 
		}
	}
	
	$output+=$line + [Environment]::NewLine
}

$output | Out-File c:\temp\output.txt

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 38860845
There is something missing in line 9, $value is never set, and I cannot see how you should do that.
Another, similar way:
$map = import-csv C:\Temp\Values.txt -delimiter '  ' -header key, value

get-content c:\temp\source.txt | foreach-object {
   $line = $_
   foreach ($cur in $map)
   {  $line = $line -replace "(.+\[""$($cur.key)""\] =).*", "`$1 $($cur.value)"   }
  $line
} | out-File C:\Temp\Output.txt

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38860961
Line 9 should be:
$value = $map[$_]
0
 
LVL 12

Author Comment

by:chandru_sol
ID: 38864539
Thanks Guys!!

File extension is .cfg. is it ok to open .cfg in powershell and save it to .cfg without any changes in foramt

KC
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 250 total points
ID: 38864721
Not without changes. We do not know the original format (ASCII, UTF8, UTF16) of the file, and out-file will use UTF16 (Unicode) by default. And using the direct pipe solution I showed will not work.

Assuming you have "standard" ASCII files:
$map = import-csv C:\Temp\Values.txt -delimiter '  ' -header key, value

$text = get-content c:\temp\File.Cfg
foreach ($line in $text)
   foreach ($cur in $map)
   {  $line = $line -replace "(.+\[""$($cur.key)""\] =).*", "`$1 $($cur.value)"   }
  $line
} | out-File -Encoding ASCII C:\Temp\File.Cfg

Open in new window

0
 
LVL 12

Author Comment

by:chandru_sol
ID: 38866358
Thanks! How to find format of existing file?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38866941
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question