The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 04:05 AM
kernel parameter values mhbd UNIX for Dummies Questions & Answers 1 03-21-2008 04:50 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 06:57 AM
How to pass one parameter from one column to another column in AWK prasanta jena Shell Programming and Scripting 1 07-30-2007 03:08 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 10:13 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-07-2006
Registered User
 

Join Date: May 2005
Posts: 69
Capturing the values of column in one parameter

Hi,

I am trying to capture the values of a column in a parameter..here is what I wanted to do...

1,2,3,4
2,3,4,1
3,4,1,2
4,1,2,3

is there any way that I could get the values of column values into one parameter??

Here is what I want...
COL1=1,2,3,4
COL2=2,3,4,1
COL3=3,4,1,2
COL4=,4,1,2,3

I am just taking an example of 1234 and original file will not have same values in both rows and columns....

Is it possible??

Thanks,
Reply With Quote
Forum Sponsor
  #2  
Old 06-08-2006
Registered User
 

Join Date: Feb 2006
Posts: 52
Hi,

I think firstly your query is not clear ...
Pls specify that u have one coulmn or multiple column seperated by comma

If its the first case then ...ie only one column then here's the solution

put yr inputs in a file called file1

for var in `cat file1`
do
kvar=`echo $var`
......
.....
.....
done


where ..... symbolize what operation u want to do with var ie kvar ie 1,2,3,4

thks

Aparna
Reply With Quote
  #3  
Old 06-09-2006
Registered User
 

Join Date: May 2005
Posts: 69
Thanks aparna,

I may have not stated it correctly but this is what I want, I have a file where I have values seperated by a comma and have 4 rows and 4 columns (4x4 matrix)
cat file1
a,b,c,d
e,f,g,h
i,j,k,l
m,n,o,p

all I want is...parameters
COL1=a,e,i,m
COL2=b,f,j,n
COL3=c,g,k,o
COL4=d,h,l,p

any suggestions here...?
Reply With Quote
  #4  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
This might help


#! /bin/bash
row=""
row4=""
row3=""
row2=""
count=0

while read line; do

row=${row}`echo ${line%%,*}` # extracts first character from the string
row4=${row4}`echo ${line##*,}`# extracts last character

echo $line > temp # saves read line to a temporary file
row2=${row2}`awk -F"," '{ print $2 }' temp` # extracts second character
row3=${row3}`awk -F"," '{ print $3 }' temp` # extracts third character

count=`expr $count + 1` # Adds the ","

if [ $count -lt 4 ]; then
row=${row}","
row2=${row2}","
row3=${row3}","
row4=${row4}","
fi
done<$1 # Read data from file
rm -f filez # Removes the old output file if it exists

# Add all the rows to the new output file.
echo $row >> filez
echo $row2 >> filez
echo $row3 >> filez
echo $row4 >> filez

rm -f temp # delete the temporary file

echo "Contents of the new File:" # display the contents of the output file
cat filez

May not be the best solution possible but it works
Reply With Quote
  #5  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Location: Bangalore, India
Posts: 8
the following code will give u the first row (ie a,e,i,m)

Code:
gawk -F ',' 'BEGIN {col=""}{ col=col $1 "," ; }END {print substr ( col,1,length ( col ) -1 ) }' file1
change '$1' as '$2' to get second row & '$3' to get third row.
Reply With Quote
  #6  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
$ cat filex
a,b,c,d
e,f,g,h
i,j,k,l
m,n,o,p

#Using his logic, it can be done in 1 line as

$ awk -F "," 'BEGIN { col="";col2="";col3="";col4="" }{col=col $1; col2=col2 $2; col3=col3 $3; col4=col4 $4 }END { print col"\n"col2"\n"col3"\n"col4 }' filex > filez
# It might seem complex at first but infact is a simple assignment and reassignment of variables and then printing them out to the desired file.

$ cat filez
aeim
bfjn
cgko
dhlp

Thanks prajith, I learnt something new about awk.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0