Transpose an entire text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transpose an entire text file
# 8  
Old 09-30-2009
Smilie What about this one?
Code:
awk '{for(x=0;++x<=NF;)a[x]=a[x]?a[x]$x:$x}END{for(y=0;++y<=NF;)print a[y]}' FS= file

# 9  
Old 09-30-2009
Quote:
Originally Posted by danmero
Smilie What about this one?
Code:
awk '{for(x=0;++x<=NF;)a[x]=a[x]?a[x]$x:$x}END{for(y=0;++y<=NF;)print a[y]}' FS= file

Code:
this code is not working for me but radoulov code is working find because he is using the substr command ....
but my question is how I can set the FS variable to null FS="" using solaries 10??

BR
# 10  
Old 09-30-2009
Quote:
Originally Posted by danmero
Smilie What about this one?
Code:
awk '{for(x=0;++x<=NF;)a[x]=a[x]?a[x]$x:$x}END{for(y=0;++y<=NF;)print a[y]}' FS= file

The problem is that the null string as FS separator is treated differently in older AWK implementations.

From Effective GAWK Programming:

Quote:
There are times when you may want to examine each character of a record separately.
This can be done in gawk by simply assigning the null string ("") to FS. In
this case, each individual character in the record becomes a separate field.[...]
Traditionally, the behavior of FS equal to "" was not defined. In this case, most
versions of Unix awk simply treat the entire record as only having one field. (d.c.)
In compatibility mode (see the section “Command-Line Options” in Chapter 11), if
FS is the null string, then gawk also behaves this way
I mean:

Code:
zsh-4.3.10[t]% print abc | gawk '{while (++i<=NF) print "$"i, "is:", $i}' FS=
$1 is: a
$2 is: b
$3 is: c
zsh-4.3.10[t]% print abc | gawk --posix '{while (++i<=NF) print "$"i, "is:", $i}' FS=
$1 is: abc



---------- Post updated at 02:45 PM ---------- Previous update was at 02:43 PM ----------

Quote:
Originally Posted by ahmad.diab
[...]
but my question is how I can set the FS variable to null FS="" using solaries 10??
You already did it, but it has a different meaning for your AWK implementation.
And of course, you can install gawk on Solaris Smilie.
# 11  
Old 09-30-2009
Quote:
This solution provided by danmero is not working on solaries were my system can't take FS="" as null value???
Code:
awk '{z=length($0);split($0,a,"");for(x=0;++x<=z;)b[x]=b[x]?b[x]a[x]:a[x]}END{for(y=0;++y<=z;)print b[y]}' file

# 12  
Old 09-30-2009
Quote:
Originally Posted by danmero
Code:
awk '{z=length($0);split($0,a,"");for(x=0;++x<=z;)b[x]=b[x]?b[x]a[x]:a[x]}END{for(y=0;++y<=z;)print b[y]}' file

For older awk implementations split on null string is not special either Smilie
As far as I know only substr will work.
# 13  
Old 09-30-2009
Can't confirm but should work on Solaris base on awk man page Solaris Smilie
Let's see if will work for OP.
# 14  
Old 09-30-2009
Quote:
Originally Posted by radoulov
For older awk implementations split on null string is not special either Smilie
As far as I know only substr will work.
Code:
you are right radoulv I already test split function using null as a separator but without any use...
BR

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Print text between 2 strings for the entire file

hey guys, for the following output: starting open open close close starting close starting open close close starting open open close open (2 Replies)
Discussion started by: boaz733
2 Replies

3. UNIX for Dummies Questions & Answers

Append two lines of text to php.ini in the entire directory tree.e

I am looking to write a script that will read the php.ini files on my web host. If the two lines do exist do nothing. If not append two lines to the end of it then move on to the next directory and open the next php.ini file. I have the beginning of one that was given to me on another web site but... (6 Replies)
Discussion started by: Larrykh465
6 Replies

4. Shell Programming and Scripting

Replacing entire fields with specific text at end or beginning of field

Greetings. I've got a csv file with data along these lines: Spumoni's Pizza Place, Placemats n Things, Just Lamps Counterfeit Dollars by Vinnie, Just Shades, Dollar StoreI want to replace the entire comma-delimited field if it matches something ending in "Place" or beginning with "Dollar",... (2 Replies)
Discussion started by: palmfrond
2 Replies

5. UNIX for Dummies Questions & Answers

Fill csv entire column with content from another text file

I have a csv that looks like this: ,yude-to-nap2,0,0,0,0,0 ,2twis-yude-to-nap2,0,0,0,0,0 ,2tiws-yude-to-nap2,0,0,0,0,0 ,2arcos-yude-to-nap2,0,0,0,0,0 and another file named m1 that has a single line of text as content: Feb 1 15:30:20 How can I fill the whole the empty column of the... (1 Reply)
Discussion started by: RobertoRivera
1 Replies

6. Shell Programming and Scripting

Transpose few columns alone in a Text file

Hi Friends, I am Stuck up with a problem on transposing Rows to Coloumns.. Though there are many threads on this my problem is little difficult.. I have a tab separated file like Below, computer selling_loc currency_type manufacturer_name salesweek-wk1 sales-wk2 ...wk-3 ..wk4 till... (7 Replies)
Discussion started by: heinz_holan
7 Replies

7. Shell Programming and Scripting

Transpose a text file.

Hello, I have a text file which is like a matrix m rows and n columns. Now I want to convert it into n rows and m columns. Thanks for hint. (1 Reply)
Discussion started by: zhshqzyc
1 Replies

8. Shell Programming and Scripting

Context Sensitive smallcap => TitleCap conversion in an entire text file

I desperately need to write a script that go into text documents that list the location of files in small caps and have it convert the directories and subdirectories that lead to the file as Title Caps while leaving the file itself in small caps... to illustrate what I mean: I need to turn txt... (6 Replies)
Discussion started by: monkeyman
6 Replies

9. Shell Programming and Scripting

File Transpose

Hi ALL I have one input file say FILE1 which looks as below. a=1 b=2 c=3 a=4 b=5 c=6 . . . Here a,b,c...etc are variable names. The output file(FILE2) should look like 1,2,3 4,5,6 ..... ..... (5 Replies)
Discussion started by: 46019
5 Replies

10. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies
Login or Register to Ask a Question