nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

To find and replace in text file

This is a discussion on To find and replace in text file within the Shell scripting forums, part of the Development/Scripting category; Hi, I need help on following problem. I have a text file something like below... I have to replace ":" ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 14-11-2007, 09:27 PM
Junior Member
User
 
Join Date: Nov 2007
OS: RedHat
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
nashtech is on a distinguished road
Default To find and replace in text file

Hi,

I need help on following problem.

I have a text file something like below...

I have to replace ":" "." "space" with , using either sed or awk can some one help me on this...

Thanks in Advance..

=====================================

21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776
29:2007.11.13 00:38:13:918 Env/TP-Processor6:INFO:CORE BEGIN Session=3152E297D96912192109B8F9BEBB1446.bstby1 Active Sessions: 777
88:2007.11.13 00:38:15:832 Env/TP-Processor2:INFO:CORE BEGIN Session=50D731F3EE59D3595A25140F166E9F93.bstby1 Active Sessions: 767
116:2007.11.13 00:38:16:793 Env/TP-Processor7:INFO:CORE BEGIN Session=B7977F67D06591A78AA2F290DBCE6F2C.bstby1 Active Sessions: 768
144:2007.11.13 00:38:19:219 Env/TP-Processor12:INFO:CORE BEGIN Session=19C3468F71BC6B2BEA03C3F2D4F74E44.bstby1 Active Sessions: 769
172:2007.11.13 00:38:21:930 Env/TP-Processor2:INFO:CORE BEGIN Session=13DA23B1E5787E4E07B3098A17CAFBAC.bstby1 Active Sessions: 770


=============================================
Reply With Quote
  #2 (permalink)  
Old 15-11-2007, 09:19 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 246 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Code:
echo '21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776' | sed -e "s/:/./g"
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 15-11-2007, 09:57 PM
Junior Member
User
 
Join Date: Nov 2007
OS: RedHat
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
nashtech is on a distinguished road
Default

Thanks a lot Vivek... It worked perfectly
Reply With Quote
  #4 (permalink)  
Old 31-07-2008, 06:46 PM
Junior Member
User
 
Join Date: Jul 2008
OS: Debian
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
pooppp is on a distinguished road
Default i got the below answer but if i m hving tht txt in file..where to mention my filename

i got the below answer but if i m hving tht txt in file..where is the place int the script to mention my filename sam.txt
sam.txt contains
21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776
29:2007.11.13 00:38:13:918 Env/TP-Processor6:INFO:CORE BEGIN Session=3152E297D96912192109B8F9BEBB1446.bstby1 Active Sessions: 777
88:2007.11.13 00:38:15:832 Env/TP-Processor2:INFO:CORE BEGIN Session=50D731F3EE59D3595A25140F166E9F93.bstby1 Active Sessions: 767
116:2007.11.13 00:38:16:793 Env/TP-Processor7:INFO:CORE BEGIN Session=B7977F67D06591A78AA2F290DBCE6F2C.bstby1 Active Sessions: 768
144:2007.11.13 00:38:19:219 Env/TP-Processor12:INFO:CORE BEGIN Session=19C3468F71BC6B2BEA03C3F2D4F74E44.bstby1 Active Sessions: 769
172:2007.11.13 00:38:21:930 Env/TP-Processor2:INFO:CORE BEGIN Session=13DA23B1E5787E4E07B3098A17CAFBAC.bstby1 Active Sessions: 770


Quote:
Originally Posted by nashtech View Post
Hi,

I need help on following problem.

I have a text file something like below...

I have to replace ":" "." "space" with , using either sed or awk can some one help me on this...

Thanks in Advance..

=====================================

21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776
29:2007.11.13 00:38:13:918 Env/TP-Processor6:INFO:CORE BEGIN Session=3152E297D96912192109B8F9BEBB1446.bstby1 Active Sessions: 777
88:2007.11.13 00:38:15:832 Env/TP-Processor2:INFO:CORE BEGIN Session=50D731F3EE59D3595A25140F166E9F93.bstby1 Active Sessions: 767
116:2007.11.13 00:38:16:793 Env/TP-Processor7:INFO:CORE BEGIN Session=B7977F67D06591A78AA2F290DBCE6F2C.bstby1 Active Sessions: 768
144:2007.11.13 00:38:19:219 Env/TP-Processor12:INFO:CORE BEGIN Session=19C3468F71BC6B2BEA03C3F2D4F74E44.bstby1 Active Sessions: 769
172:2007.11.13 00:38:21:930 Env/TP-Processor2:INFO:CORE BEGIN Session=13DA23B1E5787E4E07B3098A17CAFBAC.bstby1 Active Sessions: 770

=============================================
Reply With Quote
  #5 (permalink)  
Old 30-01-2010, 12:17 AM
Junior Member
User
 
Join Date: Jan 2010
OS: Debian
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
santoshkpt is on a distinguished road
Default From Santosh P

Open file in vi editor and do this :%s/:/.g

Quote:
Originally Posted by pooppp View Post
i got the below answer but if i m hving tht txt in file..where is the place int the script to mention my filename sam.txt
sam.txt contains
21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776
29:2007.11.13 00:38:13:918 Env/TP-Processor6:INFO:CORE BEGIN Session=3152E297D96912192109B8F9BEBB1446.bstby1 Active Sessions: 777
88:2007.11.13 00:38:15:832 Env/TP-Processor2:INFO:CORE BEGIN Session=50D731F3EE59D3595A25140F166E9F93.bstby1 Active Sessions: 767
116:2007.11.13 00:38:16:793 Env/TP-Processor7:INFO:CORE BEGIN Session=B7977F67D06591A78AA2F290DBCE6F2C.bstby1 Active Sessions: 768
144:2007.11.13 00:38:19:219 Env/TP-Processor12:INFO:CORE BEGIN Session=19C3468F71BC6B2BEA03C3F2D4F74E44.bstby1 Active Sessions: 769
172:2007.11.13 00:38:21:930 Env/TP-Processor2:INFO:CORE BEGIN Session=13DA23B1E5787E4E07B3098A17CAFBAC.bstby1 Active Sessions: 770
Reply With Quote
  #6 (permalink)  
Old 31-01-2010, 06:32 AM
Member
User
 
Join Date: Sep 2006
Posts: 77
Thanks: 0
Thanked 20 Times in 16 Posts
Rep Power: 6
ghostdog74 has a spectacular aura about ghostdog74 has a spectacular aura about ghostdog74 has a spectacular aura about
Default

you can just use the shell

Code:
while read -r line
do
    echo ${line//[:. ]/,}
done <"file" > new
mv new file
or you can use awk

Code:
awk '{gsub(/[:, ]/,","}1' file
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Cups is not printing text file with proper format, staircasing issue Ashish Pathak CentOS / RHEL / Fedora 1 18-11-2007 08:12 AM
can not find the username in /etc/passwd file subrata1in Getting started tutorials 5 27-03-2007 11:45 AM
can not find user name in /etc/passwd file subrata1in Linux software 2 26-03-2007 10:44 AM
rearranging columns in a text file sureshbup Shell scripting 2 06-12-2006 09:43 AM
Replacing text in a file using awk postyrus Shell scripting 4 02-05-2005 03:31 PM


All times are GMT +5.5. The time now is 05:11 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38