The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

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 06:24 AM
Too simple to search for spudtheimpaler UNIX for Dummies Questions & Answers 3 10-21-2006 09:55 AM
Search & replace videsh77 Shell Programming and Scripting 1 01-14-2005 03:10 AM
Simple Search and Replace - Revisited Brandt Shell Programming and Scripting 1 04-23-2004 07:45 AM
search and Replace mukeshannamalai UNIX for Advanced & Expert Users 4 09-14-2001 03:21 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-15-2004
Registered User
 

Join Date: Jan 2004
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Simple? Search replace

in vi, if i type ctrl-J, i can shift the next line up to the current line.
I need to do this whenever a specific string exists (in particular ^M) ,however i have not been able to find a way to do this in vi, sed, or awk

seems simple enough.

can someone help me?

thanks in advance
Forum Sponsor
  #2 (permalink)  
Old 01-15-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
You must mean shift-J, not control J.

In vi you can do this:
:g/^M/j

But to type the ^M, you need to type cntl-V followed by cntl-M.
  #3 (permalink)  
Old 01-15-2004
Registered User
 

Join Date: Jan 2004
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
thanks, but it does not join all of the ^Ms

240|^M\
^M\
^M\
Old Port Marketing order recd $62,664.- to ship 6/22/99^M\
^M\
^M\
^M\
^MCompleted by: C. Malczynski|


becomes:

240|^M\ ^M\
^M\ Old Port Marketing order recd $62,664.- to ship 6/22/99^M\
^M\ ^M\
^M\ ^MCompleted by: C. Malczynski|

but should be:

240|^M\ ^M\^M\ Old Port Marketing order recd $62,664.- to ship 6/22/99^M\^M\ ^M\^M\ ^MCompleted by: C. Malczynski|


this is what i typed :g/^M\\/j ----- (ctrl-V, ctrl-M)


any suggestions??

p.s. thanks, this has been stumping me for awhile

Last edited by Brandt; 01-15-2004 at 12:50 PM.
  #4 (permalink)  
Old 01-15-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
:g/^M\\$/j
and run the command a 2nd or third time until no more trailing ^M\ remain.
  #5 (permalink)  
Old 01-15-2004
Registered User
 

Join Date: Jan 2004
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
thanks, this has been really helpful, but i need to run this on several files that have >1000000 records, which i can break down into smaller files if need be (migrating data in case you haven't guessed), hence the reason i was trying to find a one time fell swoop, the solution you provided is much better than me scrolling for ^M and hitting Shft-J five or six times, but also doesn't lend itself to being run unattended, do you know of a way to peform the same task with sed?

if not, the help you have given me has been great, and i appreciate your quick responses.

thanks again
  #6 (permalink)  
Old 01-15-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
This is a sed script. It does not use a shell. You must get all 11 lines exactly as I have them.
Code:
#! /usr/bin/sed -nf
#
#
s/^M\\$/^M\\/
H
t
x
s/\n//g
p
s/.*//
h
Call it combiner or something. Run it like:
combiner < oldfile > newfile
  #7 (permalink)  
Old 01-16-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,208
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
The paste command can be used for serial merging, e.g...
Code:
paste -s -d'' file1 > file2
  #8 (permalink)  
Old 01-16-2004
Registered User
 

Join Date: Jan 2004
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
sweet! looks like voodoo to me, if you have the patience to explain what that script did, i would love to learn more about it.

thank you very, much!!!!
  #9 (permalink)  
Old 01-16-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I'll try, but it's not real easy to explain...

s/^M\\$/^M\\/ changes cntl-m backslash to cntl-m backslash. The $ makes this happen only if the pattern is at end of line. This results in no change, but it set a success flag that can be tested later. That is how we know if we want to join the next line.

H appends the pattern area to the hold area. The pattern area is where lines arrive as they are read.

t test to see if that previous s commnand worked or not. If if did, we jump to the end of the script. Well not the last line. This means that we are finished with the current line. So we will read a new line and restart the script.

x exchange. What I really want to do is to retrieve the hold area. Since we are here, the test failed. So we have a line that did not end with cntl-m backslash.

s/\n//g That hold area that I just retrieved is a collection of lines that I want to join. So I delete the newlines.

p print the line. Since there was a -n on the sed command line, no lines are printed automatically.

s/.*// Remove all characters. This empties the pattern area. Thre is a d command, but it has side effects. I usually go with this as my delete.

h copies pattern area to hold area. Now it's empty too.
  #10 (permalink)  
Old 01-16-2004
Registered User
 

Join Date: Jan 2004
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
i think i get the gist, i was able to use this script to clean up a few other anomalies as well.

thank you very much, i was to the point of asking the users if they really needed those records... which, invariably, they answer yes... you have saved me much headache.

thanks again.
Google UNIX.COM
Closed Thread

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script rsync ftp scp recursive segmentation fault(coredump) sftp script snoop unix solaris change ip address stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


All times are GMT -7. The time now is 03:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101