Prepend date and time to each output line #82

Closed
opened 2025-06-06 16:57:02 +02:00 by Benjamin_Loison · 5 comments

I remember having already investigated this previously but do not find any mention in this repository issues, Benjamin_Loison/Debian/issues and Benjamin-Loison/cinnamon/issues when searching line, lines and prepend.

I remember having already investigated this previously but do not find any mention in this repository issues, [Benjamin_Loison/Debian/issues](https://codeberg.org/Benjamin_Loison/Debian/issues) and [Benjamin-Loison/cinnamon/issues](https://github.com/Benjamin-Loison/cinnamon/issues) when searching *line*, *lines* and *prepend*.
Author
Owner
Bash script:
counter=0
while true
do
    echo $counter
    sleep 1
    ((counter++))
done

for witness test.

<details> <summary>Bash script:</summary> ```bash counter=0 while true do echo $counter sleep 1 ((counter++)) done ``` </details> for witness test.
Author
Owner

DuckDuckGo and Google search Linux prepend string to stdout lines.

DuckDuckGo and Google search *Linux prepend string to stdout lines*.
Author
Owner

The Super User answer 868517:

Bash script:
IFS=
(
counter=0
while true
do
    echo $counter
    sleep 1
    ((counter++))
done
) | { x=1; while IFS= read -d'' -s -N 1 char; do
  [ $x ] && printf "Remote: "
  printf "$char"
  unset x
  [ "$char" == "
" ] && x=1
done; }
Output:
Remote: 0
Remote: 1
Remote: 2
...
[The Super User answer 868517](https://superuser.com/a/868517): <details> <summary>Bash script:</summary> ```bash IFS= ( counter=0 while true do echo $counter sleep 1 ((counter++)) done ) | { x=1; while IFS= read -d'' -s -N 1 char; do [ $x ] && printf "Remote: " printf "$char" unset x [ "$char" == " " ] && x=1 done; } ``` </details> <details> <summary>Output:</summary> ``` Remote: 0 Remote: 1 Remote: 2 ... ``` </details>
Author
Owner
Bash script:
IFS=
(
counter=0
while true
do
    echo $counter
    sleep 1
    ((counter++))
done
) | { x=1; while IFS= read -d'' -s -N 1 char; do
  [ $x ] && date | head -c -1 && echo -n ': '
  printf "$char"
  unset x
  [ "$char" == "
" ] && x=1
done; }
Output:
Fri Jun  6 05:05:14 PM CEST 2025: 0
Fri Jun  6 05:05:15 PM CEST 2025: 1
Fri Jun  6 05:05:16 PM CEST 2025: 2
...
<details> <summary>Bash script:</summary> ```bash IFS= ( counter=0 while true do echo $counter sleep 1 ((counter++)) done ) | { x=1; while IFS= read -d'' -s -N 1 char; do [ $x ] && date | head -c -1 && echo -n ': ' printf "$char" unset x [ "$char" == " " ] && x=1 done; } ``` </details> <details> <summary>Output:</summary> ``` Fri Jun 6 05:05:14 PM CEST 2025: 0 Fri Jun 6 05:05:15 PM CEST 2025: 1 Fri Jun 6 05:05:16 PM CEST 2025: 2 ... ``` </details>
Author
Owner
It helps [termux/termux-api-package/issues/137](https://github.com/termux/termux-api-package/issues/137).
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Benjamin_Loison/linux#82
No description provided.