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
New to scripting, Need help newbie22102 Shell Programming and Scripting 2 02-25-2008 11:08 AM
scripting help james94538 Shell Programming and Scripting 2 02-08-2008 07:36 PM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 08:12 AM
scripting guru's pls help me with scripting on AIX thatiprashant Shell Programming and Scripting 1 01-20-2006 07:58 PM
KSH Scripting dstaller Shell Programming and Scripting 1 11-16-2005 02:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 10-10-2008
nani123 nani123 is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
Need scripting Help

Dear Scripting experts,
Request to guide me in moving column values to rows
Example:
File 1:

1,a,b,c,d,e,f,g,h
2,f,g,h,i,l

Output file
1,a,b,c
1,d,e,f
1,g,h
2,f,g,h
2,i,l


Actually I tried with using awk and sed but unfortunately i couldn't get the resultant.

Regards
nani
  #2 (permalink)  
Old 10-10-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by nani123 View Post
Dear Scripting experts,
Request to guide me in moving column values to rows

Your example doesn't move columns to rows; it splits lines.

Here's a shell solution.
Code:
IFS=,
set -f
while read line
do
  set -- $line
  if [ $# -le 4 ]
  then
    print "$line"
  else
   a=$1
   shift
   while [ $# -gt 3 ]
   do
     printf "%s," "$a" "$1" "$2"
     printf "%s\n" "$3"
     shift 3
   done
   printf "%s," "$a"
   while [ $# -gt 1 ]
   do
     printf "%s," "$1"
     shift
   done
   printf "%s\n" "$1"
  fi
done < "$FILE"
If the file is large, it will be slow, and you should convert the script to awk using the same logic.
  #3 (permalink)  
Old 10-10-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
$ cat file
1,a,b,c,d,e,f,g,h
2,f,g,h,i,l
$ awk -F, '{
  printf$1FS;c=0
  for(i=2;i<=NF;i++)
    printf$i (++c%3?i==NF?RS:FS:RS$1FS)
}' file
1,a,b,c
1,d,e,f
1,g,h
2,f,g,h
2,i,l
  #4 (permalink)  
Old 10-10-2008
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
Here's a fairly simple Python approach (tested and working).

Code:
$ cat nani123.py 
#!/usr/bin/env python

input = open("temp.txt", 'r')

for line in input:

    line = line.rstrip()
    line = line.split(',')
    num = line[0]
    data = line[1:]

    while data:
        print num + "," + ','.join(data[:3])
        data = data[3:]
Test:

Code:
$ cat temp.txt 
1,a,b,c,d,e,f,g,h
2,f,g,h,i,l


$ python nani123.py 
1,a,b,c
1,d,e,f
1,g,h
2,f,g,h
2,i,l
  #5 (permalink)  
Old 10-10-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Perl is missing:

Code:
perl -F, -pae's/((?:(?:.*?),){3}[^,]*)/$1\n$F[0]/g' file
  #6 (permalink)  
Old 10-10-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
And now, my made up space language
Code:
snurdo <inputfile> {zort blip -f[] $1} | glop | ('{prattle ???}',"Take me to your leader")
Disclaimer: The preceding message was an attempt at humour
  #7 (permalink)  
Old 10-10-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by avronius View Post
And now, my made up space language
Code:
snurdo <inputfile> {zort blip -f[] $1} | glop | ('{prattle ???}',"Take me to your leader")
Disclaimer: The preceding message was an attempt at humour
That was missing too!
Sponsored Links
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 03:16 PM.


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