1

I have a text file, which has an inconsistent timestamp format, that I would like to standardize. This is in a transcript from an interview; ultimately for textual analysis.

What command could I run 3x for each set of lines, through Linux terminal?

(1st) 00:0 before each line 1 thru 128 e.g. 0:32.6 becomes 00:00:32.6

(2nd) 00: before each line 129 thru 713 e.g. 10:02.6 becomes 00:10:02.6

(3rd) 0 before each line 714 thru 2285 e.g. 1:00:07.0 becomes 01:00:07.0

I'm aware I could split the file into 3, and add a different prefix to all of the lines of each file; but I need to execute numerous commands on the file, and constantly spliting and merging files would waste time.

The following articles:

https://unix.stackexchange.com/questions/336768/insert-character-in-the-beginning-of-specified-line-in-file

https://baeldung.com/linux/file-insert-prefix-every-line

I want to edit a specific lines (multiple) with sed command

Either handle only specific lines, or all lines; and I want to edit a specific series of lines

The best I could come up with:

$ sed "$(print -f '%ds/./00:0&/g;' 1 [thru] 128)" filename

But I don't know how the [thru] functionality would be included.

1 Answer 1

3

You can use a range specification start,end before a command to restrict it to those line numbers. And you can use multiple -e options to execute different commands for different ranges.

sed -e '1,128s/^/00:0/' -e '129,713s/^/00:/' -e '714,2285s/^/0/' filename

There's no need to use the g option in your substitute commands, since ^ can only match once on a line.

Sign up to request clarification or add additional context in comments.

1 Comment

You should accept the answer by clicking on the green checkmark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.