Jump to content
Sign in to follow this  
neddie

Replacing line breaks using Kate

Recommended Posts

Hi,

I've got a text file, and I'm trying to remove the line breaks in it. I'm playing with Kate and its find-and-replace tool with regular expressions. At the start, the file looks like this:

something
something else

whatever

and I want to transform it to the form

something,something else,whatever

So what I'm trying is something like find "<return><return>" replace with "<return>" and find "<return>" replace with ",". Kate's tool however only seems to work on a single line, and no matter whether I look for \n or \r or \n\r or \r\n it doesn't find the end of line. I can search for ^$ which finds the empty line, but I can't replace it with anything to remove it.

Any tips? Or an easy alternative method?

Edited by neddie

Share this post


Link to post
Share on other sites

Here's a command line way to do it.

 

cat aTextfile | tr "\n" "," |  sed 's/,,/,/g' | sed 's/,$//' >aNewTextfile

 

It first translates all linefeed chars to a comma (which creates one long single line), then converts any instances of two commas in a row to a single comma, and then strips the trailing comma off the last phrase in the line, then writes the output to a new file. Taking your original data, the result is this:

 

something,something else,whatever

 

This can probably be done more elegantly, but this will work (given your spec that there are no more than 2 linefeeds in a row).

Share this post


Link to post
Share on other sites

Hey, thanks for that. tr is a new command for me, looks very useful!

Only snag is that given I'm doing most of the rest of the editing with Kate, that means I have to save the file, run something from the command line, go back to Kate and reload the file? Don't you think it's odd that you can't do that in an editor, especially when it gives "Newline" and "Carriage Return" in the find options? I even tried OpenOffice Writer but no joy there either.

 

I had thought of trying sed but that also only works on single lines, as far as I know, so the missing link was tr - thanks!

Share this post


Link to post
Share on other sites

If you want to do it solely within kate, there is a way of doing it that I just found out about.

 

Open the text file in Kate

Ctrl-R (Edit -> Replace)

Text to Find: $

Check the "Regular Expression" box

Replacement text: ,

Click Replace button

 

This will replace all linefeed chars with a comma, but will leave the originally separate lines unjoined.

 

Then you can use the Tools -> Join command (Ctrl-J) to manually join the lines by pressing Ctrl-J repetitively until all lines are joined. [UPDATE: See EDIT paragraph below]. This, however, will leave a space after each comma so then you have to globally replace ", " with just a comma.

 

EDIT: I just discovered that if you select all the text in Kate, then issue the Ctrl-J, all the separate lines will be joined into one line.

Edited by jboy

Share this post


Link to post
Share on other sites

I just discovered that if you select all the text in Kate, then issue the Ctrl-J, all the separate lines will be joined into one line. So after doing the Replace to convert linefeed chars to commas, highlight all the text, and then press Ctrl-J. Now all you have to do is replace the ", " with "," and you're done.

Share this post


Link to post
Share on other sites

Woo! Ctrl-J does it! Weird thing, only replaces all of the line breaks with spaces, (no option to choose anything else, or nothing), not very flexible but does the job.

 

So it's 3 steps - firstly replace $ with , to give a separator at the end of every line. Secondly, select the lines and do a Tools-Join Lines (Ctrl-J) to join them all together into a single line. Thirdly, remove the spaces it put in and the multiple commas from the empty lines, by replacing ", " with "," and ",," with ",".

 

Or, simpler if you know there are no spaces in the entries, do a Ctrl-J and replace all spaces with commas.

 

Thanks, guys!

 

PS/ Tried the Kate command line but it didn't recognise "replace" and didn't recognise "help replace", so I guess something's not installed.

Share this post


Link to post
Share on other sites

OK, I read that, but the command line page doesn't mention plugins and the plugins page doesn't mention the command line - are you sure they're related? Certainly in my kate config there are no options to do with the command line.

 

By the way, just for info, the tr command has a "squeeze repeats" option, so jboy's script gets even simpler:

cat inputfile.txt | tr -s "\n" , > outputfile.txt

Share this post


Link to post
Share on other sites

Oooh, that squeeze-repeats option is nice! Great catch, neddie. Much more elegant. Will definitely have to remember that option for future reference.

Share this post


Link to post
Share on other sites
OK, I read that, but the command line page doesn't mention plugins and the plugins page doesn't mention the command line - are you sure they're related? Certainly in my kate config there are no options to do with the command line.
Kate's editor component has an internal command line, allowing you to perform various actions from a minimal GUI.

docs.kde.org/stable/en/kdebase/kate/advanced-editing-tools-commandline.html

 

Kate is using two different forms of plug-ins, namely plug-ins for the Kate application and plug-ins for the Kate editor component.

 

You can enable both types of plug-ins in the configuration dialog, which also provides access to additional configuration options for plug-ins that requires that.

docs.kde.org/stable/en/kdebase/kate/plugins.html

Share this post


Link to post
Share on other sites

Sorry to sound pedantic, but that just says that kate has a command line, and kate has plugins. It even says that the command line is internal, which means not a plugin. It definitely doesn't say that the command line is a plugin, and doesn't show any way to activate it. Activating plugins yes, but no mention of the command line whatsoever. So as I said, and as your quotes show, the command line page mentions the command line, and the plugins page mentions plugins.

 

You didn't say whether your command line works, can you execute the commands "replace a b" or "s/a/b/g" from your command line in kate?

 

PS/ jboy: :D

Share this post


Link to post
Share on other sites

Sorry, neddie. To paraphrase the above: Kate has an internal command line, which is part of the editor component. There is a plugin for the editor component, which needs activating.

 

I haven't tried any of this, as I have only just got home and my tea awaits. :)

Share this post


Link to post
Share on other sites

I think I see the confusion - the phrase "plugin for X" is a bit ambiguous - it could mean that it is a plugin which provides the feature X, or it could mean that it's a plugin which can plug in to X. I think you're thinking that the plugins for Kate's editor component _provide_ the editor component, whereas in fact they _plug_in_ to it.

 

I've got the editor component, it's what I'm doing my editing with. The editor component, which I've got, has an internal command line thingy. The plugins, which can plugin to this editor component, provide extra things like a "Word Completion plugin" or an "Autobookmarker" - handy extras which you can enable or not. So they are plugins _for_ the editor. Like extras for Firefox are plugins _for_ Firefox, but they don't provide Firefox, and plugins for Gimp plug in to Gimp.

 

In this case, the quotes you sent say that the editor component has an _internal_ command line tool, and there are plugins for the editor component.

Whew! Confusion over! :thumbs:

Share this post


Link to post
Share on other sites

I only suggested trying the plug-in because you said Kate didn't recognise the replace command. :)

 

I can use the replace command in the command line, but I can't get it to work with \n or \r.

 

I think I'll call it a day...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...