LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-13-2013, 10:34 AM   #1
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Rep: Reputation: Disabled
Replace Data in a file using Perl or any other utility


I have a file with text that I need to find and replace. The text is not always the same but it does have a consistent method for searching. For instance the text would be machine: NNN where NNN would change. I want to replace machine: NNN with machine: myname.
How do i perform a search and replace for inconsistent data and replace it with consistent data.

With perl I would use perl -p -i -e 's/machine:./machine:myname /g' ALLJOBS.08.txt

The problem with this is it doesn't replace the original machine names just appends the new name to the original name.
 
Old 08-13-2013, 10:38 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
sed:
Code:
sed s/"machine: ..."/"machine: myname"/g mfarch99.txt

Last edited by schneidz; 08-13-2013 at 10:40 AM.
 
Old 08-13-2013, 10:42 AM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Code:
sed -E 's/machine\:\ [0-9]+/machine\:\ myname/g' ALLJOBS.08.txt
Should do the trick:


Code:
cat test
machine: 2323123
machine: 1232929292

Code:
sed -E 's/machine\:\ [0-9]+/machine\:\ myname/g' test
machine: myname
machine: myname
 
Old 08-13-2013, 11:34 AM   #4
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thanks. I will try both and and let you know which will work the best.
 
Old 08-13-2013, 01:58 PM   #5
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Command to replace machine name

sed -i -e '/machine:/ s/machine: .*/machine: newname/g' ALLJOBS.08.txt
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace data of one column with data on other file corresponding to transaction ID saurabhmehan Linux - Newbie 1 12-23-2010 01:19 AM
Perl replace text in file ShaqDiesel Programming 27 08-11-2010 10:50 PM
[SOLVED] Perl replace text in file mwanakenya Programming 9 04-16-2010 03:08 AM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
Perl: replace character in file vippie Programming 4 03-23-2007 04:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration