LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-19-2017, 07:46 AM   #1
Asoo
Member
 
Registered: Apr 2017
Posts: 33

Rep: Reputation: Disabled
How to replace "-\n>" to "->" in linux using sed


Hi,

I would like to do the following:

Code:
123456-
>123456
to

Code:
123456->123456
I would like to replace "\n" between "-" and ">" to be replaced by none.

As I have other "\n" that should be unchanged.

Thanks!
 
Old 04-19-2017, 08:03 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,829

Rep: Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006
And your attempts have been what ?. If you've made no effort, why should we do it for you ?.
sed has the commands to do this - well documented.
 
Old 04-19-2017, 08:23 AM   #3
Asoo
Member
 
Registered: Apr 2017
Posts: 33

Original Poster
Rep: Reputation: Disabled
I have done few attempts. But I didn't wrote it here because none of it worked.

I wrote this (which are obviously not correct):

head abc.txt | sed 's/-\n\>/-\>/g'

head abc.txt | sed 's/-\n>/->/g'

head abc.txt | sed 's/-\\n\>/-\>/g'

I am confused because when I wanted to enter "\n" between line, it is done quite easily by replacing ">" to ">\n" but reverse of it doesn't apply. That's why I am stuck.
 
Old 04-19-2017, 08:34 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,893
Blog Entries: 3

Rep: Reputation: 3583Reputation: 3583Reputation: 3583Reputation: 3583Reputation: 3583Reputation: 3583Reputation: 3583Reputation: 3583Reputation: 3583Reputation: 3583Reputation: 3583
It's tricky to get at the new line. Based on the small sample you gave, you could do it by checking for a dash at the end of each line and then, if one is found, appending the next line of input before doing the substitution.

Code:
sed -e '/-$/{ N; s/\n//; }' abc.txt
The /-$/ looks for a dash at the end of a line, then if one is found the N and s/// commands are executed.

Code:
man sed
 
2 members found this post helpful.
Old 04-19-2017, 08:46 AM   #5
Asoo
Member
 
Registered: Apr 2017
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thank you so much for the reply!

Yes, it worked. Thanks for the link of manual. (I was also reading it)
 
Old 04-19-2017, 08:27 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,829

Rep: Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006Reputation: 4006
Quote:
Originally Posted by Asoo View Post
I am confused because when I wanted to enter "\n" between line, it is done quite easily by replacing ">" to ">\n" but reverse of it doesn't apply. That's why I am stuck.
sed is stream oriented, so it gives you a record at a time - up to, but not including, the newline. Hence your problems. The solution provided is a simple way to overcome this if the data structure is known in advance.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
KDevelop: Making "Find"/"Replace" bars not be per-file? Abscissa256 Linux - Software 0 09-11-2015 10:44 PM
unable to replace "ö" to "p" in shell scripting with "sed" meninmech Programming 5 06-22-2012 02:58 PM
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:02 PM.

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