Need help with sorting in paragraphs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with sorting in paragraphs
# 1  
Old 09-12-2012
Data Need help with sorting in paragraphs

I am very new to shell scripting, current try to do a sorting of a text file in paragraphs with ksh script.

example:

File content:

A1100001 line 1 = "testing"
line 2 = something,
line 3 = 100

D1200003 line 1 = "testing"
line 2 = something,
line 3 = 100

B1200003 line 1 = "testing"
line 2 = something,
line 3 = 100

i want the output to be like:

output:

A1100001 line 1 = "testing"
line 2 = something,
line 3 = 100

B1200003 line 1 = "testing"
line 2 = something,
line 3 = 100

D1200003 line 1 = "testing"
line 2 = something,
line 3 = 100

i try use sort but it will sort every line, instead of the 3 lines together

Any advise on how to do a sort by the "A1100001" yet move the 3 lines togther?
# 2  
Old 09-12-2012
Not the best way.. but it will do what you want...Smilie

Code:
awk '{if($0 != ""){ if(s){s=s"*"$0}else{s=$0}}else{ print s"*";s=""}}END{print s"*"}' file | sort | tr "*" '\n'

# 3  
Old 09-12-2012
Quote:
Originally Posted by pamu
Not the best way.. but it will do what you want...Smilie

Code:
awk '{if($0 != ""){ if(s){s=s"*"$0}else{s=$0}}else{ print s"*";s=""}}END{print s"*"}' file | sort | tr "*" '\n'

Thanks this works, do u mind explaining to me what it means?

By the way i have a dumb question to ask, after doing this, how can i save it in the original file?

Sorry for asking dumb question, i am quite new here, started on this only for a week, really thanks alot Smilie
# 4  
Old 09-12-2012
Quote:
Originally Posted by gavin_L
how can i save it in the original file?
To save in original file do like this...
Code:
awk '{if($0 != ""){ if(s){s=s"*"$0}else{s=$0}}else{ print s"*";s=""}}END{print s"*"}' file | sort | tr "*" '\n' > temp_file
mv temp_file file

here in script first i check for the blank line..with if($0 != "").
then check if the variable s present or not. and append next line to the previous line separated by *

Check the output of below command.
Code:
awk '{if($0 != ""){ if(s){s=s"*"$0}else{s=$0}}else{ print s"*";s=""}}END{print s"*"}' file

then run the sort command to get the output as we want it.

then * should be replaced with new line \n to get desired output.

hope it clears you doubt...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract paragraphs and count them

Hi, I have a text with a number of paragraphs in them. My problem is I need to locate certain errors/warning and extract/count them. Problem is I do not know how many paras are there with that particular type of error/warning. I had thought that somehow if I could count the number of... (25 Replies)
Discussion started by: dsid
25 Replies

2. Shell Programming and Scripting

Formatting paragraphs with fmt with rule

Hi, i know that i can format a whole file using fmt -w 78 file > file_new Now, i would like to implement a rule in the following way: - If a paragraph starts with % (and ends with %), the paragraph should remain unchanged. - Otherwise the command stated above should be applied to the... (4 Replies)
Discussion started by: johnlocke85
4 Replies

3. Shell Programming and Scripting

Split text into paragraphs

Hi all! I want to make a code to split sentences into paragraphs maybe 4-5 sentences into one <p>text</p> there are no new lines in the text string any ideas with AWK, SSH? Thank you! (5 Replies)
Discussion started by: sanantonio7777
5 Replies

4. Shell Programming and Scripting

Extract paragraphs under conditions

Hi all, I want to extract some paragraphs out of a file under certain conditions. - The paragraph must start with 'fmri' - The paragraph must contain the string 'restarter svc:/system/svc/restarter:default' My input is like that : fmri svc:/system/vxpbx:default state_time Wed... (4 Replies)
Discussion started by: Armoric
4 Replies

5. Shell Programming and Scripting

file separated into paragraphs or pages

hi, i have file, file is separated into parahgraphs by these line(----------). i want to find out logId = string : "AIALARM", in each parahgraph or page if found then i want to cut next five lines.... ... (3 Replies)
Discussion started by: dodasajan
3 Replies

6. Shell Programming and Scripting

fetching paragraphs with SED

hi, i am a SED newbie and i need some help. i have a log file as shown below. and i want to search specific Error Code, and fetch the whole paragraph. ... ... ................. ....ErrCode... ................. ... ... ... ................. ....ErrCode... ... (4 Replies)
Discussion started by: ipat
4 Replies

7. Shell Programming and Scripting

removing certain paragraphs for matching patterns

Hi, I have a log file which might have certain paragraphs. Switch not possible Error code 1234 Process number 678 Log not available Error code 567 Process number 874 ..... ...... ...... Now I create an exception file like this. cat text.exp Error code 1234 Process number 874 (7 Replies)
Discussion started by: kaushys
7 Replies

8. Shell Programming and Scripting

how to filter out some paragraphs in a file

Hi, I am trying to filter out those paragraphs that contains 'CONNECT', 'alter system switch logfile'. That means say the input file is : ------------------------------------------------------- Wed Jun 7 00:32:31 2006 ACTION : 'CONNECT' CLIENT USER: prdadm CLIENT TERMINAL: Wed Jun 7... (7 Replies)
Discussion started by: cnlhap
7 Replies

9. UNIX for Dummies Questions & Answers

Using sed to remove paragraphs with variables

Hi everyone, I have a file with multiple entries and I would like to remove the ones that contain either /A"> or /A/, where A can be any letter of the alphabet. Here's an example of the entries: <Topic r:id="Top/World/Fran"> <catid>476</catid> <link... (1 Reply)
Discussion started by: BlueberryPickle
1 Replies

10. Shell Programming and Scripting

how to sort paragraphs by date within a file

hi all i want help in sortng date in paragraphs within file , i want to ask as if there any option to sort a certain pattern of file not the rest of file.i.e the data of file become sorted with respect to date i have a log file as follows !! *A0628/081 /08-01-10/13 H... (1 Reply)
Discussion started by: nabmufti
1 Replies
Login or Register to Ask a Question