Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 11-28-2007
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  
Old 11-28-2007
Code:
for d in `echo A78645~B67354~H74658~N536482 | sed y/\~/\ /`; do echo $d; done

# 3  
Old 11-28-2007
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
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
# 5  
Old 11-28-2007
Quote:
Originally Posted by Lakris
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
# 6  
Old 11-28-2007
Quote:
Originally Posted by vgersh99
  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
# 7  
Old 11-28-2007
Just 'cos it's an interesting excercise in the different uses of streams:
Quote:
Originally Posted by Lakris
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)
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 to columns jimmyf UNIX for Beginners Questions & Answers 6 01-09-2017 01:56 PM
Compare 2 csv files by columns, then extract certain columns of matcing rows bkane3 Shell Programming and Scripting 5 06-04-2014 04:29 AM
Columns and rows shelllearner Shell Programming and Scripting 3 03-20-2014 05:43 AM
Columns to rows apenkov Shell Programming and Scripting 11 11-27-2013 07:27 PM
Evaluate 2 columns, add sum IF two columns match on two rows mtucker6784 Shell Programming and Scripting 6 08-07-2013 12:57 PM
Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks ks_reddy Shell Programming and Scripting 4 02-04-2013 04:37 AM
Columns to Rows sameeribraimo Windows & DOS: Issues & Discussions 1 07-13-2012 12:33 AM
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
Columns into rows Ramya_1104 Shell Programming and Scripting 1 05-17-2010 04:10 AM


All times are GMT -4. The time now is 06:21 PM.

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