LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2010, 02:00 AM   #1
mwanakenya
LQ Newbie
 
Registered: Apr 2010
Location: ~
Distribution: RHEL, Fedora
Posts: 4

Rep: Reputation: 0
Perl replace text in file


Hi All,

I have a text file in this format

aaa1 bbb1 ccc1 63 ddd1 eee1 fff1 ggg1 12 hhh1 7347
aaa2 bbb2 ccc2 8334 ddd2 eee2 fff2 ggg2 722 hhh2 5623
aaa3 bbb3 ccc3 8925 ddd3 eee3 fff3 ggg3 6232 hhh3 653
aaa4 bbb4 ccc4 13 ddd4 eee4 fff4 ggg4 2442 hhh4 333
aaa5 bbb5 ccc5 4523 ddd5 eee5 fff5 ggg5 2332 hhh5 7823
aaa6 bbb6 ccc6 3434 ddd6 eee6 fff6 ggg6 6522 hhh6 23
aaa7 bbb7 ccc7 5634 ddd7 eee7 fff7 ggg7 422 hhh7 6447

In this I'd like to search for hhh1 and if I get a match delete hhh1 in addition to 7347, search for hhh6, delete hhh6 in addition to 23 alongside it and so on... How do I go about it in perl?
 
Old 04-16-2010, 02:06 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Use sed in case you want to do inplace replacement.

And if perl is the only option.

open a file1 in read mode.
open a file2 in write mode.
Read file1 line by line
Replace if the line contain the matching text.
write the line to file2.
move file1 to file1.bak
move file2 to file1

Last edited by PMP; 04-16-2010 at 02:12 AM. Reason: Removed a line
 
Old 04-16-2010, 02:09 AM   #3
mwanakenya
LQ Newbie
 
Registered: Apr 2010
Location: ~
Distribution: RHEL, Fedora
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks, how would I accomplish it using sed?
 
Old 04-16-2010, 02:12 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Have a look at this thread I hope it helps.
http://www.linuxquestions.org/questi...n-file-476382/

For sed please tell your distro
 
Old 04-16-2010, 02:29 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
seems like what you don't want is always the last 2nd field onwards, so do this with awk

Code:
 awk '$(NF-1)=="hhh1"{$NF=$(NF-1)=""}1' file
 
Old 04-16-2010, 02:48 AM   #6
mwanakenya
LQ Newbie
 
Registered: Apr 2010
Location: ~
Distribution: RHEL, Fedora
Posts: 4

Original Poster
Rep: Reputation: 0
In this particular case I have other fields after the "last 2nd field" that I need to retain
eg

aaa1 bbb1 ccc1 63 ddd1 eee1 fff1 ggg1 12 hhh1 7347 jjj1 455 kkk1 74764 ....
aaa2 bbb2 ccc2 8334 ddd2 eee2 fff2 ggg2 722 hhh2 5623 jjj2 66 kkk2 76423 ....

the challenge is once I have matched "hhh1" to expand and remove the numerical field after it
 
Old 04-16-2010, 03:01 AM   #7
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
sed 's#hhh1[ 0-9]\+##g' text.txt
For inplace replacement
Code:
sed -i 's#hhh1[ 0-9]\+##g' text.txt
Make sure you backup your original files before going for an inplace replacement
 
Old 04-16-2010, 03:02 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Can we assume the hhhX field will always be in the same spot?
 
Old 04-16-2010, 03:03 AM   #9
mwanakenya
LQ Newbie
 
Registered: Apr 2010
Location: ~
Distribution: RHEL, Fedora
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks! Worked like a charm!
 
Old 04-16-2010, 03:08 AM   #10
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Well If you have no more queries on it. Please mark this thread as solved.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl replace text in file ShaqDiesel Programming 27 08-11-2010 10:50 PM
How do I Find and Replace text within files using Perl (no one-liners please) GuerillaSquad Programming 3 12-31-2009 09:01 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
How do I replace text with perl with a list fo files? nadavvin Programming 7 09-14-2006 07:12 PM
Perl: Search and replace directories within text files Erhnam Programming 2 03-07-2006 04:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:45 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