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
paste each 10 lines of single column to several column nica Shell Programming and Scripting 4 01-22-2009 04:18 AM
converting .txt to comma delimeted file OSD Shell Programming and Scripting 1 09-16-2008 03:51 AM
Converting Binary decimal coded values to Ascii Values gaur.deepti UNIX for Advanced & Expert Users 3 04-02-2008 12:33 PM
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
Comma Seperated List of Values brap45 Shell Programming and Scripting 9 02-23-2006 05:12 AM

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 01-22-2009
nvuradi nvuradi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 23
Converting Column values to comma delimted single Row

I have a requirement in which i have to read a file which has multiple columns seperated by a pipe "|" from this i have to read each column values seperately and create a comma seperated row for the column and write to another file.

eg:

Input file:
ColA ColB
1 2
2 x
3 y
4 c
5 q

from this i have to create two filles, each having data as

file_colA:
1,2,3,4,5

file_colB:
2,x,y,c,q


i have created shell script using a for loop, which may take long time when i implement it in prod.

can any one suggest an approach using awk or sed?


thanks
  #2 (permalink)  
Old 01-22-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

Code:
file=/path/to/inputfile
numcols=2 ## adjust to taste
n=1
  IFS='
'
while [ $n -le $numcols ]
do
  {
    printf "%s," $( cut -d '|' -f "$n" "$file" )
    echo
  } > file_col$n
  n=$(( $n + 1 ))
done
  #3 (permalink)  
Old 01-22-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
it doesn't look like your 'input' file has '|' separated fields.
does the 'ColA' and 'ColB' headers actually exist in the 'input' file?
  #4 (permalink)  
Old 01-22-2009
nvuradi nvuradi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 23
Quote:
Originally Posted by vgersh99 View Post
it doesn't look like your 'input' file has '|' separated fields.
does the 'ColA' and 'ColB' headers actually exist in the 'input' file?


sorry for the wrong file
Input file:
ColA ColB
1|2
2|x
3|y
4|c
5|q

and it doesnt have column header.

thanks johnson for the approach, but it will try to read the file line by line, which i want to avoid. since in production i may have millions of records and this may cause a bottleneck.
  #5 (permalink)  
Old 01-22-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by nvuradi View Post
thanks johnson for the approach, but it will try to read the file line by line, which i want to avoid. since in production i may have millions of records and this may cause a bottleneck.
how else would you transpose a matrix?
  #6 (permalink)  
Old 01-23-2009
laknar laknar is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 55
i have single column which is starting with same string(many number of rows)
i have to convert each into a single row.how can i do that?

laknar
std
mes
23
55
laknar
isd
phone no
address
amount
99

I have to convert above like below.

laknar|std|mes|23|55
laknar|isd|phone no|address|amount|99
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 07:39 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