Lines to Column conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Lines to Column conversion
# 1  
Old 09-10-2012
Lines to Column conversion

Hi All,

I have a sample file with content:
Code:
[root@xyx1 tmp]# cat test1.txt
mahesh
suresh
anil
[root@xyx1 tmp]#

I need to get it like this:
Code:
m s a
a u n
h r i
e e l
s s
h h

Basically row to column and column to row. I am trying to use this:
Code:
while read line
do
     echo $line | fold -bw 1
done<test1.txt

But its showing output as
Code:
m
a
h
e
s
h
s
u
r
e
s
h
a
n
i
l

Can anyone help please?

Thanks,
RK


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 09-10-2012 at 08:16 AM.. Reason: code tags
# 2  
Old 09-10-2012
If using awk is an option, you may try:
Code:
awk '{for(i=1;i<=length;i++) a[NR,i]=substr($0,i,1);if(length>max) max=length}
END{
for(i=1;i<=max;i++){
 for(j=1;j<=NR;j++) 
  printf "%s ",a[j,i]?a[j,i]:" "
 printf "\n"
}}' file


Last edited by elixir_sinari; 09-10-2012 at 10:13 AM..
# 3  
Old 09-10-2012
Code:
awk '{a[NR]=$0;if(length>max) max=length}END{for(j=1;j<=max;j++){for(i=1;i<=NR;i++){printf "%s ",substr(a[i],j,1)};printf "\n"}}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

2. Shell Programming and Scripting

Text Conversion in Column

Hi all; We have a table: COL1 COL2 COL3 COL4 COL5 COL6 COL7 val1 val2 val3 val4 val5 1 val7 val1 val2 val3 val4 val5 3 val7 val1 val2 val3 val4 val5 5 val7 val1 val2 val3 val4 val5 2 val7 ... As you see, 6th columns are filled with numbers as, 1, 2, 3, 4, etc... and I want to make a... (10 Replies)
Discussion started by: gc_sw
10 Replies

3. Shell Programming and Scripting

Conversion if 1st column is match (awk '{s+=$1} END

Hi im trying to add numbers, got no problem with it im using awk '{s+=$1} END {print s, "MB"}', but what if the numbers are like mention below. who will i add them 2000 KB 1 MB Answer: 2001 Desired: 2000 KB 1 MB Answer: 3000 (4 Replies)
Discussion started by: invinzin21
4 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Uneven column to row conversion

Hi Unix Forum, I have a relatively easy question i suppose for which, however, until now i could not find a solution. I am working with a program that will give me an output file similar to the following: A 1 2 3 4 B 1 2 3 4 C 1 (9 Replies)
Discussion started by: Leander
9 Replies

5. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Solaris

Rows to column conversion in Solaris

i am using the command pkginfo -l | /usr/xpg4/bin/grep -e 'VENDOR' -e 'NAME' -e 'VERSION' >> /tmp/test1.txt in order to get name,vendor and version of the applications installed on Solaris machine.The output stored in test1.txt in as shown below: NAME: Solaris Zones (Usr) VERSION: ... (4 Replies)
Discussion started by: omkar.jadhav
4 Replies

7. Shell Programming and Scripting

Row to Column conversion?

I have a text file with the geneIds separated by space in each line. The number Ids in lines are different. The file is like: abc qwe tyu ghj jkl dfg sdf cvb sdk fgh tyu uio iop tyu rty eru wer rty iop asd sdf dfg fgh zxc I want to format the file like: abc qwe tyu ghj jkl ... (7 Replies)
Discussion started by: sammy777
7 Replies

8. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

9. UNIX for Dummies Questions & Answers

converting unique identifiers in a column using conversion file

Hello, I often have this problem: I have a file with a column of unique identifiers e.g. file1 below has an id column and data column/columns with p rows: cat data1 dog data2 cow data3 . . . elephant datap-1 horse datap and I have a conversion file,file2, with n<p rows... (4 Replies)
Discussion started by: peanuts48
4 Replies

10. Shell Programming and Scripting

column to row conversion with additional pattern

Hi there, I've an input file1 as follows: 1001 1002 1003 1004 1005 I would like to have an output file2 as follows: Numbers are 1001/ 1002/ 1003/ 1004/ 1005/ Any help is appreciated. (2 Replies)
Discussion started by: kbirde
2 Replies
Login or Register to Ask a Question