Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Sun Aug 20, 2017 4:47 am


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

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

Joined: Thu Aug 06, 2009 2:48 am
Posts: 105

Offline
 

Unix shell - using TR to replace new lines with spaces

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  
E-mail friendPrint 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. How to echo newline in Linux/Unix shell

mandrei99

0

16711

Tue Jun 25, 2013 5:58 am

mandrei99 View the latest post

There are no new unread posts for this topic. How to add line numbers in unix shell output

mandrei99

0

841

Fri Oct 31, 2014 5:31 am

mandrei99 View the latest post

There are no new unread posts for this topic. SED how to remove multiple white spaces from a string

debuser

0

6900

Thu Feb 25, 2010 7:14 am

debuser View the latest post

There are no new unread posts for this topic. How to replace dot or any character with AWK

mandrei99

0

7505

Tue Jan 28, 2014 6:35 am

mandrei99 View the latest post

There are no new unread posts for this topic. AWK: How to replace a newline with actual \n

admin

0

53

Thu Jul 20, 2017 11:02 am

admin View the latest post

There are no new unread posts for this topic. How to replace new line in perl script

mandrei99

1

1813

Wed Jun 26, 2013 8:18 am

admin View the latest post

There are no new unread posts for this topic. How to replace \w SED multiple matches on the same line without piping

mandrei99

0

1051

Tue Oct 08, 2013 6:29 am

mandrei99 View the latest post

There are no new unread posts for this topic. PERL: How to replace non-ascii characters/bytes in a file

admin

0

1126

Fri Apr 10, 2015 8:58 am

admin View the latest post

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

debuser

0

522

Mon Apr 26, 2010 11:06 am

debuser View the latest post

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

debuser

0

5049

Fri Apr 02, 2010 6:07 am

debuser View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can 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:  
cronNews 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 - 5 hours [ DST ]



phpBB SEO