Row to Column conversion?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Row to Column conversion?
# 1  
Old 08-03-2013
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:
Code:
 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:
Code:
 abc
 qwe
 tyu
 ghj
 jkl
 dfg
 sdf

 cvb
 sdk
 fgh
 tyu
 
 uio
 iop
 tyu
 .
 . 
 iop
 
 asd 
 sdf
 .
 . 
 .
 zxc

Any help would be appreciated. Thanks!

Last edited by Scott; 08-03-2013 at 11:16 AM.. Reason: Please use code tags
# 2  
Old 08-03-2013
Code:
$ 
$ cat f02
abc qwe tyu ghj jkl dfg sdf
cvb sdk fgh tyu
uio iop tyu rty eru wer rty iop
asd sdf dfg fgh zxc
$ 
$ perl -pne 'BEGIN {$\="\n"}s/ /\n/g' f02
abc
qwe
tyu
ghj
jkl
dfg
sdf

cvb
sdk
fgh
tyu

uio
iop
tyu
rty
eru
wer
rty
iop

asd
sdf
dfg
fgh
zxc

$ 
$

This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 08-04-2013
Code:
awk '{$1=$1}1' OFS="\n" ORS="\n\n" infile

This awk works if you have a space in front of some of the line or not
cvb vs cvb
@durden_tylor, you have removed this space in f02
perl gives an extra blank row due to this space

Last edited by Jotne; 08-04-2013 at 05:41 AM..
This User Gave Thanks to Jotne For This Post:
# 4  
Old 08-04-2013
sed works too ...
Code:
sed -e "s/$/\n/" -e "s/[ \n]/\n/g" infile

This User Gave Thanks to Just Ice For This Post:
# 5  
Old 08-04-2013
sed has same problem as perl
it adds double blank line, since there is a space infront of the line
Code:
abc
qwe
tyu
ghj
jkl
dfg
sdf


cvb
sdk
fgh
tyu


uio
...
...

This User Gave Thanks to Jotne For This Post:
# 6  
Old 08-04-2013
Quote:
Originally Posted by Just Ice
sed works too ...
Code:
sed -e "s/$/\n/" -e "s/[ \n]/\n/g" infile

The OP did not specify an operating system, so I'll mention that because of \n in the replacement text, that sed script will fail on most non-Linux systems.

Regards,
Alister

---------- Post updated at 01:17 PM ---------- Previous update was at 01:09 PM ----------

Quote:
Originally Posted by Jotne
Code:
awk '{$1=$1}1' OFS="\n" ORS="\n\n" infile

This awk works if you have a space in front of some of the line or not
If we assume that the sample data and sample output in the OP are exactly correct, then your solution is incorrect. The sample output has a leading space which your solution discards. Additionally, your solution adds a concluding blank line which does not appear in the sample output.

The OP should clarify exactly what they want because there is a discrepancy between the problem description and the sample input/output. The post's text appears to state (due to the grammar, it's impossible to be certain) that input fields are delimited by a single space (supported by the sample data). If that's correct, then, in the absence of special consideration (which isn't mentioned in the post) a leading space represents an empty first field which should become a blank line. However, this would cause an ambiguity in the output format, since a blank line could represent an empty field or a record separator.

If we assume that the leading space on every line of the input and output is an artifact of copy/pasting, a simple, portable solution which treats the input as newline delimited records of single-space delimited fields and converts it to blank-line delimited records (with the exception that the final record is not followed by a blank line) of newline delimited fields:
Code:
sed 'y/ /\n/; $!G'

Regards,
Alister

Last edited by alister; 08-04-2013 at 02:23 PM..
This User Gave Thanks to alister For This Post:
# 7  
Old 08-04-2013
I do see now that the OPs output request also has space in front
Code:
awk '{$1=" "$1}1' OFS="\n " ORS="\n\n"

This gives space in front of all line.
But we do not know for sure of is this he wants Smilie
This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
3 Replies

2. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

3. 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

4. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

complicated row to rows conversion

I have a file containing rows with the following format. Field1|Field2|Field3|data1:data data2:data data3:"dataA:data dataB:data" data4:data:data (and so on) I need to format the above row into multiple rows that look like this: Field1|Field2|Field3|data1|data ... (2 Replies)
Discussion started by: newreverie
2 Replies

7. Shell Programming and Scripting

Date conversion in row while preserving rest of fields

Hi Forum. I searched the forum for a solution but could not find an exact one to my problem. I have some records in the file where I would like to convert the last date field to another format while preserving the rest of the other fields. For example: Found:... (6 Replies)
Discussion started by: pchang
6 Replies

8. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

9. 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

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question