Want to unwrap lines in a text file unix best solution needed.

Asked By 30 points N/A Posted on -

Hey there,

I want to unwrap lines in a text file Unix formatted. I've tried several different software but didn't get the expected resulted. So I want the best solution for this.

Answered By 0 points N/A #164007

Want to unwrap lines in a text file unix best solution needed.


Hi Walter,

In order to unwrap lines in your unix text file (input.txt), you need to create the text file : unwordwrap.awk that contains the following :


BEGIN {RS="nn"; FS="n"}

{for (i=1;i<=NF;i++) printf $i" ";  printf "nn" }


Then run it using : awk -f unwordwrap.awk < input.txt


And here after is the explanation of the code in unwordwrap.awk file:

BEGIN {RS="nn"; FS="n"}
Defines the record separator (RS) to be two newlines (this is one newline by default, but we want to grab records that are 'anything between two newlines'), and defines the field separator (FS) to be a single newline (this is usually any run of one or more whitespace characters, but we want a field to be a whole line).

for (i=1;i<=NF;i++) printf $i" "
Runs through each field (a line in our case) in each record (the bits between the blank lines), and prints the field with no newline after it, joining the split lines back up.

printf "nn"
Prints a double newline to get your blank line after each line in the output.


Best Regards.


Login/Register to Answer

Related Questions