2

I want to sort the following data in a particular order. I tried sort in different way but couldn't find any solution. please help. I am a newbie in Unix.:(

Data:-

method1:entry:2013.09.18.19.18.30
method1:exit:2013.09.18.19.18.30
method2:entry:2013.09.18.19.18.30
method2:exit:2013.09.18.19.18.30
method3:entry:2013.09.18.19.18.30
method4:entry:2013.09.18.19.18.30
method4:exit:2013.09.18.19.18.30
method1:entry:2013.09.18.19.18.30
method1:exit:2013.09.18.19.18.30
method3:exit:2013.09.18.19.18.30
method3:entry:2013.09.18.19.18.30
method5:entry:2013.09.18.19.18.30
method5:exit:2013.09.18.19.18.30
method3:exit:2013.09.18.19.18.30

Desired output:-

method1:entry:2013.09.18.19.18.30
method1:exit:2013.09.18.19.18.30
method1:entry:2013.09.18.19.18.30
method1:exit:2013.09.18.19.18.30
method2:entry:2013.09.18.19.18.30
method2:exit:2013.09.18.19.18.30
method3:entry:2013.09.18.19.18.30
method3:exit:2013.09.18.19.18.30
method3:entry:2013.09.18.19.18.30
method3:exit:2013.09.18.19.18.30
method4:entry:2013.09.18.19.18.30
method4:exit:2013.09.18.19.18.30
method5:entry:2013.09.18.19.18.30
method5:exit:2013.09.18.19.18.30

The sorting should be based on method name and 'entry-exit' occurrence.

3
  • How do you know which method1:exit should be placed after a method1:entry? Both method1:exits are identical. The same is true for method1:entry itself. You just want to match pairs of entry and exits which have same method name? Commented Oct 3, 2013 at 7:39
  • @moghaddam: Yes, I want to just pair entry and exit with same method name. Commented Oct 3, 2013 at 7:44
  • I Think there is no way to do this directly in shell using commands and utilities (like sort, set or awk). You have to write some python or perl scripts to do this Commented Oct 3, 2013 at 15:51

2 Answers 2

2

It appears you simply want to sort by method name, which is the first colon-delimited field.

sort -t: -s -k1,1 file.txt

The -s flag (stable sort) prevents sort from modifying the relative order of lines with the same first field.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

sed -e 's/:/ /g' file.txt | sort |
awk 'BEGIN { var_entry="entry"; var_exit="exit"; flag="entry" }
    { if (flag == $2 && var_entry ==$2 ){
        i = 0; flag=var_exit; }
      else if (flag == $2 && var_exit == $2 ){
        i = 0; flag=var_entry; };
      i++ ; print i, $0 }' |
sort -t" " -k 2,2 -k 1,1 | sed 's/^[0-9]* //g'

The logic behind this is :

  1. sed -e 's/:/ /g replaces : with a space so the delimiters are consistent.

  2. sort simply sorts on method1 column.

  3. awk step appends another column so that we can sort on that column so that we have a pattern like entry exit for matching method1, output is :

    1 method1 entry 2013.09.18.19.18.30
    2 method1 entry 2013.09.18.19.18.30
    1 method1 exit 2013.09.18.19.18.30
    2 method1 exit 2013.09.18.19.18.30
    1 method2 entry 2013.09.18.19.18.30
    1 method2 exit 2013.09.18.19.18.30
    1 method3 entry 2013.09.18.19.18.30
    2 method3 entry 2013.09.18.19.18.30
    1 method3 exit 2013.09.18.19.18.30
    2 method3 exit 2013.09.18.19.18.30
    1 method4 entry 2013.09.18.19.18.30
    1 method4 exit 2013.09.18.19.18.30
    1 method5 entry 2013.09.18.19.18.30
    1 method5 exit 2013.09.18.19.18.30

  4. sort -t" " -k 2,2 -k 1,1 : then we sort on method1 column (2nd column) and if there are conflicts we resolve them on the newly added column viz. 1st column. output is :

    1 method1 entry 2013.09.18.19.18.30
    1 method1 exit 2013.09.18.19.18.30
    2 method1 entry 2013.09.18.19.18.30
    2 method1 exit 2013.09.18.19.18.30
    1 method2 entry 2013.09.18.19.18.30
    1 method2 exit 2013.09.18.19.18.30
    1 method3 entry 2013.09.18.19.18.30
    1 method3 exit 2013.09.18.19.18.30
    2 method3 entry 2013.09.18.19.18.30
    2 method3 exit 2013.09.18.19.18.30
    1 method4 entry 2013.09.18.19.18.30
    1 method4 exit 2013.09.18.19.18.30
    1 method5 entry 2013.09.18.19.18.30
    1 method5 exit 2013.09.18.19.18.30

  5. sed 's/^[0-9]* //g' : we remove the extra column which was created.

4 Comments

@snyder.The output is coming as a single line in your code. Is there any way can I get the output in different lines? wc -l filename is returning 1.
@anz try this cat file.txt | sed 's/method/\nmetthod/g'
method name won't be constant, it can be some other name also.
@anz I have tested the above command on my shell, it gives newline. I am using bash, which shell are you using ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.