LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2010, 04:41 AM   #1
webhope
Member
 
Registered: Apr 2010
Posts: 184

Rep: Reputation: 30
how to escape breakline?


How to escape breaklines to \n?
${content[$n]} contains chars like (), spaces and /.

To me, this doesn't work:
Code:
content_block=$(echo ${content[$n]} | awk 'BEGIN { FS="\n"; RS="";}  {b = gensub(/(\n)/, "\\\\n", "g"); print b }')
It looks like awk is not good solution for this.

Last edited by webhope; 05-13-2010 at 05:25 AM.
 
Old 05-13-2010, 05:19 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
May we know the value of ${content[$n]} and what an "escape breakline" is? When you ask "How to remove escape breaklines to \n?" do you mean "How to change escape breaklines to \n?"
 
Old 05-13-2010, 05:24 AM   #3
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by catkin View Post
May we know the value of ${content[$n]} and what an "escape breakline" is? When you ask "How to remove escape breaklines to \n?" do you mean "How to change escape breaklines to \n?"
Sorry I wanted to write to escape breaklines.

text
Code:
title Sata Mandriva kernel
(hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2)  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img
 
Old 05-13-2010, 05:36 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by webhope View Post
Sorry I wanted to write to escape breaklines.

text
Code:
title Sata Mandriva kernel
(hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2)  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img
OK but what does "escape breaklines" mean? What is a breakline?

I took your data and command, hoping what you want would become clear but it didn't help. How does the output below differ from what you want?
Code:
c@CW8:~$ content[1]='title Sata Mandriva kernel
> (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2)  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
> initrd (hd0,2)/boot/initrd.img'
c@CW8:~$ echo ${content[1]} | awk 'BEGIN { FS="\n"; RS="";}  {b = gensub(/(\n)/, "\\\\n", "g"); print b }'
title Sata Mandriva kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2) resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788 initrd (hd0,2)/boot/initrd.img
 
Old 05-13-2010, 05:57 AM   #5
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Well breakline is the invisible \n . (edit - proper: new line!)
End escape the invisible breakline (or break line?) means to transform invisible \n to visible: \n:

Target string should look like this:
'title Sata Mandriva\nkernel (hd0,2) ...\n...'

My command cannot work. Cannot change the character because there is not breakline \n . It was removed because of the file separator.

Last edited by webhope; 05-13-2010 at 06:07 AM.
 
Old 05-13-2010, 06:24 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about:
Code:
echo ${content[$n]} | awk '{printf "%s\\n",$0}'
 
Old 05-13-2010, 06:34 AM   #7
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
How about:
Code:
echo ${content[$n]} | awk '{printf "%s\\n",$0}'
I think this doesn't do what I wanted

Code:
# content[1]='title Sata Mandriva kernel
> (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2)  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
> initrd (hd0,2)/boot/initrd.img'
#
# echo ${content[1]} | awk '{printf "%s\\n",$0}'
title Sata Mandriva kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2) resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788 initrd (hd0,2)/boot/initrd.img\n
Problem is that the \n is only on the end of string; but I need it on every end of line.
 
Old 05-13-2010, 06:47 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Where are your quotes to preserve the white space??

Code:
#!/bin/bash

content[1]='title Sata Mandriva kernel
(hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2)  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img'

echo "${content[1]}" | awk '{printf "%s\\n",$0}'
Output:
Code:
title Sata Mandriva kernel\n(hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2)  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788\ninitrd (hd0,2)/boot/initrd.img\n
 
1 members found this post helpful.
Old 05-13-2010, 07:01 AM   #9
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Grail, you have truth. The quotes are the magic! Thanx!
 
Old 05-13-2010, 07:34 AM   #10
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Is it possible to implement your solution to this code?

I have this command to escape some characters:
Code:
original_block=$(echo ${original_content[$n]} | awk 'BEGIN { FS="\n"; RS="";}  {b = gensub(/([\]*+/])/, "\\\\&", "g"); print b }')
Now to add the replace command of breaklines (to \n)

Last edited by webhope; 05-13-2010 at 07:36 AM.
 
Old 05-13-2010, 07:52 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yes if you get rid of the BEGIN{}
 
Old 05-13-2010, 09:11 AM   #12
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
How to do it?

I would try #3 but I need to escape the characters. In #3 they are not escaped.

Code:
#1 - 1st I need to escape some characters but it removes the \n
original_block=$(echo ${original_content[$n]} | awk 'BEGIN { FS="\n"; RS="";}  {b = gensub(/([\]*+/])/, "\\\\&", "g"); print b }')

#2 - not working
original_block=$(echo "$original_block" | awk '{printf "%s\\n",$0}');

#3 - working
original_block=$(echo "${original_content[$n]}" | awk '{printf "%s\\n",$0}');

Last edited by webhope; 05-13-2010 at 09:32 AM.
 
Old 05-13-2010, 09:16 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
#!/bin/bash

content[1]='title Sata Mandriva kernel
(hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2)  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img'

echo "${content[1]}" | awk '{b = gensub(/([\]*+/])/, "\\\\&", "g"); printf "%s\\n",b }'
 
1 members found this post helpful.
Old 05-13-2010, 09:27 AM   #14
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by webhope View Post
How to do it?

I would try #2 but I need to escape the characters. In #2 they are not escaped.
I think I said it wrong. #2 was not working because of escaped characters and removed \n . #3 was working but having not escaped characters
 
Old 05-13-2010, 09:32 AM   #15
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
Code:
echo "${content[1]}" | awk '{b = gensub(/([\]*+/])/, "\\\\&", "g"); printf "%s\\n",b }'
Thank you this is workable.
 
  


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
how do i escape a / ??? Fascistchicken Linux - General 10 09-12-2004 11:28 AM
Escape character ? juanb Linux - Newbie 2 08-31-2004 10:03 AM
Escape! Wind0wR3fuge Linux - Newbie 6 07-11-2004 03:00 AM
escape sequence help in C name_in_use450 Linux - General 6 07-01-2004 09:23 AM
The Great Escape Treefrog Linux - Software 6 11-11-2003 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:36 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