Unix/Linux Go Back    


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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-28-2007   -   Original Discussion by mgirinath
mgirinath's Unix or Linux Image
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 Unix and Linux 11-28-2007   -   Original Discussion by mgirinath
porter's Unix or Linux Image
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 Unix and Linux 11-28-2007   -   Original Discussion by mgirinath
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 22 January 2018, 3:18 PM EST
Location: Foxborough, MA
Posts: 8,204
Thanks: 231
Thanked 786 Times in 733 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 Unix and Linux 11-28-2007   -   Original Discussion by mgirinath
Lakris's Unix or Linux Image
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 Unix and Linux 11-28-2007   -   Original Discussion by mgirinath
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 22 January 2018, 3:18 PM EST
Location: Foxborough, MA
Posts: 8,204
Thanks: 231
Thanked 786 Times in 733 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 Unix and Linux 11-28-2007   -   Original Discussion by mgirinath
Lakris's Unix or Linux Image
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 Unix and Linux 11-28-2007   -   Original Discussion by mgirinath
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
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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-09-2011 12:57 AM
rows to columns suresh3566 Shell Programming and Scripting 3 03-10-2011 04:54 AM
Rows into columns? regexnub Shell Programming and Scripting 8 06-07-2010 11:29 PM
Columns into rows Ramya_1104 Shell Programming and Scripting 1 05-17-2010 05:10 AM
# of rows and columns kylle345 Shell Programming and Scripting 3 07-02-2009 09:15 PM



All times are GMT -4. The time now is 06:18 AM.