edit file in script

lolaferrari
lolaferrari used Ask the Experts™
on
Hello,

i have a config file that i want to change one line in. I would like to comment out the line, cpoy it and make one change to it. If that's too awkward I might just edit the line

Here is a sample of what the file is
...
....
OUT   FILE    VALUE=/usr/sbin/test_me
...
OUT  FILE2 VALUE=/usr/sbin/test_me2
,,,

I want to replace the line with FILE2 to become

OUT FILE2 VALUE=/usr/sbin/test_newvalue

I have to do this in many files so I need to be able to script ot
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
pod@box:~$ cat testfile
OUT     FILE    VALUE=/usr/sbin/test_me
OUT     FILE2   VALUE=/usr/sbin/test_me2
pod@box:~$ perl -i -pe 'if ( $_ =~ /FILE2/ ) { s/test_me2/test_newvalue/ }' testfile
pod@box:~$ cat testfile
OUT     FILE    VALUE=/usr/sbin/test_me
OUT     FILE2   VALUE=/usr/sbin/test_newvalue

Open in new window


If you know the paths to the files, put them in a file and use a for loop.

pod@box:~$ for i in `cat list`; do perl -i -pe 'if ( $_ =~ /FILE2/ ) { s/test_me2/test_newvalue/ }' $i; done

Open in new window

Top Expert 2011

Commented:
I would like to comment out the line, cpoy it and make one change to it. If that's too awkward I might just edit the line

Apologies I overlooked the first part and just went straight to the edit.
Top Expert 2011
Commented:
@lolaferrari

Does that solution work for you?
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Top Expert 2011

Commented:
@lola, were you able to try this out?

Author

Commented:
many thanks - you don't happen to have a ksh or bash solution do you?
Top Expert 2011

Commented:
Yes, execute that line of perl within your ksh/bash script ;)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial