Solved

edit file in script

Posted on 2011-09-05
6
247 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:lolaferrari
[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
  • 5
6 Comments
 
LVL 21

Expert Comment

by:Papertrip
ID: 36485629
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

0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36485680
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.
0
 
LVL 21

Accepted Solution

by:
Papertrip earned 500 total points
ID: 36500266
@lolaferrari

Does that solution work for you?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:Papertrip
ID: 36520753
@lola, were you able to try this out?
0
 

Author Closing Comment

by:lolaferrari
ID: 36546477
many thanks - you don't happen to have a ksh or bash solution do you?
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36546679
Yes, execute that line of perl within your ksh/bash script ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

752 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