Notice we have to escape the dot character when we replace “.
#Ubuntu large text file editor how to#
Let’s see how to fix original.js and write the result to fixed.js: $ tr " \n" "\n " fixed.js fliter(” string, and it is a character usually present in a JavaScript file, so it should produce short lines. filter(” in our target file, we can choose the character “ ” to split the lines. Finally, we can write our script to replace a string in a file with very long lines. So far, we saw how to split long lines, replace a string, and rejoin the lines. Let’s see how to run sed to replace the string “Alan Turing” with “Alan Mathison Turing”: $ echo "Alan Turing was born in London." | sed 's/Alan Turing/Alan Mathison Turing/' Let’s use tr “ \n” “\n ” to split the line, and then tr “\n ” “ \n” to rejoin it:Īfter splitting the input into several lines, we can run sed to replace the string. This can be easily done by swapping the tr parameters to produce the inverse replacement. This way, tr will change the first character from the first parameter ( ) with the first character from the second parameter ( \n), and also the second character from the first parameter ( \n) with the second character from the second parameter ( ).Īs we are adding new lines to the input, we’ll then rejoin the lines, so the output is consistent with the input.
Doing this will help to preserve the original newline characters. In case there are newlines in our file, we should change both “ ” with “\n” and “\n” with “ ”. Let’s see how to replace “ ” with “\n”: $ echo "line one line two" | tr " " "\n" It will replace the first parameter with the second parameter. tr processes one character at a time, meaning it can handle large files with large lines without issue. To split the one-line into several lines, we can use the program tr to substitute a character with “\n”. Also, the input file has to contain the character and produce relatively small lines when we replace it with “\n”. We have to choose a character that is not in the string we want to replace. In our case, we’ll be replacing another character with “\n” and feed sed with this new input.
In Linux, by default, lines are separated with the newline character “\n”.