1

I have some data in the following format::

Info-programNumber!/TvSource/11100001_233a_32c0/13130^Info-channelName!5 USA^Info-Duration!1575190^Info-programName!CSI: ab cd

Delimiter = Info-

I tried to sort the string based on the delimiter in ascending order. But none of my solutions are working.

Expected Result:

Info-channelName!5 USA^Info-Duration!1575190^Info-programName!CSI: ab cd^Info-programNumber!/TvSource/11100001_233a_32c0/13130

Is there any command that will allow me to do this or do i need to write an awk script to iterate over the string and sort it?

1
  • 2
    isn't the delimiter ^ ? Commented Jun 28, 2013 at 11:21

2 Answers 2

3

Temporarily split the info into multiple lines so you can sort:

tr ^ \\n | sort | tr \\n ^

Note: if you have multiple entries, you have to write a loop, which processes it per line.. with huge datasets this is probably not a good idea (too slow), in which case pick a programming language.. but you were asking about the shell...

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

Comments

0

Can be done in awk itself:

awk -F "^" '{OFS="^"; for (i=1; i<=NF; i++) a[i]=$i} 
          END {n=asort(a, b); for(i=1; i<=n; i++) printf("%s%s", b[i], FS); print ""}' file

1 Comment

If this answer helped you solve your problem, please consider marking it as "accepted", so users facing a similar problem in the future will be able to see it easily.

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.