Unix Scripting : Sort a Portion of a File and not the complete file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Scripting : Sort a Portion of a File and not the complete file
# 1  
Old 08-02-2012
Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order.
Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted.

Currently All_users.txt contains the following lines.

Code:
##############
# ARS USERS
##############
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (11)
paul, Paul Smith, Stevn Smiley, REQ000001, ARS (11)
sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11)
adam, Adam smils, Paul Reily, REQ000001, ARS (11)
#
##############
# ATRIUM USERS
##############
randy, Randy Roch, Steven Seagul, REQ000002, ATRIUM (11)
michelle, Michelle Morques, Steven Seagul, REQ000003, ATRIUM (11)
lois, Lois Hill, Randy Rocker, Steven Seagul, REQ000004, ATRIUM (11)
#
##############
# INSTALLER USERS
##############
nguyen, Nguyen Le, Steven Seagul, REQ000001, INSTALLER (13)
mark, Mark Goodwan, Steven Seagul, REQ000005, INSTALLER (13)

I need the output to be sorted like this.

Code:
##############
 # ARS USERS
 ##############
adam, Adam smils, Paul Reily, REQ000001, ARS (11)
 mike, Mike Conway, Stevn Smiley, REQ000001, ARS (11)
 paul, Paul Smith, Stevn Smiley, REQ000001, ARS (11)
 sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11)
 #
 ##############
 # ATRIUM USERS
 ##############
 randy, Randy Roch, Steven Seagul, REQ000002, ATRIUM (11)
 michelle, Michelle Morques, Steven Seagul, REQ000003, ATRIUM (11)
 lois, Lois Hill, Randy Rocker, Steven Seagul, REQ000004, ATRIUM (11)
 #
 ##############
 # INSTALLER USERS
 ##############
 nguyen, Nguyen Le, Steven Seagul, REQ000001, INSTALLER (13)
 mark, Mark Goodwan, Steven Seagul, REQ000005, INSTALLER (13)

Thanks in advance
# 2  
Old 08-02-2012
sort <filename>
Your issue looks simple.
Is it something you are looking for?
# 3  
Old 08-02-2012
It's not so simple, because he doesn't want to sort the comments along with everything else... I was thinking of splitting the file into n parts for separate sorting based on comments or blanks, but was hoping there was a more straightforward way which eluded me.
# 4  
Old 08-02-2012
@bobbygsk, thanks for your reply.

No, its not that simple as we look like. Sorting the file completely re-orders all the lines.
I need to sort only the portion of the file and not the complete file. My file has 3500 lines and i need to sort only specific lines.

Sort <filename> doesnt provide the result as expected.

Code:
root@bmcpunscm-lnx-test: /tmp-> sort < users_all.txt > users_all.txt1
root@bmcpunscm-lnx-test: /tmp-> cat users_all.txt1
#
#
##############
##############
##############
##############
##############
##############
adam, Adam smils, Paul Reily, REQ000001, ARS (11)
# ARS USERS
# ATRIUM USERS
# INSTALLER USERS
lois, Lois Hill, Randy Rocker, Steven Seagul, REQ000004, ATRIUM (11)
mark, Mark Goodwan, Steven Seagul, REQ000005, INSTALLER (13)
michelle, Michelle Morques, Steven Seagul, REQ000003, ATRIUM (11)
mike, Mike Conway, Stevn Smiley, REQ000001, ARS (11)
nguyen, Nguyen Le, Steven Seagul, REQ000001, INSTALLER (13)
paul, Paul Smith, Stevn Smiley, REQ000001, ARS (11)
randy, Randy Roch, Steven Seagul, REQ000002, ATRIUM (11)
sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11)

# 5  
Old 08-02-2012
This should work:
Code:
awk ' BEGIN     { sorting=0 }
/^#/    { if (sorting) {
                close("sort")
                sorting=0
          }     
          print 
          next  
        }
        { print | "sort"
          sorting=1
        }
END     { if (sorting) close("sort") }' users_all.txt

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-03-2012
Thanks a tonnnn Don and your script works Great.
Can you please explain the flow of the script so that i can understand the flow.
# 7  
Old 08-03-2012
The command
Code:
print | "sort"

opens up a pipe to a sort command (if a sort command isn't already running) and writes the current input line to it.

The command
Code:
close("sort")

closes the pipe and flushes the output from the sort command to standard output.
This is done whenever a pipe to sort is open and either a line starting with "#" is found or the end of the input is found.

So, any line starting with "#" is written directly to standard output by the awk script and any set of lines between lines starting with "#" are piped through sort before being written to standard output.
This User Gave Thanks to Don Cragun 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

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

3. Shell Programming and Scripting

How to find complete file names in UNIX if i know only extention of file

Suppose I have a file which contains other file names with some extention . text file containt gdsds sd8ef g/f/temp_temp.sum yyeta t/unix.sum ghfp hrwer h/y/test.text.dat if then.... I want to get the complete file names, like for above file I should get output as temp_temp.sum... (4 Replies)
Discussion started by: panchal
4 Replies

4. Shell Programming and Scripting

How to search and append words in the same file using unix scripting file operations

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958(which is hostname) only,... (5 Replies)
Discussion started by: gsreeni
5 Replies

5. UNIX for Dummies Questions & Answers

Unable to sort file name in Unix

Hi, I'm unable to sort the files with the required names on Unix. Details have been enclosed:- Below are the fle name inside a folder, now i need to sort them in a way that they are group with the seconds occurance in the file name (E.g 01954,07947,06794) 01.01954.MXRS1.rcv... (3 Replies)
Discussion started by: kkkk_ssss
3 Replies

6. Shell Programming and Scripting

UNix one-liner to sort a file

Hi everyone, I have a txt file as: >001.b1 GCTAGTGCTAGCTAGCTAGCATCGATCGAT >002.b1 CAGTCAGTCGTAGTGCTAGCTGATGCTCGT >003.b1 CGATCGTAGTCGTATCGATGCTGACGTAGG >002.g1 ATGCTGATCGACTAGCTAGTCGT >015.b1 CGATCTAGTAGTGCTAGTCGTTT >001.g1 ATGCTGATCGACTAGCTAGTCGT >003.g1 CGATGCTAGTCGATGCTGACGGG (3 Replies)
Discussion started by: ad23
3 Replies

7. Post Here to Contact Site Administrators and Moderators

Where can I download the VTC - Unix Shell Scripting Advanced complete video

Where can I download the VTC - Unix Shell Scripting Advanced complete video. I don't know in which thread I should post this question.Plz help me out, or just tell me the link in the reply to this post. Thanks in advance. (0 Replies)
Discussion started by: villain41
0 Replies

8. UNIX for Dummies Questions & Answers

how to find complete path of a file in unix

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to capture... (5 Replies)
Discussion started by: yahoo!
5 Replies

9. UNIX for Advanced & Expert Users

how to find complete path of a file in unix

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to capture... (1 Reply)
Discussion started by: yahoo!
1 Replies

10. Shell Programming and Scripting

Separate a portion of text file into another file

Hi, I have my input as follows : I have given two entries- From system Mon Aug 1 23:52:47 2005 Source !100000006!: Impact !100000005!: High Status ! 7!: New Last Name+!100000001!: First Name+ !100000003!: ... (4 Replies)
Discussion started by: srikanth_ksv
4 Replies
Login or Register to Ask a Question