The UNIX and Linux Forums  

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
Removing a file name (-T-G1) bobo UNIX for Dummies Questions & Answers 3 03-05-2008 11:26 AM
Need Help Removing a File GTRocker8824 UNIX for Dummies Questions & Answers 1 02-15-2008 12:04 AM
Spurious line feeds ajcannon Shell Programming and Scripting 2 10-29-2007 07:24 AM
removing a line from a file and then placing into another file iago UNIX for Dummies Questions & Answers 1 09-09-2007 02:46 AM
removing file vbaskar UNIX for Advanced & Expert Users 5 02-12-2002 04:43 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-08-2004
old git old git is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 4
Question removing spurious charactors from file

Morning,

Hopefully someone can help an newbie!

I have been getting a number of file corruptions with
Rogue "Control M" charactors [ carriage return ].

The effect is:

Should be

aaaa
bbbb
cccc
dddd

(4 lines)

But getting

aaaa
bb<Ctl M>
bb
ccccc
dddd

(5 lines)

I am having to correct this manually using vi...


Anyone know a way to do this in a script?

Effectively I want to delete any occurrances of the string
"^M\n"

I can't use tr as

tr -d "^M" leaves two lines.
tr -d "^M\n" also deletes the genuine "\n" at end of line.


Any suggestions? sed?

The problem with manuals is that you need to know what to look up!
  #2 (permalink)  
Old 07-08-2004
dkaplowitz dkaplowitz is offline
Registered User
  
 

Join Date: Jun 2004
Location: ~Philadelphia
Posts: 63
in vi:

esc. to cmd mode, then enter

Code:
:%s/CTRL+V CTRL+M//g
Note I left a space betw. the 2 CTRL sequences for readability, you shouldn't have any spaces.
  #3 (permalink)  
Old 07-08-2004
old git old git is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 4
Thanks.

However, I am able to do this using vi.

I am asking how to do it in a script,
so it can be automated.
  #4 (permalink)  
Old 07-08-2004
dkaplowitz dkaplowitz is offline
Registered User
  
 

Join Date: Jun 2004
Location: ~Philadelphia
Posts: 63
Sorry I relied too much on your title and assumed it was a single file.

Try playing with this:
Code:
for fl in *.php; do
mv $fl $fl.old
sed 's/FINDSTRING/REPLACESTRING/g' $fl.old > $fl
#rm -f $fl.old
done
You could also do it something like:
Code:
for i in `find ./ -type f -exec grep -l "yourstringhere" {} \;`
        do 
        # use our sed rules to make changes. put them in a temp file
        sed -f ./sedlist $i > $i.tmp
        # this is optional. make a back up to be safe.
        #cp $i $i.sedbak
        # move the new temp file over the original
        mv $i.tmp $i
        done
without the "sedlist" file

Of course, change the values to suit your needs.
  #5 (permalink)  
Old 07-08-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Try...

awk '/\r/{sub("\r","");printf $0;next};1' file1 > file2
  #6 (permalink)  
Old 07-08-2004
old git old git is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 4
the problem is that the
corruption inserts a <Ctl M> charactor and throws a line.

Hence

instead of bbbb

we get :

bb<CtlM>
bb

By simply substituting <Ctl M> with ''
I get

bb
bb

when I want

bbbb


In vi I have to use "J" to concatinate the two lines after subsituting <Ctl M> for ''

Hence I think I need to substitute the string
^M\n with ''

but I can't get this to work
  #7 (permalink)  
Old 07-08-2004
old git old git is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 4
Eureka!


Thanks Ygor.


That's done the trick!

(albeit with nawk rather than awk!)
Closed Thread

Bookmarks

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 11:58 AM.


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