Replace \r in a string with actual returns?

  • Follow


Hi, in a text file there is a \r wherever a return should be. The
actual returns aren't put in the file because it would complicate the
parsing. However, when I'm done parsing, I put the text in a variable
"result" and I would like to convert all the \r in result to actual
returns. How can I do this? I'm using gsub but I don't know what the
replacement string should be. Thanks for any help with this.

0
Reply dan.j.weber (10) 10/29/2005 7:28:03 PM

On 2005-10-29, dan.j.weber@gmail.com wrote:
> Hi, in a text file there is a \r wherever a return should be. The
> actual returns aren't put in the file because it would complicate the
> parsing. However, when I'm done parsing, I put the text in a variable
> "result" and I would like to convert all the \r in result to actual
> returns. How can I do this? I'm using gsub but I don't know what the
> replacement string should be.

"\n"

> Thanks for any help with this.

-- 
  Chris F.A. Johnson                   | Author:
  <http://cfaj.freeshell.org>          |      Shell Scripting Recipes:
  Any code in this post is released    |  A Problem-Solution Approach,
  under the GNU General Public Licence |                 2005, Apress
0
Reply Chris 10/29/2005 7:40:56 PM


dan.j.weber@gmail.com wrote:
> Hi, in a text file there is a \r wherever a return should be. The
> actual returns aren't put in the file because it would complicate the
> parsing. However, when I'm done parsing, I put the text in a variable
> "result" and I would like to convert all the \r in result to actual
> returns. How can I do this? I'm using gsub but I don't know what the
> replacement string should be. Thanks for any help with this.
> 

It's probably "\n", but maybe all you really need is this:

awk -v RS="\r" '($1=$1)1' file

Post some sampel in/out and what you've tried so far for more suggestions.

	Ed.
0
Reply Ed 10/29/2005 7:43:40 PM

dan.j.weber@gmail.com wrote:
> Hi, in a text file there is a \r wherever a return should be. The
> actual returns aren't put in the file because it would complicate the
> parsing. However, when I'm done parsing, I put the text in a variable
> "result" and I would like to convert all the \r in result to actual
> returns. How can I do this? I'm using gsub but I don't know what the
> replacement string should be. Thanks for any help with this.

awk '{gsub(/\\r/, "\r")}8'

0
Reply William 10/29/2005 8:21:31 PM

In article <1130617291.483507.210810@g43g2000cwa.googlegroups.com>,
William James <w_a_x_man@yahoo.com> wrote:
....
>awk '{gsub(/\\r/, "\r")}8'

The {} is unnecessary.  2 stroke penalty.

0
Reply gazelle 10/29/2005 8:24:42 PM

Okay, this worked. Thanks for all your responses. I have one more
question about search/replace and hopefully I won't have to start a new
thread:

I want to replace all ' (apostrophes) with empty strings. However, my
awk code must be typed on the command line. Since awk uses ' to enclose
the code, this doesn't work:

awk -v greeting="what's up" 'BEGIN {gsub("'", "", title); print
greeting}'

I tried escaping the ' with \ but it still halts the code. Any
suggestions?

0
Reply dan 10/30/2005 2:22:54 AM

dan.j.weber@gmail.com wrote:
> Okay, this worked. Thanks for all your responses. I have one more
> question about search/replace and hopefully I won't have to start a new
> thread:
> 
> I want to replace all ' (apostrophes) with empty strings. However, my
> awk code must be typed on the command line. Since awk uses ' to enclose
> the code, this doesn't work:
> 
> awk -v greeting="what's up" 'BEGIN {gsub("'", "", title); print
> greeting}'
> 
> I tried escaping the ' with \ but it still halts the code. Any
> suggestions?
> 

$ awk -v greeting="what's up" -v sq=\' 'BEGIN {gsub(sq, "", greeting); 
print greeting}'
whats up

Regards,

	Ed.
0
Reply Ed 10/30/2005 2:42:56 AM

dan.j.weber@gmail.com writes:
>I want to replace all ' (apostrophes) with empty strings. However, my
>awk code must be typed on the command line. Since awk uses ' to enclose
>the code, this doesn't work:
>
>awk -v greeting="what's up" 'BEGIN {gsub("'", "", title); print
>greeting}'
>
>I tried escaping the ' with \ but it still halts the code. Any
>suggestions?

gsub("\047","",title) where \047 represents the octal code for apostrophe.

Also try:
gawk -v greeting="what's up" 'BEGIN {gsub("'\''", "",greeting); print
 greeting}'
-- 
John Savage                   (my news address is not valid for email)
 
0
Reply John 11/3/2005 11:31:22 PM

7 Replies
142 Views

(page loaded in 0.108 seconds)

Similiar Articles:













7/16/2012 3:23:22 PM


Reply: