Ivorde Unix/Linux/Database/Web/Mail Forum

View unanswered posts
View active topics
It is currently Sat Feb 04, 2012 9:58 pm


Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

Bookmark this article:

Author Message
debuser
Post  Post subject: Unix shell - using TR to replace new lines with spaces  |  Posted: Thu Feb 25, 2010 3:08 pm

Joined: Thu Aug 06, 2009 9:48 am
Posts: 90

Offline
Save on Delicious
Unix shell - using TR to replace new lines with spaces

There are a few ways of replacing new lines with other characters. Below I'll show a few examples for replacing new lines in strings or standard input/output.

Replace new lines with spaces:
Code:
# cat test.file
     In the first synopsis form, the characters in string1 are translated into
     the characters in string2 where the first character in string1 is trans-
     lated into the first character in string2 and so on.  If string1 is
     longer than string2, the last character found in string2 is duplicated
     until string1 is exhausted.

     In the second synopsis form, the characters in string1 are deleted from
     the input.

     In the third synopsis form, the characters in string1 are compressed as
     described for the -s option.

     In the fourth synopsis form, the characters in string1 are deleted from
     the input, and the characters in string2 are compressed as described for
     the -s option.

     The following conventions can be used in string1 and string2 to specify
     sets of characters:

     character  Any character not described by one of the following conven-
                tions represents itself.
Code:

# tr '\n' ' \n' < test.file
     In the first synopsis form, the characters in string1 are translated into      the characters in string2 where the first character in string1 is trans-      lated into the first character in string2 and so on.  If string1 is      longer than string2, the last character found in string2 is duplicated      until string1 is exhausted.       In the second synopsis form, the characters in string1 are deleted from      the input.       In the third synopsis form, the characters in string1 are compressed as      described for the -s option.       In the fourth synopsis form, the characters in string1 are deleted from      the input, and the characters in string2 are compressed as described for      the -s option.       The following conventions can be used in string1 and string2 to specify      sets of characters:       character  Any character not described by one of the following conven-                 tions represents itself.


Below I will replace new lines with a comma (,):
Code:
# tr '\n' ',\n' < test.file
     In the first synopsis form, the characters in string1 are translated into,     the characters in string2 where the first character in string1 is trans-,     lated into the first character in string2 and so on.  If string1 is,     longer than string2, the last character found in string2 is duplicated,     until string1 is exhausted.,,     In the second synopsis form, the characters in string1 are deleted from,     the input.,,     In the third synopsis form, the characters in string1 are compressed as,     described for the -s option.,,     In the fourth synopsis form, the characters in string1 are deleted from,     the input, and the characters in string2 are compressed as described for,     the -s option.,,     The following conventions can be used in string1 and string2 to specify,     sets of characters:,,     character  Any character not described by one of the following conven-,                tions represents itself.,,


Top
Display posts from previous:  Sort by  
Print view

Topics related to - "Unix shell - using TR to replace new lines with spaces"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. SED how to remove multiple white spaces from a string

debuser

0

7948

Thu Feb 25, 2010 2:14 pm

debuser View the latest post

There are no new unread posts for this topic. Remove empty lines in vi/vim

debuser

0

344

Mon Apr 26, 2010 6:06 pm

debuser View the latest post

There are no new unread posts for this topic. Using sed to comment/uncomment lines in files

debuser

0

2517

Fri Apr 02, 2010 1:07 pm

debuser View the latest post

There are no new unread posts for this topic. Remove the character before the last character with SED - shell scripting

mandrei99

0

1462

Wed Nov 04, 2009 5:49 pm

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC + 2 hours [ DST ]

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
DAJ Glass 2 template created by Dustin Baccetti

phpBB SEO