👤


Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Columns to rows

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-28-2007
mgirinath mgirinath is offline
Registered User
 
Join Date: May 2005
Last Activity: 3 November 2008, 2:18 PM EST
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
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
Quote:
A78645~B67354~H74658~N536482
How do i get the occueance of ~ and then split it into rows as and below is what I have to achieve.
Quote:
A78645
B67354
H74658
N536482
Thanks
Sponsored Links
    #2  
Old 11-28-2007
porter porter is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 8 January 2008, 6:50 PM EST
Posts: 2,965
Thanks: 0
Thanked 5 Times in 5 Posts
Code:
for d in `echo A78645~B67354~H74658~N536482 | sed y/\~/\ /`; do echo $d; done

Sponsored Links
    #3  
Old 11-28-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 18 July 2018, 1:34 AM EDT
Location: Foxborough, MA
Posts: 8,310
Thanks: 257
Thanked 838 Times in 782 Posts
nawk -f transpose.awk myFile

transpose.awk:
Code:
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)
}

    #4  
Old 11-28-2007
Lakris Lakris is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 8 April 2015, 8:34 AM EDT
Location: Stockholm
Posts: 317
Thanks: 0
Thanked 7 Times in 7 Posts
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.
Quote:
cat splitsource.txt| tr "~" "\n"
This will shove the textfile through the program tr, and replace all occurances of ~ with a newline.
Try it.

/Lakris
Sponsored Links
    #5  
Old 11-28-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 18 July 2018, 1:34 AM EDT
Location: Foxborough, MA
Posts: 8,310
Thanks: 257
Thanked 838 Times in 782 Posts
Quote:
Originally Posted by Lakris View Post
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
Sponsored Links
    #6  
Old 11-28-2007
Lakris Lakris is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 8 April 2015, 8:34 AM EDT
Location: Stockholm
Posts: 317
Thanks: 0
Thanked 7 Times in 7 Posts
Quote:
Originally Posted by vgersh99 View Post
  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
Sponsored Links
    #7  
Old 11-28-2007
Smiling Dragon's Unix or Linux Image
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
 
Join Date: Nov 2007
Last Activity: 27 July 2017, 4:23 PM EDT
Location: New Zealand
Posts: 1,060
Thanks: 25
Thanked 29 Times in 28 Posts
Just 'cos it's an interesting excercise in the different uses of streams:
Quote:
Originally Posted by Lakris View Post
Code:
cat splitsource.txt| tr "~" "\n"

Can become:
Code:
tr "~" "\n" < splitsource.txt

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

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

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rows into Columns greycells Shell Programming and Scripting 1 05-08-2011 11:57 PM
rows to columns suresh3566 Shell Programming and Scripting 3 03-10-2011 03:54 AM
Rows into columns? regexnub Shell Programming and Scripting 8 06-07-2010 10:29 PM
Columns into rows Ramya_1104 Shell Programming and Scripting 1 05-17-2010 04:10 AM
# of rows and columns kylle345 Shell Programming and Scripting 3 07-02-2009 08:15 PM



All times are GMT -4. The time now is 02:51 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?