LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 03-18-2007, 02:59 PM   #1
smiler
Member
 
Registered: Sep 2002
Distribution: Mandrake 10.0 O
Posts: 48

Rep: Reputation: 15
How can I search and replace linefeeds interactively?


I have textfiles with linebreaks.
I want to throw some of them away with search and replace function in kwrite or Kate or another editor.

I don't find any way to do this and nothing on google

Can anyone help?
 
Old 03-19-2007, 01:04 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
"Or another editor", huh?

Ok, how about vi?

If by "linebreak" you mean the line feed that's at the end of every line, just go to the line whose ending line feed you want to eliminate, and use the J command (upper case jay) to combine that line with the next line.

On the other hand, if by "linebreak" you mean a line that contains no characters, you can search for such a line by searching for

Code:
^$
If you want to delete that blank line, make sure you're still on that line, and use the same J command. Then use the n command (lower case en) to go to the next empty line.

Rinse. Repeat.

If you're new to vi, google for

Code:
vi tutorial
You'll get a wealth of information.

Hope this helps.

Last edited by wjevans_7d1@yahoo.co; 03-19-2007 at 01:06 AM.
 
Old 03-26-2007, 03:26 AM   #3
smiler
Member
 
Registered: Sep 2002
Distribution: Mandrake 10.0 O
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by wjevans_7d1@yahoo.co
"Or another editor", huh?

Ok, how about vi?

If by "linebreak" you mean the line feed that's at the end of every line, just go to the line whose ending line feed you want to eliminate, and use the J command (upper case jay) to combine that line with the next line.

...

Hope this helps.
Now you'r teasing - almost 30 years ago I was officially introduced to vi - and I proved to be virtually immune to this.

The idea was to to be able to quickly remove a lot of linefeed characters from a long piece of text.
Most often you would want to remove all but the double ones denoting paragraph breaks. This is a task that is often needed when for instance you receive som text on email and want to include it some other place where the length of the lines don't fit.

It is much much faster to use a search and replace function than to manually navigate around and delete.

I found out that you can do this in editpad, which is developed for windows, but there is a linux version. There you can copy a linefeed, paste it into the search box and then rush through the text replacing and skipping as needed.
 
Old 03-26-2007, 04:33 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed - maybe start your education here.
 
Old 03-26-2007, 08:43 AM   #5
smiler
Member
 
Registered: Sep 2002
Distribution: Mandrake 10.0 O
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by syg00
sed - maybe start your education here.
Thanks - a really good tutorial - starting from the beginning - the thing with newlines comes at the very end of part 3, which I don't see any links to, but changing the numbers in the web address did the trick. Also very useful - though I don't see that it is possible to use it interactively
 
Old 03-26-2007, 09:44 AM   #6
henrysukumar
Member
 
Registered: Feb 2007
Location: Bangalore
Distribution: Fedora
Posts: 33

Rep: Reputation: 15
will this helps you,

vim filename

:g/\n/s//replaceword/gc

This will ask interactively y/n for replacing it.
 
Old 03-27-2007, 12:45 AM   #7
Zention
Member
 
Registered: Mar 2007
Posts: 119

Rep: Reputation: 16
Well I would advise vim.

Here is a oneliner to not exactly do what you want, it removes empty lines if they are 2 or above but leaves one.

<ESC>%s/\n{3,}/\r\r/e

I also dislike trailing spaces and once I turned on trailing space highlighting I noticed it all over the place.

<ESC>%s/\s*$//ge

to turn on trailing space detection:

hi TrailingEOL ctermbg=DarkGrey

nb. if you are using DarkGrey as your background this won't work

I like to call both the remove more than one line spacing and remove trailing spaces with the click of one button.

So I use a function in a vim resource file.

Code:
function! RemoveTrailing()
    %s/\s*$//ge
    %s/\n{3,}/\r\r/e
    nohl
endfun

map <F5> : call RemoveTrailing()<CR>
and map it to F5

Last edited by Zention; 03-27-2007 at 12:50 AM.
 
Old 03-29-2007, 08:55 AM   #8
smiler
Member
 
Registered: Sep 2002
Distribution: Mandrake 10.0 O
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by henrysukumar
will this helps you,

vim filename

:g/\n/s//replaceword/gc

This will ask interactively y/n for replacing it.
You guys are really persistent - VI is tracking me down;-) (in the reinkarnation of VIM)

I have tried to teach myself. Here is a funny step by step guide that I found useful:
http://www.vi-improved.org/tutorial.php

My vim seems debile though. Entering the suggested command does start an interactive replace function, with this:

replace with replaceword (y/n/a/q/l/^E/^Y)

in the bottom. But it only accepts y for an answer (pretty clever, hu). Other answers makes nothing happen - it doesn't continue with other answers but y and a, which have the same effenct as y. As a bit of consolation it only replaces every second newline in the file (every time you run it).

It looked promising, but something is rotten here.
I also tried the GUI version, this also have the function in the GUI and it accepts \n as a newline search and it does highlight all the newlines in the file, but it cannot delete or replace them. This is version 7.0.30
 
Old 03-31-2007, 01:26 PM   #9
Petro P
LQ Newbie
 
Registered: Mar 2007
Location: Toronto, Canada
Distribution: Gentoo GNU/Linux
Posts: 15

Rep: Reputation: 0
That's because vi/vim is a really powerful editor - you can get a lot done and very quickly. A friend of mine uses nothing BUT vim to edit text and he can edit stuff way faster than I can in any other text editor (command line or not). It's not a difference in typing speed or anything like that, it's just that vim is that versatile and will allow you to get more done.

In fact, where I go to school you are not allowed to use anything other than vim or emacs as a text editor. There's a reason for that

Cheers.
 
Old 04-13-2007, 08:18 AM   #10
smiler
Member
 
Registered: Sep 2002
Distribution: Mandrake 10.0 O
Posts: 48

Original Poster
Rep: Reputation: 15
By the way - I found that Gedit does the newline interactive search and replace just fine.

Can you confirm that your VIM can do it too? So I can be assured that it is mine that is defunct
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Advanced Search and Replace patrokov Linux - Software 33 07-09-2006 10:44 PM
search/replace in many files allelopath Linux - General 1 08-02-2005 09:21 PM
Python search and replace Accordion Programming 1 02-22-2005 07:54 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Search and replace links xtrude Linux - Newbie 4 01-29-2003 11:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 10:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration