Merge multiple .bash_history files with timestamps

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












I have generated multiple .bash_history files by switching between snapshots on a VM. I would like to recover all these files into the single live .bash_history file in my currently running instance.



The files have timestamps in them and I would like to preserve them as much as possible. However, it looks like some lines don't have timestamps on them. For example:



#1542817796
vi ~/.bash_profile
set +x
cd -
cd w/Edge-Agent/edge-agent-ged/
make
set -x
man make
make -npq
make -npq .DEFAULT
make
make
ack npq /etc
ack npq /usr/share/
set +x
ack npq /usr/share/
sudo vi /usr/share/bash-completion/completions/make
dv
hg diff
vi ~/.bashrc
#1542826064
ls
#1542826066
vi ~/.bashrc
#1542826074
cd ..
#1542826321


I would like to order the merged list by timestamp because I was switching back and forth between the snapshots and the files interleave with each other. If there's no timestamp available for a command we can infer it from the last timestamp before and the first timestamp after.










share|improve this question























  • What are the existing bash_history filenames to merge?
    – Jeff Schaller
    Dec 5 at 0:42










  • edgeos_history.[012]
    – Stephen Rasku
    Dec 5 at 0:43










  • How to read that sample? Any command without a time stamp needs the previous time stamp assigned?
    – RudiC
    Dec 5 at 14:07










  • @RudiC sure. That works
    – Stephen Rasku
    Dec 5 at 14:08














up vote
0
down vote

favorite
1












I have generated multiple .bash_history files by switching between snapshots on a VM. I would like to recover all these files into the single live .bash_history file in my currently running instance.



The files have timestamps in them and I would like to preserve them as much as possible. However, it looks like some lines don't have timestamps on them. For example:



#1542817796
vi ~/.bash_profile
set +x
cd -
cd w/Edge-Agent/edge-agent-ged/
make
set -x
man make
make -npq
make -npq .DEFAULT
make
make
ack npq /etc
ack npq /usr/share/
set +x
ack npq /usr/share/
sudo vi /usr/share/bash-completion/completions/make
dv
hg diff
vi ~/.bashrc
#1542826064
ls
#1542826066
vi ~/.bashrc
#1542826074
cd ..
#1542826321


I would like to order the merged list by timestamp because I was switching back and forth between the snapshots and the files interleave with each other. If there's no timestamp available for a command we can infer it from the last timestamp before and the first timestamp after.










share|improve this question























  • What are the existing bash_history filenames to merge?
    – Jeff Schaller
    Dec 5 at 0:42










  • edgeos_history.[012]
    – Stephen Rasku
    Dec 5 at 0:43










  • How to read that sample? Any command without a time stamp needs the previous time stamp assigned?
    – RudiC
    Dec 5 at 14:07










  • @RudiC sure. That works
    – Stephen Rasku
    Dec 5 at 14:08












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have generated multiple .bash_history files by switching between snapshots on a VM. I would like to recover all these files into the single live .bash_history file in my currently running instance.



The files have timestamps in them and I would like to preserve them as much as possible. However, it looks like some lines don't have timestamps on them. For example:



#1542817796
vi ~/.bash_profile
set +x
cd -
cd w/Edge-Agent/edge-agent-ged/
make
set -x
man make
make -npq
make -npq .DEFAULT
make
make
ack npq /etc
ack npq /usr/share/
set +x
ack npq /usr/share/
sudo vi /usr/share/bash-completion/completions/make
dv
hg diff
vi ~/.bashrc
#1542826064
ls
#1542826066
vi ~/.bashrc
#1542826074
cd ..
#1542826321


I would like to order the merged list by timestamp because I was switching back and forth between the snapshots and the files interleave with each other. If there's no timestamp available for a command we can infer it from the last timestamp before and the first timestamp after.










share|improve this question















I have generated multiple .bash_history files by switching between snapshots on a VM. I would like to recover all these files into the single live .bash_history file in my currently running instance.



The files have timestamps in them and I would like to preserve them as much as possible. However, it looks like some lines don't have timestamps on them. For example:



