![]() |
|
|
|
|
|||||||
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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. |
|
|||
|
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 |
|
||||
|
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. |
|
|||
|
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 |
| 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 |