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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #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,310
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.