The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
Search and Replace in Ksh DeepakXavier Shell Programming and Scripting 9 05-28-2007 08:11 AM
sed search and replace d__browne UNIX for Dummies Questions & Answers 7 04-26-2006 09:46 AM
how to search for a str and replace it.. sekar sundaram Shell Programming and Scripting 6 12-29-2005 06:11 PM
search and Replace mukeshannamalai UNIX for Advanced & Expert Users 4 09-14-2001 06:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-24-2003
bridgeje bridgeje is offline
Registered User
  
 

Join Date: Oct 2003
Location: Santa Cruz
Posts: 2
Angry Search and replace sed or tr

Hi folks,
I need to search and replace specific text in a file and replace it. I have a text file that does not have any newlines or carriage returns. All newlines have been removed. Here is what I need to do.

Find the exact string “DH” (quotes included) and replace it with \n”DH” (basically putting a newline in front of all occurances of “DH”)

I have tried using sed
sed 's/\<"DH"\>/\"DH"/g' DHt.txt > DH2.txt
When I tried the sed example the output has nothing in it. I think this is because there are no newlines or carriage returns.

And tr command…
tr -s '["DH"]' '[\\012"DH"]' < DHu.txt > DHt.txt
This replaces anything with DH in it and the newline is not added, it adds \012. It needs to to find only “DH”

Now I’m pretty new this type Unix commands so my syntax could easliy be wrong .

Thank all in advance,
Joaquin
  #2 (permalink)  
Old 10-27-2003
linuxer_rh linuxer_rh is offline
Registered User
  
 

Join Date: Aug 2003
Location: china
Posts: 7
maybe it is hard to work only used sed or tr
but you cant do it both used sed and tr
for example :you can used sed to add a special char before "DH" then used tr to replace the special char by \n
  #3 (permalink)  
Old 10-27-2003
JoBa JoBa is offline
Registered User
  
 

Join Date: Sep 2002
Location: The Netherlands
Posts: 16
Hello Bridgeje,

One possibility is to open the file in vi and do the following:

:%s/"DH"/^M"DH"/g

for ^M you should type ctrl v, crtl enter
  #4 (permalink)  
Old 10-27-2003
bridgeje bridgeje is offline
Registered User
  
 

Join Date: Oct 2003
Location: Santa Cruz
Posts: 2
I tired the insert character and earch and replace it with \n, but that speacial character could be in the file and I cannot replace it.

I also tried vi and then typing
:%s/"DH"/^M"DH"/g
But I must have done something wrong, when I type in the line it says "what" Is there a way to type it in the command line?

Thanlks for the help eveyone,
Joaquin
  #5 (permalink)  
Old 10-27-2003
linuxer_rh linuxer_rh is offline
Registered User
  
 

Join Date: Aug 2003
Location: china
Posts: 7
maybe you can try this (i had ran the code in bash shell )
cat yourfile|sed s'/\(\"DH\"\)/\
> \1/'g
  #6 (permalink)  
Old 10-28-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
The problem with these suggestions is that they are intended to be used on text files. The operative definition of text file is a collection of lines. The OP does not have a file that is a collection of lines nor would he have one if he succeeds in doing what he wants.

If I really had to solve this this problem, I would write a special purpose C program. I'm not a perl expert, but I strongly suspect that perl could also handle this problem.

Any perl programmers out there?
  #7 (permalink)  
Old 10-28-2003
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try using a sed script, containing...
Code:
s/DH/\
DH/g
Use it like this...
Code:
sed -f scriptfile file1 > file2

Last edited by Ygor; 10-28-2003 at 11:49 AM..
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 09:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0