Converting unix epoch timestamp column in every row using sed or awk
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a file with the following input sample data:
1137921146.499 180900 61.153.158.197 1409
1137921158.698 181622 61.153.158.197 1409
1137921758.163 180026 221.226.124.114 1374
1137921802.016 179485 121.13.128.132 1409
the 1st column is unix epoch timestamp which i need to convert to human readable format plus i want the data to be delimited as follows
Sun Jan 22 01:12:26 PST 2006|180900|61.153.158.197|1409
Sun Jan 22 01:12:38 PST 2006|181622|61.153.158.19|1409
i tried to add delimeters using sed 's/ 1,/|/g' and converting the date using date -d @1137921146.499. But i am unable to club these two together in one command..
awk sed timestamps
add a comment |
up vote
1
down vote
favorite
I have a file with the following input sample data:
1137921146.499 180900 61.153.158.197 1409
1137921158.698 181622 61.153.158.197 1409
1137921758.163 180026 221.226.124.114 1374
1137921802.016 179485 121.13.128.132 1409
the 1st column is unix epoch timestamp which i need to convert to human readable format plus i want the data to be delimited as follows
Sun Jan 22 01:12:26 PST 2006|180900|61.153.158.197|1409
Sun Jan 22 01:12:38 PST 2006|181622|61.153.158.19|1409
i tried to add delimeters using sed 's/ 1,/|/g' and converting the date using date -d @1137921146.499. But i am unable to club these two together in one command..
awk sed timestamps
Downvoted because the problem consists of two separate problems and each can be found simply via a quick search.
– ceremcem
May 23 '17 at 7:11
1
After the edit it's okay, I think. He knows how to solve each problem, but not how to do combine it
– Philippos
May 23 '17 at 7:27
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a file with the following input sample data:
1137921146.499 180900 61.153.158.197 1409
1137921158.698 181622 61.153.158.197 1409
1137921758.163 180026 221.226.124.114 1374
1137921802.016 179485 121.13.128.132 1409
the 1st column is unix epoch timestamp which i need to convert to human readable format plus i want the data to be delimited as follows
Sun Jan 22 01:12:26 PST 2006|180900|61.153.158.197|1409
Sun Jan 22 01:12:38 PST 2006|181622|61.153.158.19|1409
i tried to add delimeters using sed 's/ 1,/|/g' and converting the date using date -d @1137921146.499. But i am unable to club these two together in one command..
awk sed timestamps
I have a file with the following input sample data:
1137921146.499 180900 61.153.158.197 1409
1137921158.698 181622 61.153.158.197 1409
1137921758.163 180026 221.226.124.114 1374
1137921802.016 179485 121.13.128.132 1409
the 1st column is unix epoch timestamp which i need to convert to human readable format plus i want the data to be delimited as follows
Sun Jan 22 01:12:26 PST 2006|180900|61.153.158.197|1409
Sun Jan 22 01:12:38 PST 2006|181622|61.153.158.19|1409
i tried to add delimeters using sed 's/ 1,/|/g' and converting the date using date -d @1137921146.499. But i am unable to club these two together in one command..
awk sed timestamps
awk sed timestamps
edited Nov 17 at 20:41
Rui F Ribeiro
38.2k1475123
38.2k1475123
asked May 23 '17 at 6:57
Prat
113
113
Downvoted because the problem consists of two separate problems and each can be found simply via a quick search.
– ceremcem
May 23 '17 at 7:11
1
After the edit it's okay, I think. He knows how to solve each problem, but not how to do combine it
– Philippos
May 23 '17 at 7:27
add a comment |
Downvoted because the problem consists of two separate problems and each can be found simply via a quick search.
– ceremcem
May 23 '17 at 7:11
1
After the edit it's okay, I think. He knows how to solve each problem, but not how to do combine it
– Philippos
May 23 '17 at 7:27
Downvoted because the problem consists of two separate problems and each can be found simply via a quick search.
– ceremcem
May 23 '17 at 7:11
Downvoted because the problem consists of two separate problems and each can be found simply via a quick search.
– ceremcem
May 23 '17 at 7:11
1
1
After the edit it's okay, I think. He knows how to solve each problem, but not how to do combine it
– Philippos
May 23 '17 at 7:27
After the edit it's okay, I think. He knows how to solve each problem, but not how to do combine it
– Philippos
May 23 '17 at 7:27
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
You can use awk program like this:
awk '"$3"' file
the core is to use strftime function to convert epoch to date format
Here is the output:
#awk '"$3"' file
Sun Jan 22 10:12:26 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 2006|179485|121.13.128.132|1409
P.S. Or you can use implicit output delimiter:
awk 'BEGIN " $1= strftime("%c",$1) 1' file
add a comment |
up vote
1
down vote
Or with your shell:
while read timestamp pid ip port; do
echo "$(date -d @$timestamp)|$pid|$ip|$port"
done <yourfile
add a comment |
up vote
1
down vote
Using what you already know:
- GNU
date
can convert a timestamp to a formatted date by giving it@timestamp
. - Replacing spaces by
|
will give you the output you want.
To that, we add
- GNU
date
may operate on a file, converting dates in one batch.
To batch convert dates with GNU date
we have to extract the timestamps and prefix them with @
:
$ sed 's/^([^ ]*).*$/@1/' data.in
@1137921146.499
@1137921158.698
@1137921758.163
@1137921802.016
The sed
expression substitutes each line with the first space-delimited field prefixed with @
.
With bash
(and ksh93
, or any shell that understands process substitutions):
$ date -f <( sed 's/^([^ ]*).*$/@1/' data.in )
Sun Jan 22 10:12:26 CET 2006
Sun Jan 22 10:12:38 CET 2006
Sun Jan 22 10:22:38 CET 2006
Sun Jan 22 10:23:22 CET 2006
Then we need to take the other fields of the input data and replace the delimiters:
$ cut -d ' ' -f 2- data.in | tr ' ' '|'
180900|61.153.158.197|1409
181622|61.153.158.197|1409
180026|221.226.124.114|1374
179485|121.13.128.132|1409
Then we paste these two things together with a |
as delimiter:
$ paste -d '|' <( date -f <( sed 's/^([^ ]*).*$/@1/' data.in ) ) <( cut -d ' ' -f 2- data.in | tr ' ' '|' )
Sun Jan 22 10:12:26 CET 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 CET 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 CET 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 CET 2006|179485|121.13.128.132|1409
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
You can use awk program like this:
awk '"$3"' file
the core is to use strftime function to convert epoch to date format
Here is the output:
#awk '"$3"' file
Sun Jan 22 10:12:26 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 2006|179485|121.13.128.132|1409
P.S. Or you can use implicit output delimiter:
awk 'BEGIN " $1= strftime("%c",$1) 1' file
add a comment |
up vote
4
down vote
You can use awk program like this:
awk '"$3"' file
the core is to use strftime function to convert epoch to date format
Here is the output:
#awk '"$3"' file
Sun Jan 22 10:12:26 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 2006|179485|121.13.128.132|1409
P.S. Or you can use implicit output delimiter:
awk 'BEGIN " $1= strftime("%c",$1) 1' file
add a comment |
up vote
4
down vote
up vote
4
down vote
You can use awk program like this:
awk '"$3"' file
the core is to use strftime function to convert epoch to date format
Here is the output:
#awk '"$3"' file
Sun Jan 22 10:12:26 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 2006|179485|121.13.128.132|1409
P.S. Or you can use implicit output delimiter:
awk 'BEGIN " $1= strftime("%c",$1) 1' file
You can use awk program like this:
awk '"$3"' file
the core is to use strftime function to convert epoch to date format
Here is the output:
#awk '"$3"' file
Sun Jan 22 10:12:26 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 2006|179485|121.13.128.132|1409
P.S. Or you can use implicit output delimiter:
awk 'BEGIN " $1= strftime("%c",$1) 1' file
edited May 22 at 3:01
answered May 23 '17 at 7:35
Romeo Ninov
4,76431626
4,76431626
add a comment |
add a comment |
up vote
1
down vote
Or with your shell:
while read timestamp pid ip port; do
echo "$(date -d @$timestamp)|$pid|$ip|$port"
done <yourfile
add a comment |
up vote
1
down vote
Or with your shell:
while read timestamp pid ip port; do
echo "$(date -d @$timestamp)|$pid|$ip|$port"
done <yourfile
add a comment |
up vote
1
down vote
up vote
1
down vote
Or with your shell:
while read timestamp pid ip port; do
echo "$(date -d @$timestamp)|$pid|$ip|$port"
done <yourfile
Or with your shell:
while read timestamp pid ip port; do
echo "$(date -d @$timestamp)|$pid|$ip|$port"
done <yourfile
edited May 23 '17 at 7:43
answered May 23 '17 at 7:38
Philippos
5,98211547
5,98211547
add a comment |
add a comment |
up vote
1
down vote
Using what you already know:
- GNU
date
can convert a timestamp to a formatted date by giving it@timestamp
. - Replacing spaces by
|
will give you the output you want.
To that, we add
- GNU
date
may operate on a file, converting dates in one batch.
To batch convert dates with GNU date
we have to extract the timestamps and prefix them with @
:
$ sed 's/^([^ ]*).*$/@1/' data.in
@1137921146.499
@1137921158.698
@1137921758.163
@1137921802.016
The sed
expression substitutes each line with the first space-delimited field prefixed with @
.
With bash
(and ksh93
, or any shell that understands process substitutions):
$ date -f <( sed 's/^([^ ]*).*$/@1/' data.in )
Sun Jan 22 10:12:26 CET 2006
Sun Jan 22 10:12:38 CET 2006
Sun Jan 22 10:22:38 CET 2006
Sun Jan 22 10:23:22 CET 2006
Then we need to take the other fields of the input data and replace the delimiters:
$ cut -d ' ' -f 2- data.in | tr ' ' '|'
180900|61.153.158.197|1409
181622|61.153.158.197|1409
180026|221.226.124.114|1374
179485|121.13.128.132|1409
Then we paste these two things together with a |
as delimiter:
$ paste -d '|' <( date -f <( sed 's/^([^ ]*).*$/@1/' data.in ) ) <( cut -d ' ' -f 2- data.in | tr ' ' '|' )
Sun Jan 22 10:12:26 CET 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 CET 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 CET 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 CET 2006|179485|121.13.128.132|1409
add a comment |
up vote
1
down vote
Using what you already know:
- GNU
date
can convert a timestamp to a formatted date by giving it@timestamp
. - Replacing spaces by
|
will give you the output you want.
To that, we add
- GNU
date
may operate on a file, converting dates in one batch.
To batch convert dates with GNU date
we have to extract the timestamps and prefix them with @
:
$ sed 's/^([^ ]*).*$/@1/' data.in
@1137921146.499
@1137921158.698
@1137921758.163
@1137921802.016
The sed
expression substitutes each line with the first space-delimited field prefixed with @
.
With bash
(and ksh93
, or any shell that understands process substitutions):
$ date -f <( sed 's/^([^ ]*).*$/@1/' data.in )
Sun Jan 22 10:12:26 CET 2006
Sun Jan 22 10:12:38 CET 2006
Sun Jan 22 10:22:38 CET 2006
Sun Jan 22 10:23:22 CET 2006
Then we need to take the other fields of the input data and replace the delimiters:
$ cut -d ' ' -f 2- data.in | tr ' ' '|'
180900|61.153.158.197|1409
181622|61.153.158.197|1409
180026|221.226.124.114|1374
179485|121.13.128.132|1409
Then we paste these two things together with a |
as delimiter:
$ paste -d '|' <( date -f <( sed 's/^([^ ]*).*$/@1/' data.in ) ) <( cut -d ' ' -f 2- data.in | tr ' ' '|' )
Sun Jan 22 10:12:26 CET 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 CET 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 CET 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 CET 2006|179485|121.13.128.132|1409
add a comment |
up vote
1
down vote
up vote
1
down vote
Using what you already know:
- GNU
date
can convert a timestamp to a formatted date by giving it@timestamp
. - Replacing spaces by
|
will give you the output you want.
To that, we add
- GNU
date
may operate on a file, converting dates in one batch.
To batch convert dates with GNU date
we have to extract the timestamps and prefix them with @
:
$ sed 's/^([^ ]*).*$/@1/' data.in
@1137921146.499
@1137921158.698
@1137921758.163
@1137921802.016
The sed
expression substitutes each line with the first space-delimited field prefixed with @
.
With bash
(and ksh93
, or any shell that understands process substitutions):
$ date -f <( sed 's/^([^ ]*).*$/@1/' data.in )
Sun Jan 22 10:12:26 CET 2006
Sun Jan 22 10:12:38 CET 2006
Sun Jan 22 10:22:38 CET 2006
Sun Jan 22 10:23:22 CET 2006
Then we need to take the other fields of the input data and replace the delimiters:
$ cut -d ' ' -f 2- data.in | tr ' ' '|'
180900|61.153.158.197|1409
181622|61.153.158.197|1409
180026|221.226.124.114|1374
179485|121.13.128.132|1409
Then we paste these two things together with a |
as delimiter:
$ paste -d '|' <( date -f <( sed 's/^([^ ]*).*$/@1/' data.in ) ) <( cut -d ' ' -f 2- data.in | tr ' ' '|' )
Sun Jan 22 10:12:26 CET 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 CET 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 CET 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 CET 2006|179485|121.13.128.132|1409
Using what you already know:
- GNU
date
can convert a timestamp to a formatted date by giving it@timestamp
. - Replacing spaces by
|
will give you the output you want.
To that, we add
- GNU
date
may operate on a file, converting dates in one batch.
To batch convert dates with GNU date
we have to extract the timestamps and prefix them with @
:
$ sed 's/^([^ ]*).*$/@1/' data.in
@1137921146.499
@1137921158.698
@1137921758.163
@1137921802.016
The sed
expression substitutes each line with the first space-delimited field prefixed with @
.
With bash
(and ksh93
, or any shell that understands process substitutions):
$ date -f <( sed 's/^([^ ]*).*$/@1/' data.in )
Sun Jan 22 10:12:26 CET 2006
Sun Jan 22 10:12:38 CET 2006
Sun Jan 22 10:22:38 CET 2006
Sun Jan 22 10:23:22 CET 2006
Then we need to take the other fields of the input data and replace the delimiters:
$ cut -d ' ' -f 2- data.in | tr ' ' '|'
180900|61.153.158.197|1409
181622|61.153.158.197|1409
180026|221.226.124.114|1374
179485|121.13.128.132|1409
Then we paste these two things together with a |
as delimiter:
$ paste -d '|' <( date -f <( sed 's/^([^ ]*).*$/@1/' data.in ) ) <( cut -d ' ' -f 2- data.in | tr ' ' '|' )
Sun Jan 22 10:12:26 CET 2006|180900|61.153.158.197|1409
Sun Jan 22 10:12:38 CET 2006|181622|61.153.158.197|1409
Sun Jan 22 10:22:38 CET 2006|180026|221.226.124.114|1374
Sun Jan 22 10:23:22 CET 2006|179485|121.13.128.132|1409
answered May 23 '17 at 7:55
Kusalananda
116k15218352
116k15218352
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f366706%2fconverting-unix-epoch-timestamp-column-in-every-row-using-sed-or-awk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Downvoted because the problem consists of two separate problems and each can be found simply via a quick search.
– ceremcem
May 23 '17 at 7:11
1
After the edit it's okay, I think. He knows how to solve each problem, but not how to do combine it
– Philippos
May 23 '17 at 7:27