With the input and output formatted exactly as currently specified in the question:
tac "$item" | awk -F\" '{ printf "**v%s**\n - %s\n", $2, $4 }' > "/tmp/$item" &&
mv "/tmp/$item" "$item"
Note:
- This assumes that simply reversing the input order is sufficient to get the desired output sorting [since confirmed by the OP] - if true sorting is needed, it gets trickier
(sort -t. -r -b -n -k1.2,1 -k2,2 -k3,3 "$item"
). - On OSX, use
tail -r
instead oftac
.
Also, it's better not to parse command output with for ... in
, as that would break with paths with embedded spaces, for instance.
A more robust solution is to read into an array:
IFS=$'\n' read -r -d '' -a files <<<"$(find . -type f -name README.md)"
for item in "${files[@]}"; do
# ...
done
- The only caveat is that paths mustn't contain embedded newlines, but that's typically not a concern.
- If you're using bash 4+, you can use
readarray -t files <<<"$(find . -type f -name README.md)"
instead.
Alternatively, process find
's output directly in a loop:
find . -type f -name README.md -print0 | while IFS= read -r -d '' item; do ...