top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Sorting paragraphs of text files in Linux?

+1 vote
825 views

I have text files made of paragraphs of text, separated by blank lines.

Each "paragraph" is information about a different item.

In need to sort these paragraphs based on the first line of each paragraph.

Need some hints how to accomplish this.

posted Jun 29, 2015 by Daler

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

I would just concatenate lines until the blank is reached then write out the concatenated line. The result can then be sorted.
If you want to revert the result to paragraphs, just reverse the process outputting lines of up to N characters ending in a space.

answer Jun 29, 2015 by Kaushik
Adding a "line separator" to each input line is a trivial extension to simply  concatenating.

The number of input files is irrelevant; just loop through them all as awk/gawk inputs and combine the outputs using >> and then sort.

If you also want to recreate the original file structure at the end, you could also add a "file name separator" and a file name to the end of each concatenated line.

Feed the sorted output back into awk/gawk to rebuild the files but that, of course, destroys the sort sequence.
Similar Questions
0 votes

The problem is that some of the fields contain commas, but they are inside double quotes.

Example:

sort -t, -k1,1 -k3,3 -k2,2 SomeFile.csv > OutputFile.csv

A line could look something like this:
This is the first field,"This is, well, the second field",The third field could look like this

That line has three fields:
1: This is the first field
2: "This is, well, the second field"
3: The third field could look like this

But sort consider it to have five fields:
1: This is the first field
2: "This is
3: well
4: the second field
5: The third field could look like this

How would you solve this?

+2 votes

I have a huge code base and it is showing files with zero bytes .
Can someone share a linux command to delete files with zero bytes in one attempt ?

+8 votes

How to make of use of GNUPLOT in Linux for plotting graphs by taking input from files? Data in one file represents X-axis and other file data represents Y-axis. Can anyone help me out?

+1 vote
...