Since you asked specifically about awk
, here is another one.
awk '{ gsub(/1\./,"") }1' input.txt
As any awk
tutorial will tell you, the general form of an awk
program is a sequence of 'condition { actions }'. If you have no actions, the default action is to print. If you have no conditions, the actions will be taken unconditionally. This program uses both of these special cases.
The first part is an action without a condition, i.e. it will be taken for all lines. The action is to substitute all occurrences of the regular expression /1\./
with nothing. So this will trim any '1.' (regardless of context) from a line.
The second part is a condition without an action, i.e. it will print if the condition is true, and the condition is always true. This is a common idiom for "we are done -- print whatever we have now". It consists simply of the constant 1
(which when used as a condition means "true", simply).
This could be reformulated in a number of ways. For example, you could factor the print into the first action;
awk '{ gsub(/1\./,""); print }' input.txt
Perhaps you want to substitute the integer part, i.e. any numbers before a period sign. The regex for that would be something like /[0-9]+\./
.
gsub
is a GNU extension, so you might want to replace it with sub
or some sort of loop if you need portability to legacy awk
syntax.
tr
substitutes individual characters, not strings. So (a) your command will remove all occurrences of "1" and "." anywhere in the input; and (b)tr
is not the right command for the task you are asking about. – Fare