The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to convert a single column into several rows and columns? ashton_smith UNIX for Dummies Questions & Answers 5 05-24-2008 04:44 PM
shell script required to convert rows to columns suresh3566 Shell Programming and Scripting 2 05-07-2008 05:25 AM
deleting rows & columns form a csv file code19 Shell Programming and Scripting 2 03-13-2008 10:06 AM
Cut rows obedkhan HP-UX 2 08-23-2006 09:36 AM
Finding different rows dombi Shell Programming and Scripting 2 03-06-2006 05:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-28-2007
mgirinath mgirinath is offline
Registered User
  
 

Join Date: May 2005
Posts: 69
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
  #2 (permalink)  
Old 11-28-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Code:
for d in `echo A78645~B67354~H74658~N536482 | sed y/\~/\ /`; do echo $d; done
  #3 (permalink)  
Old 11-29-2007
mgirinath mgirinath is offline
Registered User
  
 

Join Date: May 2005
Posts: 69
Quote:
Originally Posted by porter View Post
Code:
for d in `echo A78645~B67354~H74658~N536482 | sed y/\~/\ /`; do echo $d; done
The above code works but it is splitting the record if the record has space in it.

Quote:
If i have a record A3647 76~A784657~J67364 9~H7846 9586.
it splits it into
A3647
76
A784657
J67364
9
H7846
9586
can this be fixed.
  #4 (permalink)  
Old 11-29-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
echo "A3647 76~A784657~J67364 9~H7846 9586" | sed 's/\~/\
/g'
A3647 76
A784657
J67364 9
H7846 9586
  #5 (permalink)  
Old 11-29-2007
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 242
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
Quote:
A~B~C
1~2~3
X~Y~Z
to become

Quote:
A1X
B2Y
C3Z
or

Quote:
A
B
C
1
2
3
X
Y
Z

or even

Quote:
A
1
X
B
2
Y
C
3
Z


???
Have Yo tried my suggestion?

/Lakris
  #6 (permalink)  
Old 11-28-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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)
}
  #7 (permalink)  
Old 11-28-2007
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 242
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0