user109447
user109447

Reputation: 1119

timestamp - regex to change from one format to another

I have a file: START.txt with lines like:

13.03.2013 12:13:42|STRING1|NUMBER1|NUMBER2|NUMBER3

and I want change all the lines from this file to the following format:

12:13|STRING1|NUMBER1|NUMBER2|NUMBER3

How can this be done?

Upvotes: 0

Views: 504

Answers (4)

Jotne
Jotne

Reputation: 41456

Here is an awk

awk '{sub(/:[0-9]+\|/,"|",$2);print $2}' file
12:13|STRING1|NUMBER1|NUMBER2|NUMBER3

Just delete seconds from the second part and print it.

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174706

Try this GNU sed command,

$ sed -r 's/^[^ ]+? ([0-9]{2}:[0-9]{2}):[0-9]{2}(.*)$/\1\2/g' file
12:13|STRING1|NUMBER1|NUMBER2|NUMBER3

And through awk,

$ awk -F'|' -v OFS='|' '{sub (/^[^ ]+? /,"",$1); sub (/:[0-9]{2}$/,"",$1);}1' file
12:13|STRING1|NUMBER1|NUMBER2|NUMBER3

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77095

Using awk:

$ awk 'BEGIN{FS=OFS="|"}{split($1,fld,/[.: ]/);$1=fld[4]":"fld[5]}1' file
12:13|STRING1|NUMBER1|NUMBER2|NUMBER3
  • Set the Input and Output Field Separators to |.
  • Split the First column on space, . and : and put the values in an array fld.
  • Re-construct column 1 to the desired array values
  • Using 1 you print the line (which is idiomatic way of saying {print $0}.

Upvotes: 1

mifi79
mifi79

Reputation: 1106

Using this pattern should match each line and capture the portion that you wish to retain:

^\d{2}\.\d{2}\.\d{4}\s(.*)$

You can see an example of the pattern in action here: http://regexr.com/38vq0

Upvotes: 1

Related Questions