Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a specific string with tr

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.

like image 323
intelinside Avatar asked Apr 23 '12 14:04

intelinside


People also ask

How do I use tr to delete?

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.

How do I remove a specific string in Linux?

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.

Which option is used with tr command for deleting characters?

The -d ( --delete ) option tells tr to delete characters specified in SET1.

How do you delete a string from a file?

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.


1 Answers

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.

like image 53
Kevin Sjöberg Avatar answered Oct 03 '22 10:10

Kevin Sjöberg