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,
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
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...?
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
the following code will give u the first row (ie a,e,i,m)
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.
$ 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.