Sorting content between match pattern and move on with awk and sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting content between match pattern and move on with awk and sed
# 8  
Old 07-25-2014
This is specific to the specific input sample you gave; nevertheless it should work on other structures after some adaption.
Code:
awk     'NF==1  {print | "sort"}        # one field: record member; add to sort input via pipe; sort will print to stdout when pipe is closed.
         NF!=1  {close ("sort"); print} # record header or empty line; close the sort pipe to finish sorting the record; print header to stdou
        ' file


Last edited by RudiC; 07-25-2014 at 12:56 PM..
# 9  
Old 07-25-2014
what is the content is not one field?
example
Code:
S 0.0 0.0 (reg, inst050)
kfdasdf dsaf
easd sadfas 
ffadsfasfdsadsa  dsafdsafas
dd8385fdsa asdasdfaf2432vdsaf 
dsafcfasfdsa f32f32asf322dsdsaf


Last edited by radoulov; 07-25-2014 at 12:38 PM..
# 10  
Old 07-25-2014
If there are only whitespace characters on a line, the number of fields is 0. Otherwise, the number of fields is the number of sequences of non-whitespace characters separated by sequences of whitespace characters.

This is why it is so important to give us representative sample input for the problem you want to solve...

Still making some wild assumptions based on the sample input you have shown us, try:
Code:
awk '
!NF || /^S .*[(].*[)]$/ {
	# Close pipe to sort when we get a blank line or a section header.
	close("sort")
	# Copy the blank or section header line to standard output.
	print
	next
}
{	# Feed other lines to sort.
	print | "sort"
}' file


Last edited by Don Cragun; 07-25-2014 at 11:40 AM.. Reason: Add missing 1st line...
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 07-25-2014
thanks Don Cragun.
you just give me hint on how i should proceed with my several cases and i just made it. thanks.

thanks to RudiC on the example. i have fun with it.

now i can produce what i want.. it save me a lot of efforts. thanks guys.
# 12  
Old 07-25-2014
Quote:
Originally Posted by ctphua
what is the content is not one field?
example
Code:
S 0.0 0.0 (reg, inst050)
kfdasdf dsaf
easd sadfas 
ffadsfasfdsadsa  dsafdsafas
dd8385fdsa asdasdfaf2432vdsaf 
dsafcfasfdsa f32f32asf322dsdsaf

Whenever you can distinguish record headers/separators from the record data, that code snippet will work. Don Cragun refined the conditions from your very specific sample to a more general one; for other data structures you need to come up with your own, new conditions...
This User Gave Thanks to RudiC For This Post:
# 13  
Old 07-25-2014
Just for your info:
with GNU awk 4+ you don't need an external program for sorting:

Code:
awk 'BEGIN {
  # scan arrays by values ASCIIbetical order  
  PROCINFO["sorted_in"] = "@val_str_asc"
  }
{
  # split $0 by NL
  n = split($0, t, "\n")
  print t[1]; delete t[1]
  # output in order
  for (e in t)
    print t[e]
  # print the logical RS    
  print ""    
  }' RS= infile

With Perl:
Code:
perl -F'\n' -lan00e'
  print join "\n", shift @F, (sort @F), $/
   ' infile

This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

2. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

3. Shell Programming and Scripting

Pattern match with awk/sed - help

I need to grep for the pattern text inside the square brackets which are in red and not in green..my current code greps patterns both of them, which i don't want Input fileref|XP_002371341.1| oxoacyl-ACP reductase, putative gb|EPT24759.1| 3-ketoacyl-(acyl-carrier-protein) reductase ... (2 Replies)
Discussion started by: selvankj
2 Replies

4. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

5. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

6. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

Match a pattern starting with sub-pattern using sed

Hi all, I've been experiencing a difficulty trying to match a number and write it to a new file. My input file is: input.txt It contains the lines: 103P 123587.256971 3.21472112 3.1517423 1.05897234566427 58.2146258 12.35478 25.3612489 What would be the sed command to... (17 Replies)
Discussion started by: Biederman
17 Replies

9. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies

10. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies
Login or Register to Ask a Question