Columns to rows

I have a file which has values seperated by ~ as shown below and I have to achieve a column to row from this.

Incoming row is like this

How do i get the occueance of ~ and then split it into rows as and below is what I have to achieve.

Thanks

for d in `echo A78645~B67354~H74658~N536482 | sed y/\~/\ /`; do echo $d; done

nawk -f transpose.awk myFile

transpose.awk:

BEGIN {
   FS=OFS="~"
}
{for(i=1;i<=NF;i++){a[i,NR]=$i};m=NF;n=NR}
END{
   for(j=1;j<=m;j++)
         for(k=1;k<=n;k++)
             printf("%s%s",a[j,k],(k==n) ? "\n" : OFS)
}

It doesn't have to be so complicated. Depends on if You want to group it or so but the simplest way would be to use tr, i think.

This will shove the textfile through the program tr, and replace all occurances of ~ with a newline.
Try it.

/Lakris

  1. UUOC
  2. I'm guessing the OP wanted to transpose a matrix - your suggested solution will not achive this

Now this was interesting!
A guess is as good as a poop in the dark...

  1. How very intelligent of you. Give me a good example of UUOC when it comes to tr? It IS a typical filter program isn't it? Spare me the von oben.

  2. Was there anything in the question that indicated that he wanted to have a transposed matrix from the source? Instead of just splitting? I tried to supply a simple solution to a simple question. And Did I not specifically state that "Depends on if You want to group it or so but the simplest way would be", so what is Your problem? If he need clarification which is the normal way to go here, he can do so in a following post. And in that case I can give further examples.

I have seen your posts of "RTFM" and I don't agree. I like to try to help as far as I can go.

I am actually offended by your post.

/Lakris

Just 'cos it's an interesting excercise in the different uses of streams:

Can become:

tr "~" "\n" < splitsource.txt

But I suspect the system calls would be much the same either way :confused:

(And no, I'm not going anywhere near the discussion of UUOC or the OP's intent or the merits of pointing it out)

Huge difference, the "|" requires another fork(), possibly the single most expensive call in UNIXdom.

But here it's just an example to demonstrate "tr" more than coding techniques.

Yes, Dragon and Porter, I must admit I overlooked the basic concept of *ashism, that is, use < as internal instead of cat or backticks to save cycles or whatever but I tried to exemplify a concept. To push one thing through another.

I may stand corrected, but I am still offended to have been associated with UUOC...

I wouldn't worry, shell scripts are for glueing things together, not building high performance systems. If worried about performance but still want scripting then use perl, it does all the manipulation in a single process. If you want speed kick down to C.

Also, if an additional cat or two are out of the bag I wouldn't worry as long as the system works and people can understand the code.

Exactly.
Get the the job done with a minimum of fuzz. Using the toolbox at the fingers optimum. Not the CPUs. Just wish mgirinath got back in the thread for some feedback....

Now that's really useful to know (up till now I hadn't thought about the fact that the shell would have to fork the command) - as people have said, it's not exactly the tool of choice if it has to run fast but I prefer to be pedantic about wasted cycles when I have the luxury of time :slight_smile:
Cheers for the pointer!

Depending on the pooper - you made yours and I made mine - that's all - very simple. There was no intention of pointing any shortcoming or anything of the sort. It's too bad if it was taken the wrong way.

Once again - the intent was to simply point out what's been discussed at multiple forums and NewsGroups of the years. It was intended as a point of reference and nothing else.

I have no problem - I simply provided my interpretation of the OPs question and a corresponding solution - nothing more and nothing less. You can actually find lots of threads (at these forums and the others) where people state their interpretation and provide their solution - nothing wrong with that.
If the OP comes back and clarifies the requirement - so much better for him/her/it. (But somehow I have a gut feeling we won't hear about it. I might be wrong though - I've been known to be wrong before, mind ya!)

This is an "interesting" statement, Are you saying that by posting an alternative solution I have not helped the OP?
As far as the RTFMs go... If I feel that the OP has invested enough of his/her/its own time investigating the issue and is willing to "take a ride" with me coming up with the solution - I've been more then willing to invest my own time doing so. On the other hand, if I feel that the OP is leaching for a 'quick and dirty' solution and is not really interested to know WHY it works the way it does.... well... I'll point out to a 'man' page and see if the OP comes back with more detailed questions and show his/her/its own willingness. As the practice has shown - most "unwilling" OPs are just transients leaching for solutions and they tend to find other places on the net for that - so better for them!

I don't see why - (once again) it was not the intent. If you're so easily offended and take things so closely to heart by a post on the net.... well... This is becoming a topic for a different discussion......

Once again - that was not the intent!
I simply pointed out what I thought would be interesting for the OTHERS (including you) to know and to read up on - after all these forums (and many others on the net) is for sharing the knowledge. If one does not feel (s)he is gaining any - maybe it's time to reconsider.
As far as the UUOC goes... I have not been using the 'cat | grep | grep | awk | sed' "technique" for years now. It's simply not my personal style of coding. Not because I'd feel offended if I do, but simply because I do it differently - UUOC or noUUOC.
Now.... whether you want to code this way or not - it's YOUR call. But knowing the alternatives and the reasoning behind it (regardless whether you agree with it or not) I'd think it's somewhat advantageous!

The above code works but it is splitting the record if the record has space in it.

can this be fixed.

echo "A3647 76~A784657~J67364 9~H7846 9586" | sed 's/\~/\
/g'
A3647 76
A784657
J67364 9
H7846 9586

I am sorry vgersh99, but it was Your short tone, pointing me to a "bad style of programming" page, and saying that my solution don't work, that triggered me. Of course I know there are many ways to do the same thing. I may have overreacted. Sorry.

mgirinath? Can You explain more about what You want?

Do You want

to become

or

or even

???
Have Yo tried my suggestion?

/Lakris