How to use tr to replace string '-'

I have an output:

--
out1
--
out2
--
out3

      

I want to get the result:

out1
out2
out3

      

I thought about using:

tr '--' ''

      

but it doesn't recognize '-' as the first line I want to replace. How to solve this?

+2


source to share


5 answers


cat file | sed '/^--/d'

      



+6


source


Why not use it grep -v "^--$" yourfile.txt

?



+7


source


another way with awk

awk '!/^--$/' file

      

+2


source


The best you can do with tr

it is to remove the hyphens leaving blank lines. The best way to do what you need is Amro's answer with help sed

. It is important to remember that you tr

are dealing with lists of characters, not multi-character strings, so there is no point in inserting two hyphens in your parameters.

$ tr -d "-" textfile
out1

out2

out3

      

However, in order for the descriptor to tr

handle hyphens and additional characters, you need to terminate the parameters with --

or place a hyphen after the first character. Let's say you want to get rid of the hyphen and letter-o:

$ tr -d "-o" textfile
tr: invalid option -- 'o'
Try `tr --help' for more information.

$ tr -d -- "-o" textfile
ut1

ut2

ut3

$ tr -d "o-" textfile
ut1

ut2

ut3

      

It is often useful to use an option terminator --

when the character list is in a variable, so bad data does not create errors unnecessarily. This is true for teams other than tr

.

tr -d -- $charlist $file

      

0


source


You can do the same with grep

:

cat filename |grep -v "\--"

      

-1


source







All Articles