Using a command to go column by column | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Using a command to go column by column

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-25-2013
iconig iconig is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 30 January 2013, 1:23 PM EST
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Using a command to go column by column

Hi,
Could somebody help me with a code that passes each column through a command or set of commands, one column at a time. LIke this:
cat data:
4 89 87
5 3 89
10 82 4
39 10 39
100 98 9
1 4 3

Code:

Code:
awk '{print $1}' data | sort | gstat > group1
awk '{print $2}' data | sort | gstat > group2
awk '{print $3}' data | sort | gstat > group3

Of course, the right code will not have these three different codes, just one code that goes through each column and outputs the result to different files.

Thanks
Sponsored Links
    #2  
Old 01-25-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 29 July 2014, 6:28 PM EDT
Location: Galactic Empire
Posts: 3,356
Thanks: 230
Thanked 1,190 Times in 1,123 Posts

Code:
awk '{ print $1 | "sort -n > group1"; print $2 | "sort -n > group2"; print $3 | "sort -n > group3"; } ' data

Sponsored Links
    #3  
Old 01-25-2013
iconig iconig is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 30 January 2013, 1:23 PM EST
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Your code is essentially the same as mine. The problem is if I have 100 column, I do not want to write out each column by hand. I am looking for a code that will go through each column one after the other, without me having to do it myself.
Thanks
    #4  
Old 01-25-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 19 July 2014, 2:22 PM EDT
Location: France
Posts: 318
Thanks: 18
Thanked 81 Times in 76 Posts
A whole script that'll do what you want

Code:
#!/bin/sh

# Uncomment this to start by deleting tmp files that will be created for further use
# Uncomment it if you plan to use the script more than once in the same dir
# This, because of the "append" redirection in the awk script
#rm "${F:=tmp}"*

# awk script to separate columns into each tmp file
# this returns as well the number of columns for later use -stored in n-
n=$(awk -v f="$F" '
{x=0; while(x++<NF)A[x,FNR]=$x}
END{
for(i=1;i<x;i++){
	for(j=1;j<FNR;j++)print A[i,j] >> f i
	}
	print i-1
}
' data)

# Sorting each tmp into group file
# Using n to count tmp files
while [ "$((i+=1))" -lt "$n" ]; do
	sort -n "$F$i" > "group$i"
done

exit 0


Last edited by tukuyomi; 01-25-2013 at 04:26 PM..
Sponsored Links
    #5  
Old 01-25-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 29 July 2014, 6:28 PM EDT
Location: Galactic Empire
Posts: 3,356
Thanks: 230
Thanked 1,190 Times in 1,123 Posts

Code:
# Awk code for transposing and creating group files
awk 'BEGIN{t=1} {
 for(i=1;i<=NF;i++) {
  a[NR,i] = $i;
 }
} NF>nf {
 nf = NF;
} END {
 for (i=1;i<=nf;i++) {
  for (j=1;j<=NR;j++) {
   file="group"t;
   print a[j,i] > file;
   t=(j==NR)?++t:t;
  }
 }
} ' file

# Sorting the data for each group files
for file in group*
do
 sort -n "$file" > tmp; mv tmp "$file"
done

Sponsored Links
    #6  
Old 01-25-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 July 2014, 8:48 PM EDT
Location: San Jose, CA, USA
Posts: 4,183
Thanks: 163
Thanked 1,427 Times in 1,211 Posts
The following is similar to bipinajith's shell script, but doesn't create the temp files, doesn't include empty lines in the data to be sorted if some rows in the data file have more fields than others, feeds the sorted output into your gstat command, and uses 3 digits in the group files instead of a variable number of digits. If you have a 100 columns in your data file, there is also a chance that bipinajith's script will run out of file descriptors on many implementations of awk.

Try:

Code:
awk '
{       for(i = 1; i <= NF; i++) a[NR,i] = $i
        if(NF > nf) nf = NF
}
END {   for(f = 1; f <= nf; f++) {
                cmd = sprintf("sort -n | gstat > group%03d", f)
                for(i = 1; i <= NR; i++)
                        if((i,f) in a)
                                printf("%s\n", a[i,f]) | cmd
                close(cmd)
        }
}' data

As always, if you're using a Solaris/Sun OS system, use /usr/xpg4/bin/awk or nawk instead of awk .

Note that I've never heard of the gstat command, but as long as it is on your command search path, this should work. (When I tested it, I just used a shell script named gstat that reported that it had been called and listed the contents of the data it found on its standard input.)
The Following User Says Thank You to Don Cragun For This Useful Post:
Yoda (01-25-2013)
Sponsored Links
Closed 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
awk command to print only selected rows in a particular column specified by column name ks_reddy Shell Programming and Scripting 4 09-01-2012 11:32 AM
command for converting 4 column data to 1 column rpf Shell Programming and Scripting 5 12-12-2011 10:40 AM
Rename a header column by adding another column entry to the header column name URGENT!! Vavad Shell Programming and Scripting 4 08-05-2011 12:35 PM
Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 rydz00 Shell Programming and Scripting 7 11-09-2010 10:28 AM
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM



All times are GMT -4. The time now is 08:56 PM.