The UNIX and Linux Forums  

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 10:08 AM
scripting help james94538 Shell Programming and Scripting 2 02-08-2008 06: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 06:58 PM
KSH Scripting dstaller Shell Programming and Scripting 1 11-16-2005 01:30 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 10-10-2008
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-10-2008
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 1,973
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.
Reply With Quote
  #3 (permalink)  
Old 10-10-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,453
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
Reply With Quote
  #4 (permalink)  
Old 10-10-2008
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
Reply With Quote
  #5 (permalink)  
Old 10-10-2008
radoulov's Avatar
addict
 

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

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

Join Date: Apr 2008
Location: Calgary
Posts: 264
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
Reply With Quote
  #7 (permalink)  
Old 10-10-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,453
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!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
None

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 07:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66