Skip to content

Instantly share code, notes, and snippets.

@ychaouche
Created September 16, 2025 15:11
Show Gist options
  • Save ychaouche/bcf371c67d4db33d7d6e7c57ec658b83 to your computer and use it in GitHub Desktop.
Save ychaouche/bcf371c67d4db33d7d6e7c57ec658b83 to your computer and use it in GitHub Desktop.
txt.replace
Usage:
txt.replace -f fieldnumber -c command
Example:
Change the formatting of numbers in the output of ls
16:09:24 ~/MUSIQUE/CONTENT/RAM -1- $ command ls -l
total 73576
-rw-r--r-- 1 ychaouche ychaouche 5443123 Mar 9 2025 10.mp3
-rw-r--r-- 1 ychaouche ychaouche 4434170 Mar 9 2025 11.mp3
-rw-r--r-- 1 ychaouche ychaouche 4002418 Mar 9 2025 12.mp3
-rw-r--r-- 1 ychaouche ychaouche 6070898 Mar 9 2025 13.mp3
-rw-r--r-- 1 ychaouche ychaouche 4231041 Mar 9 2025 14.mp3
-rw-r--r-- 1 ychaouche ychaouche 4321738 Mar 9 2025 1.mp3
-rw-r--r-- 1 ychaouche ychaouche 5140102 Mar 9 2025 2.mp3
-rw-r--r-- 1 ychaouche ychaouche 8678962 Mar 9 2025 3.mp3
-rw-r--r-- 1 ychaouche ychaouche 3603266 Mar 9 2025 4.mp3
-rw-r--r-- 1 ychaouche ychaouche 5330273 Mar 9 2025 5.mp3
-rw-r--r-- 1 ychaouche ychaouche 5603618 Mar 9 2025 6.mp3
-rw-r--r-- 1 ychaouche ychaouche 7955893 Mar 9 2025 7.mp3
-rw-r--r-- 1 ychaouche ychaouche 5852304 Mar 9 2025 8.mp3
-rw-r--r-- 1 ychaouche ychaouche 4603442 Mar 9 2025 9.mp3
16:09:30 ~/MUSIQUE/CONTENT/RAM -1- $
16:09:59 ~/MUSIQUE/CONTENT/RAM -1- $ command ls -l | ~/CODE/TEST/BASH/txt.replace -f 5 -c "numfmt --group \$5"
total 73576
-rw-r--r-- 1 ychaouche ychaouche 5,443,123 Mar 9 2025 10.mp3
-rw-r--r-- 1 ychaouche ychaouche 4,434,170 Mar 9 2025 11.mp3
-rw-r--r-- 1 ychaouche ychaouche 4,002,418 Mar 9 2025 12.mp3
-rw-r--r-- 1 ychaouche ychaouche 6,070,898 Mar 9 2025 13.mp3
-rw-r--r-- 1 ychaouche ychaouche 4,231,041 Mar 9 2025 14.mp3
-rw-r--r-- 1 ychaouche ychaouche 4,321,738 Mar 9 2025 1.mp3
-rw-r--r-- 1 ychaouche ychaouche 5,140,102 Mar 9 2025 2.mp3
-rw-r--r-- 1 ychaouche ychaouche 8,678,962 Mar 9 2025 3.mp3
-rw-r--r-- 1 ychaouche ychaouche 3,603,266 Mar 9 2025 4.mp3
-rw-r--r-- 1 ychaouche ychaouche 5,330,273 Mar 9 2025 5.mp3
-rw-r--r-- 1 ychaouche ychaouche 5,603,618 Mar 9 2025 6.mp3
-rw-r--r-- 1 ychaouche ychaouche 7,955,893 Mar 9 2025 7.mp3
-rw-r--r-- 1 ychaouche ychaouche 5,852,304 Mar 9 2025 8.mp3
-rw-r--r-- 1 ychaouche ychaouche 4,603,442 Mar 9 2025 9.mp3
16:10:08 ~/MUSIQUE/CONTENT/RAM -1- $
#!/bin/bash
# txt.replace : Wrapper Bash pour appeler un script awk qui remplace un champ par le résultat d'une commande
usage() {
echo "Usage: $0 -f <field> -c <command>"
echo " -f <field> : Numéro du champ à remplacer (1-based)"
echo " -c <command> : Commande à exécuter, peut contenir \$<field> pour référencer le champ"
exit 1
}
# Vérification des arguments
while getopts "f:c:" opt; do
case $opt in
f) field="$OPTARG" ;;
c) command="$OPTARG" ;;
*) usage ;;
esac
done
# Vérification que les arguments requis sont fournis
if [ -z "$field" ] || [ -z "$command" ]; then
usage
fi
# Vérification que le champ est un nombre
if ! [[ "$field" =~ ^[0-9]+$ ]]; then
echo "Erreur : le champ doit être un nombre"
exit 1
fi
# Appel du script awk avec les variables field et command
awk -f ~/.lib/lib.awk -v field="$field" -v command="$command" -e '
{
# Si le champ spécifié existe
if (NF >= field) {
# Remplacer $<field> par la valeur du champ dans la commande
cmd = command
gsub("\\$" field, $field, cmd)
# Exécuter la commande via exec_command
result = exec_command(cmd " </dev/null")
# Remplacer le champ par le résultat si différent de "-"
if (result != "-") {
$field = result
}
}
# Afficher la ligne (modifiée ou non)
print
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment