Last active
March 21, 2016 13:32
-
-
Save rohitprajapati/3112ac067b3dca1a3dd4 to your computer and use it in GitHub Desktop.
Transpose from column to rows. To divide into batches. Example usage to avoid error "argument list too long": awk -f ~/codebase/my_files/scripts/test.awk test2.csv | while read line ; do echo "my_command: " $line ; done
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: Transpose from column to rows. | |
# For e.g. | |
# | |
# Input 1 - | |
# abc_1 | |
# abc_2 | |
# abc_3 | |
# abc_4 | |
# abc_5 | |
# abc_6 | |
# abc_7 | |
# abc_8 | |
# abc_9 | |
# | |
# Input 2 - | |
# abc_1,abc_2 | |
# abc_3,abc_4 | |
# abc_5,abc_6 | |
# abc_7,abc_8 | |
# abc_9 | |
# | |
# Output for both - | |
# abc_1,abc_2,abc_3,abc_4 | |
# abc_5,abc_6,abc_7,abc_8 | |
# abc_9 | |
# Example usage: awk -f ~/codebase/my_files/scripts/test.awk test2.csv | while read line ; do echo "my_command: " $line ; done | |
BEGIN { | |
FS=","; | |
OFS=","; | |
N=5 | |
count=0; | |
} | |
{ | |
for (i = 1 ; i <= NF ; i++) { | |
count++; | |
if (count == 1) { | |
printf("%s",$i); | |
} else if ((count - 1) % N == 0) { | |
printf("\n%s",$i); | |
} else { | |
printf("%s%s",OFS,$i); | |
} | |
} | |
} | |
END { | |
print "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment