4

I have the following command that I'm trying to tweak:

 stat -c %y test.txt | sed 's/^\([0-9\-]*\)/\1/'

This returns the following information:

 2016-08-03 14:52:24.000000000

I need to tweak this so that it excludes the ".000000000". I've tried a few different options but can't seem to get it right. Ultimately, I need to take the time stamp that I extract from the above command, and echo it as the first line into the file.

I have this command as an example that seems to correctly add a new line to the top of the file:

   sed -i '1s/^/this should be a date\n/' test.txt

How do I combine the two commands?

4
sed -i "1i$(stat -c %y test.txt | sed -r 's/\.[0-9]+ / /')" test.txt
| improve this answer | |
  • Florian your example includes the trailing 00000 in the timestamp – Happydevdays Aug 3 '16 at 15:39
  • @Happydevdays: I get something like 2016-08-03 17:41:42 +0200. Maybe it's caused by the locales. Try sed -r 's/\.[0-9]+//' – Florian Diesch Aug 3 '16 at 15:47
5

Like so:

( stat -c %y test_file.txt | awk -F '.' '{print $1}' ; cat test_file.txt  ) > /tmp/temp_file && mv /tmp/temp_file test_file.txt

Here's a small demo:

$> echo "Hello World" > test_file.txt
$> ( stat -c %y test_file.txt | awk -F '.' '{print $1}' ; cat test_file.txt  ) > /tmp/temp_file &&                       
> mv /tmp/temp_file test_file.txt
$> cat test_file.txt                                                                                                     
2016-08-03 09:24:27
Hello World

Explanation:

  • We get timestamp and file contents in subshell , signified by parenthesis (...)
  • stat -c %y test_file.txt | awk -F '.' '{print $1}' trimps timestamp
  • cat test_file.txt reads file contents.
  • The resulting text is timestamp plus whatever was in file, is being redirected to /tmp/temp_file
  • Finally we replace original file with contents of /tmp/temp_file
| improve this answer | |
  • Thanks so much for this helpful post. I accepted the other answer only because it's a little more concise. – Happydevdays Aug 3 '16 at 17:06
4

How about using ed instead of sed?

$ ed -s test.txt << EOF
0r !stat -c \%y %
1 s/\.[0-9]*//
w
q
EOF

This says:

  • read the output of command stat -c \%y % and insert it at position 0 in the buffer i.e. before the first line. Note that in ed, an unescaped % is shorthand for the current file.
  • on this (now first) line, substitute nothing in place of the decimal period and following digits
  • write the result back to the file and quit

The -s is optional - it just prevents ed from outputing line counts. If stat appends a timezone offset in your locale / timezone, and you wish to remove that as well, then you can modify the s command to remove the period and everything following it e.g. s/\..*//

It's also possible to use ed in a one-liner taking its commands from standard input using echo or printf, although it's a little tricky to get the escaping right:

printf '0r !stat -c \%%y %%\n1 s/\.[0-9]*//\nw\nq' | ed -s test.txt
| 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.