Search code examples
linuxawkcommand

Formatting file using AWK because the file to large to edit


Formatting file using AWK, because the text is too large to be opened, and it crashes the editor when edited.

File a

1
:23
1
:27
1:67

To output:

1:23
1:27
1:67

How can I do the formatting using AWK to do the formatting like this output. Because a program like Notepad++ and others can't open the file.


Solution

  • I would harness GNU AWK for this task following way, let file.txt content be

    1
    :23
    1
    :27
    1:67
    

    then

    awk '{ORS=/:/?"\n":"";print}' file.txt
    

    gives output

    1:23
    1:27
    1:67
    

    Explanation: I set output-row-separator (ORS) to newline if it contains : and empty line otherwise, then print line, therefore all lines which do not have : before line with : and line with : will be concatenated.

    (tested in GNU Awk 5.1.0)