#1542817796
vi ~/.bash_profile
set +x
cd -
cd w/Edge-Agent/edge-agent-ged/
make
set -x
man make
make -npq
make -npq .DEFAULT
make
make
ack npq /etc
ack npq /usr/share/
set +x
ack npq /usr/share/
sudo vi /usr/share/bash-completion/completions/make
dv
hg diff
vi ~/.bashrc
#1542826064
ls
#1542826066
vi ~/.bashrc
#1542826074
cd ..
#1542826321


I would like to order the merged list by timestamp because I was switching back and forth between the snapshots and the files interleave with each other. If there's no timestamp available for a command we can infer it from the last timestamp before and the first timestamp after.







bash command-history






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 at 0:30

























asked Dec 4 at 23:11









Stephen Rasku

1035




1035











  • What are the existing bash_history filenames to merge?
    – Jeff Schaller
    Dec 5 at 0:42










  • edgeos_history.[012]
    – Stephen Rasku
    Dec 5 at 0:43










  • How to read that sample? Any command without a time stamp needs the previous time stamp assigned?
    – RudiC
    Dec 5 at 14:07










  • @RudiC sure. That works
    – Stephen Rasku
    Dec 5 at 14:08
















  • What are the existing bash_history filenames to merge?
    – Jeff Schaller
    Dec 5 at 0:42










  • edgeos_history.[012]
    – Stephen Rasku
    Dec 5 at 0:43










  • How to read that sample? Any command without a time stamp needs the previous time stamp assigned?
    – RudiC
    Dec 5 at 14:07










  • @RudiC sure. That works
    – Stephen Rasku
    Dec 5 at 14:08















What are the existing bash_history filenames to merge?
– Jeff Schaller
Dec 5 at 0:42




What are the existing bash_history filenames to merge?
– Jeff Schaller
Dec 5 at 0:42












edgeos_history.[012]
– Stephen Rasku
Dec 5 at 0:43




edgeos_history.[012]
– Stephen Rasku
Dec 5 at 0:43












How to read that sample? Any command without a time stamp needs the previous time stamp assigned?
– RudiC
Dec 5 at 14:07




How to read that sample? Any command without a time stamp needs the previous time stamp assigned?
– RudiC
Dec 5 at 14:07












@RudiC sure. That works
– Stephen Rasku
Dec 5 at 14:08




@RudiC sure. That works
– Stephen Rasku
Dec 5 at 14:08










3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










Using GNU m4 (which supports numbered diversions up to LONG_MAX) with some additional sed to prepare the input:



#!/bin/sh

