sort with condition and insert blank line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort with condition and insert blank line
# 8  
Old 05-30-2011
THis is my output, and it works fine with SunOS. I have also mentioned the gawk version here.

Can you check what version you are running with. If you can do it with awk, may we will try using script.
Code:
user@host> (/home/user) $   gawk '{
> if($0~/^\[/)
> {
> n =asort(arr)
> for(i=1;i<=n;i++)
> printf "%s\n",arr[i]
> delete arr
> if(temp!=""){print temp}
> print $0
> temp=""
>
> }
> else if($0!~/^\#/)
> {
> arr[substr($0,0,3)]=arr[substr($0,0,3)]$0"\n ";
> }
> else
> {
> temp=$0
> }
> }
>
END{n =asort(arr);for(i=1;i<=n;i++){printf "%s\n",arr[i] };print temp}' test.txt> END{n =asort(arr);for(i=1;i<=n;i++){printf "%s\n",arr[i] };print temp}' test.txt
[groups]
abc =
 abc_read =

bac_read=
 bac=

developer=
 developer_read=

test =
 test_write=

#######################
[abc:/bcd/cde]
@test_write = rw

[bcd:/xyz/yzx]
@test= rw

################
[lmn:/bcd/cde]
  @test_write = rw

[lmn:/xyz/yzx]
  @test= rw

############################
user@host> (/home/user) $ uname -a
SunOS host 5.10 Generic_120011-14 sun4u sparc SUNW,Ultra-250 Solaris
user@host> (/home/user) $ gawk --version
GNU Awk 3.1.5
Copyright (C) 1989, 1991-2005 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
user@host> (/home/user) $

# 9  
Old 05-30-2011
Am i doing any mistake? I got below error
Code:
sunnt-x1> gawk '{
if($0~/^\[/)
{
n =asort(arr)
for(i=1;i<=n;i++)
printf "%s\n",arr[i]
delete arr
if(temp!=""){print temp}
print $0
temp=""
}
else if($0!~/^\#/)
{
arr[substr($0,0,3)]=arr[substr($0,0,3)]$0"\n ";
}
else
{
temp=$0
}
}
END{n =asort(arr);for(i=1;i<=n;i++){printf "%s\n",arr[i] };print temp}' test

Code:
gawk: cmd. line:4: (FILENAME=test FNR=1) fatal: function `asort' not defined
sunnt-x1> gawk --version
Gnu Awk (gawk) 3.0, patchlevel 0


Last edited by Franklin52; 05-30-2011 at 07:31 AM.. Reason: Please use code tags
# 10  
Old 05-30-2011
You aren't doing any mistakes, i guess gawk 3.0 may have not the asort function implemented. Let's try doing it out of awk.
# 11  
Old 06-01-2011
Hi,

Your question is not completely correct with your output where you say: and also have to insert a blank line before the line which starts with either "[" or "#". In your output there isn't a blank line just after the line '#######'. Tell me if I misunderstood.

Try next 'perl' script:
Code:
$ cat script.pl
use strict;
use warnings;

my %group;
my ($begin_grp, $end_grp) = ( qr(^\s*\[(?i:groups)\]\s*$), qr(^\s*#+\s*$) );
my $group_proc = 0;
my ($header, $sep);

while ( <> ) {
    chomp;
    if ( /$begin_grp/ .. /$end_grp/ ) {
        ( $header = $_, next ) if /$begin_grp/;
        ( $sep = $_, next ) if /$end_grp/; 
        s/^\s*(.*)\s*$/$1/;
        push @{$group{ +( substr $_, 0, 3 ) }}, $_;
        $group_proc = 1;
        next;
    }

    if ( $group_proc ) {
        print $header, "\n\n";
        for ( sort keys %group ) {
            print +( join "\n ", @{ $group{$_} } ), "\n\n";
        }
        $group_proc = 0;
        print $sep;
    }

    print "\n" if /^\s*(?:#|\[)/;
    print $_, ( /^#/ ? "": "\n");
    
}

print "\n";
$ perl script.pl infile
[groups]

abc = 
 abc_read =

bac_read=
 bac=

developer=
 developer_read=

test =
 test_write=

#######################
[abc:/bcd/cde]
@test_write = rw

[bcd:/xyz/yzx]
@test= rw

################
[lmn:/bcd/cde]
  @test_write = rw

 [lmn:/xyz/yzx]
  @test= rw

############################

Regards,
Birei
# 12  
Old 06-01-2011
Thanks birei,, I solved it.. The file actually contains two sections.
1 - defining groups
2 - Access control list based on urls.

I have to modify group list.

Hence i took group list contents from main file using sed and modified it using awk and inserted back to main file.
That worked perfectly.

Thanks for your reply and I will try to work with your solution.

Great day
Anil.G
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

2. Shell Programming and Scripting

Sort a line and Insert sorted word(s) in a line

Hello, I am looking to automate a task - which is updating an existing access control instruction of a server and making sure that the attributes defined in the instruction is in sorted order. The instructions will be of a specific syntax. For example lets assume below listed is one of an... (6 Replies)
Discussion started by: sanjayroc
6 Replies

3. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

4. Shell Programming and Scripting

insert blank line between lines

Hello, I am trying to write a script that will count the number of characters for each line in a file and all the lines that have less than 80 characters and that are ending with a period, I want it to insert a blank line after them immediately. But, for whatever reason the condition if ]] is... (3 Replies)
Discussion started by: Pouchie1
3 Replies

5. Shell Programming and Scripting

Insert blank line if grep not found

Hi all, I've googling around forum regarding my prob, the nearest would same as thread tittled Insert blank line if grep not found, but she/he did not mention the solution, so I would like to request your help I've this task, to search in file2 based on pattern in file1 and output it to... (4 Replies)
Discussion started by: masterpiece
4 Replies

6. Shell Programming and Scripting

Insert blank line in a file

I have a file with data as below : Heading 1 ------------- Heading 1 data1 Heading 1 data2 Heading 1 data3 Heading 1 data4 Heading 2 ------------- Heading 2 data1 Heading 2 data2 Heading 2 data3 Heading 2 data4 Heading 3 ------------- Heading 3 data1 Heading 3 data2 Heading 3... (2 Replies)
Discussion started by: yoursdivu
2 Replies

7. Shell Programming and Scripting

compare three files and insert a blank line at each mismatch

i need to compare three files in unix a.txt b.txt c.txt 1 2 1 2 5 3 4 6 5 5 6 6 i need to insert a blank line in the file if i don't find a match and put the items at the same column if found a match The items in the files... (4 Replies)
Discussion started by: mad_man12
4 Replies

8. Shell Programming and Scripting

How to insert a character in line in Special condition?

Hi, I have a log file generated by a tool which has the following look : /tmp/releases/directory/datefilename1_release_date.zip /tmp/releases/directory/datefilename2_release_date.zip /tmp/releases/directory/datefilename3_release_date.zip... (8 Replies)
Discussion started by: bhaskar_m
8 Replies

9. Shell Programming and Scripting

insert text and add blank line

I need your help, I want to add a text every 2nd line and also a blank line after 3 line (In the output 2nd line is "changetype: modify" and every 4th line is blank line) Input file format dn: abc orclsourceobjectdn: abcd dn: bcd orclsourceobjectdn: bcda dn: cba orclsourceobjectdn:... (7 Replies)
Discussion started by: athidhi
7 Replies

10. Shell Programming and Scripting

Insert blank line if grep not found

Hello everyone... please help if you can -- I'm stumped. Making this work will save me hours of manual labor: I need to search file2 for pattern in file1. If pattern found append file2 line to file3. If pattern not found append a blank line to file3. file1 contents example: 123 456 789... (6 Replies)
Discussion started by: michieka
6 Replies
Login or Register to Ask a Question