Is it possible to delete a specific string with tr
command in a UNIX-Shell? For example: If I type:
tr -d "1."
and the input is 1.1231
, it would show 23
as an output, but I want it to show 1231
(notice only the first 1
has gone). How would I do that?
If you know a solution or a better way, please explain the syntax since I don't want to just copy&paste but also to learn.
I have huge problems with awk, so if you use this, please explain it even more.
Using tr Command to Delete Characters The most common usage for tr is to delete characters from an input stream. You can use the -d (--delete) option followed by the character, set of characters or an interpreted sequence.
The tr command (short for translate) is used to translate, squeeze, and delete characters from a string. You can also use tr to remove characters from a string. For demonstration purposes, we will use a sample string and then pipe it to the tr command.
The -d ( --delete ) option tells tr to delete characters specified in SET1.
The replaceAll() method accepts a regular expression and a String as parameters and, matches the contents of the current string with the given regular expression, in case of match, replaces the matched elements with the String. Retrieve the contents of the file as a String.
In your example above the cut command would suffice.
Example: echo '1.1231' | cut -d '.' -f 2
would return 1231
.
For more information on cut, just type man cut
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With