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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort a line and Insert sorted word(s) in a line
# 1  
Old 09-05-2012
Question 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 existing instruction in a server. Attributes are the ones in between the (targetattr = "xx || yy")

Quote:
aci: (targetattr = "abca || abcc || abcb || babc || babd ||cabc || dabc || ............|| gabc || gabce || gbacd") (version 3.0;acl "test";allow (read,compare,search)(groupdn = "ldap:///cn=xxxxxxxxxxx"))
In this existing instruction,

1) First, i want to sort the attributes defined in ascending order like

Quote:
aci: (targetattr = "abca || abcb || abcc || babc || babd ||cabc || dabc || ............|| gabc || gabce || gbacd") (version 3.0;acl "test";allow (read,compare,search)(groupdn = "ldap:///cn=xxxxxxxxxxx"))
2) Then on the sorted instruction, I may want to add 1 or more strings like "gabcd" and "gbace" in between the existing strings (alphabetically ordered). So the expected final result must be something like below

Quote:
aci: (targetattr = "abca || abcb || abcc || babc || babd ||cabc || dabc || ............|| gabc || gabcd || gabce || gbacd || gbace") (version 3.0;acl "test";allow (read,compare,search)(groupdn = "ldap:///cn=xxxxxxxxxxx"))
Can someone help me to achieve this ?

Thanks
# 2  
Old 09-05-2012
If your awk supports asort you could try something like:
Code:
awk FS='"' '{
   MYFS="||";
   newlist="";
   n=split($2,elements,MYFS);
   asort(elements);
   for (i=1;i<n;i++) {
      newlist=newlist elements[i] MYFS;
   }
   newlist=newlist elements[n];
   $2=newlist;
   print;
}' inputfile

(although I haven't tested it...)

Last edited by CarloM; 09-05-2012 at 11:45 AM.. Reason: Typos...
# 3  
Old 09-05-2012
Hey ! Thanks for your reply. I couldn't get that piece working. how do i find if my awk supports asort. And can you please explain the code ?
# 4  
Old 09-05-2012
awk FS='"' ' - invoke awk with double-quote as the field separator.
{ - for any line
MYFS="||"; newlist=""; n=split($2,elements,MYFS); - Split field 2 into an array, using '||' to identify separate elements (and n assignment should be on split, not asort)
asort(elements); - Sort the array
for (i=1;i<n;i++) { newlist=newlist elements[i] MYFS; } - loop around the (ordered) array to create a new string, appending the current element and '||' every time
newlist=newlist elements[n]; - append the last element (should be n rather than i, or it will repeat the last-but-one)
$2=newlist; - overwrite the value of field 2 with the list we just created
print; - print the line

Last edited by CarloM; 09-05-2012 at 11:45 AM..
# 5  
Old 09-05-2012
Quote:
Originally Posted by sanjayroc
Hey ! Thanks for your reply. I couldn't get that piece working. how do i find if my awk supports asort. And can you please explain the code ?

I believe that asort() is only available in gawk (GNU awk). Try executing



Code:
awk --version

which might give you the version. Assuming it's not gawk, then you could implement your own sort. Probably not as efficient as the built-in, but better than nothing. Example below using the original code from CarloM and adding a sort (bubble) function.


Code:
awk FS='"' '

# sort v1 into ascending order
# n = length(v1) rather than passing in the length isnt supported in all flavours of awk
function bubble( v1, n,         s, i, pass, swap )
{
    swap = 1;
    pass = 0;

    while( swap )
    {
        swap = 0;
        pass++;
        for( i = 0; i < n - pass; i++ )
        {
            if( v1[i] > v1[i+1] )
            {
                swap++;
                s = v1[i];
                v1[i] = v1[i+1];
                v1[i+1] = s;
            }
        }
    }
}

{
   MYFS="||";
   newlist="";
   n=split($2,elements,MYFS);
   bubble( elements, n );   for (i=1;i<n;i++) {
      newlist=newlist elements[i] MYFS;
   }
   newlist=newlist elements[n];
   $2=newlist;
   print;
}' inputfile

# 6  
Old 09-06-2012
agama / CarloM,

mine is NOT a gawk. so i tried running manual sorting code by copying it to a script. It throws out syntax errors while executing the script. i am not an shell expert and unable to fix the syntax errors of this code. did this code worked for you ? please help !

Last edited by sanjayroc; 09-06-2012 at 10:30 AM..
# 7  
Old 09-09-2012
I cut/pasted the code from another post I made a while back on Unix.com while en route on a plane, so I didn't test it. There's a chance that I chopped something off, but after a quick peek at it again it looks OK to me. If you could post the errors, and your code, that might help determine what isn't working.


I'm again on a plane; I'm sure someone will see it and suggest changes/corrections.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

5. UNIX for Dummies Questions & Answers

Insert a line in a sorted text file(s)

Hello, I am trying to add a line (usually just a word) to some text files in a directory that are already sorted. I just don't want to run the sort command again because it can take a long time when the text or log files are really huge. I have a bashscript that will take in the 1st argument... (7 Replies)
Discussion started by: raptor25
7 Replies

6. Shell Programming and Scripting

sort with condition and insert blank line

Hi, I have a file with no blank line anywhere Its a conf file, The lines between and starts with "#" should be sort out with the first three characters(characters are between (a-z)). and insert a blank space in between the uniq lines. For clear understanding ,pls find the below input file... (11 Replies)
Discussion started by: anil8103
11 Replies

7. Shell Programming and Scripting

to insert some word somewhere in the line with shell (or perl)

hello mighty all there is a line of 50 words and i need to take a random number of words from the beginning (20 words for example) then put my word then add other 10 words from the continue then add another my special word then add 20 words till the end.. my own knowledge base can say it is... (12 Replies)
Discussion started by: tip78
12 Replies

8. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

9. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

10. Shell Programming and Scripting

insert word in each line of a file

can someone tell me how can I insert a word in front of each line in a file. i tried with sed but didn't managed yet. Is there another command or this one(sed) works? 10x anyway. (7 Replies)
Discussion started by: atticus
7 Replies
Login or Register to Ask a Question