Skip to content

Instantly share code, notes, and snippets.

@rohitprajapati
Last active March 21, 2016 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohitprajapati/3112ac067b3dca1a3dd4 to your computer and use it in GitHub Desktop.
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
# 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