I am new to programming!!
Can anyone help to remove the :
at the first position in a timestamp: :29.06.2019 23:03:17
Presently I am trying to do it using awk/cut commands as shown below:
TDS="$(grep 'Logfile started' process.log | awk '{print $3,$4}' | cut -d: -f2)"
echo "$TDS"
29.06.2019 23
And the output is not what I wanted! I want to print it as 29.06.2019 23:03:17
.
To cut off the first character, you can also use
cut -c
:Use
instead of
to get anything from the second field to the end of line:
Here is a
sed
solution:What the command
sed 's/^://'
is doing is substitutes
the colon character:
from the beginning^
of each line with the empty string//
.Here is a tricky
awk
solution, where we changing the field separator to^:
, described above, and output the second field (of each line):The task could be accomplished also with
grep
(explanation), probably this could be the fastest solution for large amount of data:Or process the file directly by the following command, where the limitation
^
is removed:The above could be achieved also by
sed
and capture groups()->\1
:Where the expression
^.*<something>.*$
will match the whole line, that contains<something>
. The commands/old/new/
will substitute this line by the content of the first capture group (the expression in the brackets could be more concrete). The option-r
enables the extended regular expressions. The option-n
will suppress the normal output ofsed
and finally the commandp
will print the matches.awk
is a cool tool, and you can solve very complex tasks with it. But for your question I would rather stick to the basic capabilities of bash.For this easy substing removal, I would do the following:
this will print:
When I was in your position and started to learn programming and bash, I learned a lot of this Handbook:
ABS - Advanced Bash-Scripting Guide
Some more examples and interesting information to your problem can be found here, look for 'Substring Removal'.
Since you're already processing this in
awk
, you may as well do the whole thing directly:The
sub
command's general format issub(/REGEX/, REPLACEMENT, TARGET)
and will replace all matches for the regular expressionREGEX
with the stringREPLACEMENT
in the input stringTARGET
. Here, we are replacing the first:
(^
means "the beginning") from the 3rd field ($3
) with nothing.Of course, if you're doing that in awk, you may as well do everything in awk and get the whole thing done in a single operation:
Or, in your case:
Another bash solution:
xargs
convertsstdin
(standard input) to positional parameters forbash
command.echo
withtail -n1 /path/to/reportfile.txt
.