Home > Back-end >  Insert characters before a line that contains numbers
Insert characters before a line that contains numbers

Time:03-25

Given a text file with lines (for example, a file with three sentences, it will be three lines). It is necessary in the lines where there are numbers to add the current time in front of them (lines).

By inserting the current time, I sort of figured it out:

sed "s/^/$(date  %T) /" text.txt

I saw it but it doesn't suit me as it is here used IF

But how can I make the strings also be checked for the presence of digits? But how to check a string for numbers and insert a date before it with one command? It is possible without

if

statement?

CodePudding user response:

You can use a regex to match the lines

sed "/[0-9]/s/^/$(date  %T) /" text.txt
  • Related