Rolf
Rolf

Reputation: 5743

Convert substring through command

Basically, how do I make a string substitution in which the substituted string is transformed by an external command?

For example, given the line 5aaecdab287c90c50da70455de03fd1e ./2015/01/26/GOPR0083.MP4, how to pipe the second part of the line (./2015/01/26/GOPR0083.MP4) to command xargs stat -c %.6Y and then replace it with the result so that we end up with 5aaecdab287c90c50da70455de03fd1e 1422296624.010000?

This can be done with a script, however a one-liner would be nice.

Upvotes: 0

Views: 71

Answers (3)

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10123

A one-liner using GNU sed, which will process the whole file:

sed -E "s/([[:xdigit:]]+) +(.*)/stat -c '\1 %.6Y' '\2'/e" file

or, using plain bash

while read -r hash pathname; do stat -c "$hash %.6Y" "$pathname"; done < file

Upvotes: 1

Milag
Milag

Reputation: 1986

#!/bin/bash

hashtime()
{
    while read longhex fname; do
        echo "$longhex $(stat -c %.6Y "$fname")"
    done
}

if [ $# -ne 1 ]; then
    echo Usage: ${0##*/} infile 1>&2
    exit 1
fi

hashtime < $1

exit 0

# one liner
awk 'BEGIN { args="stat -c %.6Y " } { printf "%s ", $1; cmd=args $2; system(cmd); }' infile

Upvotes: 2

KamilCuk
KamilCuk

Reputation: 140960

It's typical to use awk sed cut to reformat input. For example:

line="5aaecdab287c90c50da70455de03fd1e ./2015/01/26/GOPR0083.MP4"
echo "$line" |
cut -d' ' -f2- | 
xargs stat -c %.6Y

Upvotes: 0

Related Questions