tr

How to concatenate multiple lines of output to one line?

自闭症网瘾萝莉.ら 提交于 2019-11-28 03:05:13
If I run the command cat file | grep pattern , I get many lines of output. How do you concatenate all lines into one line, effectively replacing each "\n" with "\" " (end with " followed by space)? cat file | grep pattern | xargs sed s/\n/ /g isn't working for me. Use tr '\n' ' ' to translate all newline characters to spaces: $ grep pattern file | tr '\n' ' ' Note: grep reads files, cat concatenates files. Don't cat file | grep ! Edit: tr can only handle single character translations. You could use awk to change the output record separator like: $ grep pattern file | awk '{print}' ORS='" '

is there a way to use tr/// (or equivalent) in java?

柔情痞子 提交于 2019-11-28 01:49:36
I would like to know if there is an equivalent to tr/// (as used in Perl) in Java. For example, if I wanted to replace all "s"s with "p"s in "mississippi" and vice versa, I could, in Perl, write #shebang and pragmas snipped... my $str = "mississippi"; $str =~ tr/sp/ps/; # $str = "mippippissi" print $str; The only way I can think of to do it in Java is to use a dummy character with the String.replace() method, i.e. String str = "mississippi"; str = str.replace('s', '#'); // # is just a dummy character to make sure // any original 's' doesn't get switched to a 'p' // and back to an 's' with the

SED/tr etc.. : How to comment out a line that contains “string” in a file?

别说谁变了你拦得住时间么 提交于 2019-11-27 22:46:29
问题 my file contains lines such as these: -A INPUT -m state --state NEW -m tcp -p tcp --dport 2000 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 2001 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 2002 -j ACCEPT i want to comment out ( add # infront of ) the line that is -A INPUT -m state --state NEW -m tcp -p tcp --dport 2001 -j ACCEPT how can i do this via SED or some other method via command line ? should i seek for .. e.g.. 2001 and then comment out that whole

Removing all special characters from a string in Bash

柔情痞子 提交于 2019-11-27 16:12:18
问题 I have a lot of text in lowercase, only problem is, that there is a lot of special characters, which I want to remove it all with numbers too. Next command it's not strong enough: tr -cd '[alpha]\n ' In case of éćščž and some others it returns "?" But I want to remove all of them. Is there any stronger command? I use linux mint 4.3.8(1)-release 回答1: You can use tr to print only the printable characters from a string like below. Just use the below command on your input file. tr -cd "[:print:]

How to concatenate multiple lines of output to one line?

梦想的初衷 提交于 2019-11-26 23:55:21
问题 If I run the command cat file | grep pattern , I get many lines of output. How do you concatenate all lines into one line, effectively replacing each "\n" with "\" " (end with " followed by space)? cat file | grep pattern | xargs sed s/\n/ /g isn't working for me. 回答1: Use tr '\n' ' ' to translate all newline characters to spaces: $ grep pattern file | tr '\n' ' ' Note: grep reads files, cat concatenates files. Don't cat file | grep ! Edit: tr can only handle single character translations.

is there a way to use tr/// (or equivalent) in java?

半城伤御伤魂 提交于 2019-11-26 22:00:41
问题 I would like to know if there is an equivalent to tr/// (as used in Perl) in Java. For example, if I wanted to replace all "s"s with "p"s in "mississippi" and vice versa, I could, in Perl, write #shebang and pragmas snipped... my $str = "mississippi"; $str =~ tr/sp/ps/; # $str = "mippippissi" print $str; The only way I can think of to do it in Java is to use a dummy character with the String.replace() method, i.e. String str = "mississippi"; str = str.replace('s', '#'); // # is just a dummy