The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 07:05 AM
kernel parameter values mhbd UNIX for Dummies Questions & Answers 1 03-21-2008 07: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 09: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 06:08 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 01:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-07-2006
mgirinath mgirinath is offline
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,
  #2 (permalink)  
Old 06-09-2006
er_aparna er_aparna is offline
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
  #3 (permalink)  
Old 06-09-2006
mgirinath mgirinath is offline
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...?
  #4 (permalink)  
Old 06-14-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
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 (permalink)  
Old 06-14-2006
prajith78 prajith78 is offline
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.
  #6 (permalink)  
Old 06-14-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
$ 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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0