Solved

Powershell to edit multiple strings in text file

Posted on 2013-01-28
10
897 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
  • 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
 
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 68

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article will help you understand what HashTables are and how to use them in PowerShell.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now