Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swap column / row 1

Status
Not open for further replies.

lgtshadow

Technical User
Dec 31, 2003
5
US
How can I transpose all the rows for columns in a text file?

All the fields are space delimited and I have no idea how many columns there are to start off with.
I had a simple thing to do one at a time but I can't figure out how to make this expand to do all of the columns at once.

gawk '$3 == "500" {printf $4 " "}' xx.x > zz.z; echo >>zz.z

Any help you guys could offer would be wonderful.
 
How can I transpose all the rows for columns in a text file?
Something like this ?
gawk '
{for(i=1;i<=NF;++i)a[NR]=a[NR]" "$i}
END{for(i=1;i<=NR;++i)print a}
' /path/to/inputfile

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Great that does it. It amazes me how quick people are with help here thank you.
 
a variation on the theme:

Code:
# with this infile:
#
# a b c d
# e f g h
# i j k l
#
{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 ",a[j,k])}print ""}

# outputs:
#
#
# a e i
# b f j
# c g k
# d h l

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Do you mean if you have a file

a b c
d e f

you want the result to be

a d
b e
c f

If so, try this

{
for(i=1;i<=NF;++i)a=a" "$i
if (maxnf < NF) maxnf = NF
}
END{for(i=1;i<=maxnf;++i)print a}

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top