Capturing the values of column in one parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing the values of column in one parameter
# 1  
Old 06-07-2006
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,
# 2  
Old 06-09-2006
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
# 3  
Old 06-09-2006
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...?
# 4  
Old 06-14-2006
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
# 5  
Old 06-14-2006
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.
# 6  
Old 06-14-2006
$ 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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script parameter with several values

Hi, On ksh I have to write a script tha I will run like this: ./myscript.ksh -param1 val1 val2 val3 .. -param2 xxx -param3 hgf Param1 will be an array: param1= ( val1 val2 val3 ..... ) How can I do that? It is not a homework!! Thank you. (1 Reply)
Discussion started by: big123456
1 Replies

2. Shell Programming and Scripting

Capturing column headers in an array

Hello, I am processing a tab delimited text file and need to grab all of the column headers in an array. The input looks like, num Name PCA_A1 PCA_A2 PCA_A3 0 compound_00 -3.5054 -1.1207 -2.4372 1 compound_01 -2.2641 0.4287 ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Capturing multiple values from user input

Hello, I need to capture multiple values from user input and do not know how to do it. I'm writing a script to delete old files, but want to give the option to keep some by asking the user. This is what my output looks like... Old files to be deleted... 1 file1 2 file2 Then this bit of... (3 Replies)
Discussion started by: jrymer
3 Replies

5. Shell Programming and Scripting

Converting odd values to even values(or vice-versa) located in a column

Hello All, I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B. Note that my data has some other columns(not shown here) too (around 100) after col2. Tool,Data A,1 A,3 A,5 .... so on B,2 B,4 .... ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

7. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

8. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

10. Shell Programming and Scripting

How to pass one parameter from one column to another column in AWK

I am having scenario where input file is containing text like below ----------------------------------- Name,x.y.z.i.j.k,p.q.r.s.t.k,y.d.f.u.g.h,e.r.t.yu.t.u,...... Place,Bangalore,hyderabad,Chennai,Delhi,............ I need to read and put it in file in a column structure ... (1 Reply)
Discussion started by: prasanta jena
1 Replies
Login or Register to Ask a Question