0

/home/digadm02/.bash_history:#1520325239 /home/digadm02/.bash_history:sudo su

I have these kind of lines in file test.txt. I have to search the pattern of "#1520325239" and replace them with the value which I get from the command 'date -d @1520325239'. there are many lines like this. I have to read each line and replace it in the same file.

  • Just out of curiosity are you trying to clean your own mess from history? – Sagar Apr 5 '18 at 7:42
0

If I understand you well, the timestamp isn't just one, but many on similar lines. This script should do replacement you want:

#!/bin/bash

# get timestamps from the file
dates=`sed -e '1,$s/^.*#\(.*\) \/.*/\1/' /tmp/test.txt`

for date in $dates
do
    # get human readable format from timestamp
    newdate=`date -d @${date}`
    # replace timestamp with human readable date
    sed -i "1,\$s/\#$date/$newdate/" /tmp/test.txt
done
| improve this answer | |
0

You can use below command as per your need

sed -i s/#1520325239/$(date -d @1520325239)/g .bash_history
| improve this answer | |
  • could you try to provide a bit more explantation on this expression ? – Kiwy Apr 5 '18 at 8:15
0

Use sed sed -i "/#1520325239/c$(date -d @1520325239)" test.txt

-i will modify the file in place. Omit the "i" to test your results first.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.