- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: grep and replace text in a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2008 06:02 AM
тАО02-15-2008 06:02 AM
Any ideas on how to accomplish this ?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2008 06:13 AM
тАО02-15-2008 06:13 AM
Re: grep and replace text in a file
Give us and example and we will send some ideas.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2008 06:15 AM
тАО02-15-2008 06:15 AM
Re: grep and replace text in a file
awk '/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2008 06:28 AM
тАО02-15-2008 06:28 AM
Re: grep and replace text in a file
How about?
# perl -pe 's/(.*trigger.*)/something new/' file
Thus, any line in the file containing the string "trigger" is totally replaced with a line that reads "something new".
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2008 11:21 AM
тАО02-15-2008 11:21 AM
Re: grep and replace text in a file
I've tried these two commands, but the end result is a blank file, which isn't good...
LIN=`/bin/grep -n "PASS_MAX_DAYS" /etc/login.defs | /bin/grep -v "#" | /bin/awk -F : '{print $1}'`
/bin/awk '{if (NR==${LIN}) $0="PASS_MAX_DAYS 30";print}' /etc/login.defs > /etc/login.defs.NEW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2008 11:32 AM
тАО02-15-2008 11:32 AM
Re: grep and replace text in a file
12:# PASS_MAX_DAYS Maximum number of days a password may be used.
17:PASS_MAX_DAYS 99999
whish is why I was trying to isolate the 2nd line for the actual line number that needs to be changed....
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2008 11:54 AM
тАО02-15-2008 11:54 AM
SolutionYou could find the non-commented line matching the parameter you want and change the value that follows it like this:
# perl -pe 'next if /^\s*#/;s/(PASS_MAX_DAYS\s+)(\d+)\b(.*)/${1}1234${3}/' file
This would find lines (any or all) in your file that constain the string:
PASS_MAX_DAYS
...followed by whitespace (\s) and one or more digits (\d+) like:
PASS_MAX_DAYS 10
...or:
PASS_MAX_DAYS 10 #...change to your taste
...and in this example change the '10' to '1234'
If you want to perform this update in-place and retain a backup copy (*.old) of your file, do:
# perl -pi.old -e 'next if /^\s*#/;s/(PASS_MAX_DAYS\s+)(\d+)\b(.*)/${1}1234${3}/' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2008 05:03 PM
тАО02-15-2008 05:03 PM