
10-10-2008
|
|
Shell programmer, author
|
|
|
Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,310
|
|
Quote:
Originally Posted by nani123
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.
|