Topic review - Unix shell - using TR to replace new lines with spaces
Author
Message
debuser
Post subject: Unix shell - using TR to replace new lines with spaces | Posted: Thu Feb 25, 2010 8:08 am
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.,,
[b]Unix shell - using TR to replace new lines with spaces[/b]
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.
[b]Replace new lines with spaces:[/b] [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][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. [/code]
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.,,[/code]