cat <<"EOF"
m4_define(m4_chunk,`m4_divert(-1)m4_undivert($1)m4_divert($1)#$1')m4_dnl
m4_define(m4_,``m4_'')m4_dnl
m4_define(`m4_LQ',`m4_changequote([,])`m4_dnl'
m4_changequote`'')m4_dnl
m4_define(`m4_RQ',`m4_changequote([,])m4_dnl`
'm4_changequote`'')m4_dnl
EOF
sed -e "s/m4_/m4_()/g;s/`/m4_LQ()/g;s/'/m4_RQ()/g;s/m4_/`'m4_/g" -e 's/^#([0-9]+)$/m4_chunk(1)/'
| m4 -P


Usage:



cat edgeos_history.* | sort_history.sh > merged_history


The m4_chunk function does most of the work, as the separate blocks are "undiverted" in ascending numerical order when all the input ends. The rest of the script is to protect any quotemarks and existing m4_ sequences in the input from being interpreted by m4.



LQ and RQ definitions based on those in Michael Breen's Notes on the M4 Macro Language.






share|improve this answer






















  • Does it have to be left-justified? Just curious.
    – Stephen Rasku
    Dec 5 at 14:36










  • The lines inside the here-doc do, because m4 passes whitespace through unchanged.
    – JigglyNaga
    Dec 5 at 14:45










  • Some of the lines are in more than one file so I'm getting duplicate lines like this: #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T . Is there a way to resolve this? If not, I could write a simple Perl script to post-process the file.
    – Stephen Rasku
    Dec 6 at 15:33










  • You could prepend m4_divert(-1)m4_undivert($1) to the definition of m4_chunk, so that any duplicate timestamp discards any lines previously seen with that same timestamp.
    – JigglyNaga
    Dec 6 at 17:00










  • Cool. That works.
    – Stephen Rasku
    Dec 6 at 17:11

















up vote
0
down vote













Why not just do something like cat /path/to/one/.bash_history >> /path/to/live/.bash_history then you could remove duplicate enteries with awk '!seen[$0]++' .bash_history >> .bash_history2 then remove the original and rename .bash_history2 to .bash_history. However removing the duplicates like that will leave empty timestamps.






share|improve this answer




















  • I would like to sort by time stamp. I’ll make that clearer on my question.
    – Stephen Rasku
    Dec 4 at 23:48










  • Yea that makes it more complicated
    – Michael Prokopec
    Dec 5 at 0:10

















up vote
0
down vote













Would this help:



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort
#1542817796 ack npq /etc
#1542817796 ack npq /usr/share/
#1542817796 ack npq /usr/share/
#1542817796 cd -
#1542817796 cd w/Edge-Agent/edge-agent-ged/
#1542817796 dv
#1542817796 hg diff
#1542817796 make
#1542817796 make
#1542817796 make
#1542817796 make -npq
.
.
.


Make sure to list ALL history files as input stream to awk.



EDIT: Some non-satisfying, non-portable essays to retain the original order of the commands:



awk '/^#[0-9]*$/ TS = $0; next print TS, NR, $0' file* | sort -k1,1 -k2,2n


.



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort -mk1,1 





share|improve this answer






















  • Since this uses the same timestamp for all consecutive untimestamped entries it will sort those entries lexically instead of by the original order in the file. I think this can be improved by incrementing the inferred timestamp somehow.
    – Stephen Rasku
    Dec 5 at 14:23










  • I edited two more options in, none of which is necessarily satisfying nor portable.
    – RudiC
    Dec 5 at 14:32










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f486022%2fmerge-multiple-bash-history-files-with-timestamps%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Using GNU m4 (which supports numbered diversions up to LONG_MAX) with some additional sed to prepare the input:



#!/bin/sh

cat <<"EOF"
m4_define(m4_chunk,`m4_divert(-1)m4_undivert($1)m4_divert($1)#$1')m4_dnl
m4_define(m4_,``m4_'')m4_dnl
m4_define(`m4_LQ',`m4_changequote([,])`m4_dnl'
m4_changequote`'')m4_dnl
m4_define(`m4_RQ',`m4_changequote([,])m4_dnl`
'm4_changequote`'')m4_dnl
EOF
sed -e "s/m4_/m4_()/g;s/`/m4_LQ()/g;s/'/m4_RQ()/g;s/m4_/`'m4_/g" -e 's/^#([0-9]+)$/m4_chunk(1)/'
| m4 -P


Usage:



cat edgeos_history.* | sort_history.sh > merged_history


The m4_chunk function does most of the work, as the separate blocks are "undiverted" in ascending numerical order when all the input ends. The rest of the script is to protect any quotemarks and existing m4_ sequences in the input from being interpreted by m4.



LQ and RQ definitions based on those in Michael Breen's Notes on the M4 Macro Language.






share|improve this answer






















  • Does it have to be left-justified? Just curious.
    – Stephen Rasku
    Dec 5 at 14:36










  • The lines inside the here-doc do, because m4 passes whitespace through unchanged.
    – JigglyNaga
    Dec 5 at 14:45










  • Some of the lines are in more than one file so I'm getting duplicate lines like this: #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T . Is there a way to resolve this? If not, I could write a simple Perl script to post-process the file.
    – Stephen Rasku
    Dec 6 at 15:33










  • You could prepend m4_divert(-1)m4_undivert($1) to the definition of m4_chunk, so that any duplicate timestamp discards any lines previously seen with that same timestamp.
    – JigglyNaga
    Dec 6 at 17:00










  • Cool. That works.
    – Stephen Rasku
    Dec 6 at 17:11














up vote
0
down vote



accepted










Using GNU m4 (which supports numbered diversions up to LONG_MAX) with some additional sed to prepare the input:



#!/bin/sh

cat <<"EOF"
m4_define(m4_chunk,`m4_divert(-1)m4_undivert($1)m4_divert($1)#$1')m4_dnl
m4_define(m4_,``m4_'')m4_dnl
m4_define(`m4_LQ',`m4_changequote([,])`m4_dnl'
m4_changequote`'')m4_dnl
m4_define(`m4_RQ',`m4_changequote([,])m4_dnl`
'm4_changequote`'')m4_dnl
EOF
sed -e "s/m4_/m4_()/g;s/`/m4_LQ()/g;s/'/m4_RQ()/g;s/m4_/`'m4_/g" -e 's/^#([0-9]+)$/m4_chunk(1)/'
| m4 -P


Usage:



cat edgeos_history.* | sort_history.sh > merged_history


The m4_chunk function does most of the work, as the separate blocks are "undiverted" in ascending numerical order when all the input ends. The rest of the script is to protect any quotemarks and existing m4_ sequences in the input from being interpreted by m4.



LQ and RQ definitions based on those in Michael Breen's Notes on the M4 Macro Language.






share|improve this answer






















  • Does it have to be left-justified? Just curious.
    – Stephen Rasku
    Dec 5 at 14:36










  • The lines inside the here-doc do, because m4 passes whitespace through unchanged.
    – JigglyNaga
    Dec 5 at 14:45










  • Some of the lines are in more than one file so I'm getting duplicate lines like this: #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T . Is there a way to resolve this? If not, I could write a simple Perl script to post-process the file.
    – Stephen Rasku
    Dec 6 at 15:33










  • You could prepend m4_divert(-1)m4_undivert($1) to the definition of m4_chunk, so that any duplicate timestamp discards any lines previously seen with that same timestamp.
    – JigglyNaga
    Dec 6 at 17:00










  • Cool. That works.
    – Stephen Rasku
    Dec 6 at 17:11












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Using GNU m4 (which supports numbered diversions up to LONG_MAX) with some additional sed to prepare the input:



#!/bin/sh

cat <<"EOF"
m4_define(m4_chunk,`m4_divert(-1)m4_undivert($1)m4_divert($1)#$1')m4_dnl
m4_define(m4_,``m4_'')m4_dnl
m4_define(`m4_LQ',`m4_changequote([,])`m4_dnl'
m4_changequote`'')m4_dnl
m4_define(`m4_RQ',`m4_changequote([,])m4_dnl`
'm4_changequote`'')m4_dnl
EOF
sed -e "s/m4_/m4_()/g;s/`/m4_LQ()/g;s/'/m4_RQ()/g;s/m4_/`'m4_/g" -e 's/^#([0-9]+)$/m4_chunk(1)/'
| m4 -P


Usage:



cat edgeos_history.* | sort_history.sh > merged_history


The m4_chunk function does most of the work, as the separate blocks are "undiverted" in ascending numerical order when all the input ends. The rest of the script is to protect any quotemarks and existing m4_ sequences in the input from being interpreted by m4.



LQ and RQ definitions based on those in Michael Breen's Notes on the M4 Macro Language.






share|improve this answer














Using GNU m4 (which supports numbered diversions up to LONG_MAX) with some additional sed to prepare the input:



#!/bin/sh

cat <<"EOF"
m4_define(m4_chunk,`m4_divert(-1)m4_undivert($1)m4_divert($1)#$1')m4_dnl
m4_define(m4_,``m4_'')m4_dnl
m4_define(`m4_LQ',`m4_changequote([,])`m4_dnl'
m4_changequote`'')m4_dnl
m4_define(`m4_RQ',`m4_changequote([,])m4_dnl`
'm4_changequote`'')m4_dnl
EOF
sed -e "s/m4_/m4_()/g;s/`/m4_LQ()/g;s/'/m4_RQ()/g;s/m4_/`'m4_/g" -e 's/^#([0-9]+)$/m4_chunk(1)/'
| m4 -P


Usage:



cat edgeos_history.* | sort_history.sh > merged_history


The m4_chunk function does most of the work, as the separate blocks are "undiverted" in ascending numerical order when all the input ends. The rest of the script is to protect any quotemarks and existing m4_ sequences in the input from being interpreted by m4.



LQ and RQ definitions based on those in Michael Breen's Notes on the M4 Macro Language.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 6 at 17:12

























answered Dec 5 at 14:32









JigglyNaga

3,613829




3,613829











  • Does it have to be left-justified? Just curious.
    – Stephen Rasku
    Dec 5 at 14:36










  • The lines inside the here-doc do, because m4 passes whitespace through unchanged.
    – JigglyNaga
    Dec 5 at 14:45










  • Some of the lines are in more than one file so I'm getting duplicate lines like this: #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T . Is there a way to resolve this? If not, I could write a simple Perl script to post-process the file.
    – Stephen Rasku
    Dec 6 at 15:33










  • You could prepend m4_divert(-1)m4_undivert($1) to the definition of m4_chunk, so that any duplicate timestamp discards any lines previously seen with that same timestamp.
    – JigglyNaga
    Dec 6 at 17:00










  • Cool. That works.
    – Stephen Rasku
    Dec 6 at 17:11
















  • Does it have to be left-justified? Just curious.
    – Stephen Rasku
    Dec 5 at 14:36










  • The lines inside the here-doc do, because m4 passes whitespace through unchanged.
    – JigglyNaga
    Dec 5 at 14:45










  • Some of the lines are in more than one file so I'm getting duplicate lines like this: #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T . Is there a way to resolve this? If not, I could write a simple Perl script to post-process the file.
    – Stephen Rasku
    Dec 6 at 15:33










  • You could prepend m4_divert(-1)m4_undivert($1) to the definition of m4_chunk, so that any duplicate timestamp discards any lines previously seen with that same timestamp.
    – JigglyNaga
    Dec 6 at 17:00










  • Cool. That works.
    – Stephen Rasku
    Dec 6 at 17:11















Does it have to be left-justified? Just curious.
– Stephen Rasku
Dec 5 at 14:36




Does it have to be left-justified? Just curious.
– Stephen Rasku
Dec 5 at 14:36












The lines inside the here-doc do, because m4 passes whitespace through unchanged.
– JigglyNaga
Dec 5 at 14:45




The lines inside the here-doc do, because m4 passes whitespace through unchanged.
– JigglyNaga
Dec 5 at 14:45












Some of the lines are in more than one file so I'm getting duplicate lines like this: #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T . Is there a way to resolve this? If not, I could write a simple Perl script to post-process the file.
– Stephen Rasku
Dec 6 at 15:33




Some of the lines are in more than one file so I'm getting duplicate lines like this: #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T #1542121355 scripts/run-system-tests-local.sh -d srasku0 -s TC263808 | ts %T . Is there a way to resolve this? If not, I could write a simple Perl script to post-process the file.
– Stephen Rasku
Dec 6 at 15:33












You could prepend m4_divert(-1)m4_undivert($1) to the definition of m4_chunk, so that any duplicate timestamp discards any lines previously seen with that same timestamp.
– JigglyNaga
Dec 6 at 17:00




You could prepend m4_divert(-1)m4_undivert($1) to the definition of m4_chunk, so that any duplicate timestamp discards any lines previously seen with that same timestamp.
– JigglyNaga
Dec 6 at 17:00












Cool. That works.
– Stephen Rasku
Dec 6 at 17:11




Cool. That works.
– Stephen Rasku
Dec 6 at 17:11












up vote
0
down vote













Why not just do something like cat /path/to/one/.bash_history >> /path/to/live/.bash_history then you could remove duplicate enteries with awk '!seen[$0]++' .bash_history >> .bash_history2 then remove the original and rename .bash_history2 to .bash_history. However removing the duplicates like that will leave empty timestamps.






share|improve this answer




















  • I would like to sort by time stamp. I’ll make that clearer on my question.
    – Stephen Rasku
    Dec 4 at 23:48










  • Yea that makes it more complicated
    – Michael Prokopec
    Dec 5 at 0:10














up vote
0
down vote













Why not just do something like cat /path/to/one/.bash_history >> /path/to/live/.bash_history then you could remove duplicate enteries with awk '!seen[$0]++' .bash_history >> .bash_history2 then remove the original and rename .bash_history2 to .bash_history. However removing the duplicates like that will leave empty timestamps.






share|improve this answer




















  • I would like to sort by time stamp. I’ll make that clearer on my question.
    – Stephen Rasku
    Dec 4 at 23:48










  • Yea that makes it more complicated
    – Michael Prokopec
    Dec 5 at 0:10












up vote
0
down vote










up vote
0
down vote









Why not just do something like cat /path/to/one/.bash_history >> /path/to/live/.bash_history then you could remove duplicate enteries with awk '!seen[$0]++' .bash_history >> .bash_history2 then remove the original and rename .bash_history2 to .bash_history. However removing the duplicates like that will leave empty timestamps.






share|improve this answer












Why not just do something like cat /path/to/one/.bash_history >> /path/to/live/.bash_history then you could remove duplicate enteries with awk '!seen[$0]++' .bash_history >> .bash_history2 then remove the original and rename .bash_history2 to .bash_history. However removing the duplicates like that will leave empty timestamps.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 4 at 23:28









Michael Prokopec

94916




94916











  • I would like to sort by time stamp. I’ll make that clearer on my question.
    – Stephen Rasku
    Dec 4 at 23:48










  • Yea that makes it more complicated
    – Michael Prokopec
    Dec 5 at 0:10
















  • I would like to sort by time stamp. I’ll make that clearer on my question.
    – Stephen Rasku
    Dec 4 at 23:48










  • Yea that makes it more complicated
    – Michael Prokopec
    Dec 5 at 0:10















I would like to sort by time stamp. I’ll make that clearer on my question.
– Stephen Rasku
Dec 4 at 23:48




I would like to sort by time stamp. I’ll make that clearer on my question.
– Stephen Rasku
Dec 4 at 23:48












Yea that makes it more complicated
– Michael Prokopec
Dec 5 at 0:10




Yea that makes it more complicated
– Michael Prokopec
Dec 5 at 0:10










up vote
0
down vote













Would this help:



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort
#1542817796 ack npq /etc
#1542817796 ack npq /usr/share/
#1542817796 ack npq /usr/share/
#1542817796 cd -
#1542817796 cd w/Edge-Agent/edge-agent-ged/
#1542817796 dv
#1542817796 hg diff
#1542817796 make
#1542817796 make
#1542817796 make
#1542817796 make -npq
.
.
.


Make sure to list ALL history files as input stream to awk.



EDIT: Some non-satisfying, non-portable essays to retain the original order of the commands:



awk '/^#[0-9]*$/ TS = $0; next print TS, NR, $0' file* | sort -k1,1 -k2,2n


.



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort -mk1,1 





share|improve this answer






















  • Since this uses the same timestamp for all consecutive untimestamped entries it will sort those entries lexically instead of by the original order in the file. I think this can be improved by incrementing the inferred timestamp somehow.
    – Stephen Rasku
    Dec 5 at 14:23










  • I edited two more options in, none of which is necessarily satisfying nor portable.
    – RudiC
    Dec 5 at 14:32














up vote
0
down vote













Would this help:



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort
#1542817796 ack npq /etc
#1542817796 ack npq /usr/share/
#1542817796 ack npq /usr/share/
#1542817796 cd -
#1542817796 cd w/Edge-Agent/edge-agent-ged/
#1542817796 dv
#1542817796 hg diff
#1542817796 make
#1542817796 make
#1542817796 make
#1542817796 make -npq
.
.
.


Make sure to list ALL history files as input stream to awk.



EDIT: Some non-satisfying, non-portable essays to retain the original order of the commands:



awk '/^#[0-9]*$/ TS = $0; next print TS, NR, $0' file* | sort -k1,1 -k2,2n


.



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort -mk1,1 





share|improve this answer






















  • Since this uses the same timestamp for all consecutive untimestamped entries it will sort those entries lexically instead of by the original order in the file. I think this can be improved by incrementing the inferred timestamp somehow.
    – Stephen Rasku
    Dec 5 at 14:23










  • I edited two more options in, none of which is necessarily satisfying nor portable.
    – RudiC
    Dec 5 at 14:32












up vote
0
down vote










up vote
0
down vote









Would this help:



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort
#1542817796 ack npq /etc
#1542817796 ack npq /usr/share/
#1542817796 ack npq /usr/share/
#1542817796 cd -
#1542817796 cd w/Edge-Agent/edge-agent-ged/
#1542817796 dv
#1542817796 hg diff
#1542817796 make
#1542817796 make
#1542817796 make
#1542817796 make -npq
.
.
.


Make sure to list ALL history files as input stream to awk.



EDIT: Some non-satisfying, non-portable essays to retain the original order of the commands:



awk '/^#[0-9]*$/ TS = $0; next print TS, NR, $0' file* | sort -k1,1 -k2,2n


.



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort -mk1,1 





share|improve this answer














Would this help:



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort
#1542817796 ack npq /etc
#1542817796 ack npq /usr/share/
#1542817796 ack npq /usr/share/
#1542817796 cd -
#1542817796 cd w/Edge-Agent/edge-agent-ged/
#1542817796 dv
#1542817796 hg diff
#1542817796 make
#1542817796 make
#1542817796 make
#1542817796 make -npq
.
.
.


Make sure to list ALL history files as input stream to awk.



EDIT: Some non-satisfying, non-portable essays to retain the original order of the commands:



awk '/^#[0-9]*$/ TS = $0; next print TS, NR, $0' file* | sort -k1,1 -k2,2n


.



awk '/^#[0-9]*$/ TS = $0; next print TS, $0' file* | sort -mk1,1 






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 5 at 14:34

























answered Dec 5 at 14:17









RudiC

3,8891312




3,8891312











  • Since this uses the same timestamp for all consecutive untimestamped entries it will sort those entries lexically instead of by the original order in the file. I think this can be improved by incrementing the inferred timestamp somehow.
    – Stephen Rasku
    Dec 5 at 14:23










  • I edited two more options in, none of which is necessarily satisfying nor portable.
    – RudiC
    Dec 5 at 14:32
















  • Since this uses the same timestamp for all consecutive untimestamped entries it will sort those entries lexically instead of by the original order in the file. I think this can be improved by incrementing the inferred timestamp somehow.
    – Stephen Rasku
    Dec 5 at 14:23










  • I edited two more options in, none of which is necessarily satisfying nor portable.
    – RudiC
    Dec 5 at 14:32















Since this uses the same timestamp for all consecutive untimestamped entries it will sort those entries lexically instead of by the original order in the file. I think this can be improved by incrementing the inferred timestamp somehow.
– Stephen Rasku
Dec 5 at 14:23




Since this uses the same timestamp for all consecutive untimestamped entries it will sort those entries lexically instead of by the original order in the file. I think this can be improved by incrementing the inferred timestamp somehow.
– Stephen Rasku
Dec 5 at 14:23












I edited two more options in, none of which is necessarily satisfying nor portable.
– RudiC
Dec 5 at 14:32




I edited two more options in, none of which is necessarily satisfying nor portable.
– RudiC
Dec 5 at 14:32

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f486022%2fmerge-multiple-bash-history-files-with-timestamps%23new-answer', 'question_page');

);

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






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay