Solved

edit file in script

Posted on 2011-09-05
6
242 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
  • 5
6 Comments
 
LVL 21

Expert Comment

by:Papertrip
Comment Utility
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
Comment Utility
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
Comment Utility
@lolaferrari

Does that solution work for you?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 21

Expert Comment

by:Papertrip
Comment Utility
@lola, were you able to try this out?
0
 

Author Closing Comment

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

Expert Comment

by:Papertrip
Comment Utility
Yes, execute that line of perl within your ksh/bash script ;)
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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

743 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

16 Experts available now in Live!

Get 1:1 Help Now