Improving my AWK skills
up vote
4
down vote
favorite
This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations)
Could some one improve this script so I can see where I could improve?
Does some one want to show me some crazy bummed version of this?
Is setting variables in the BEGIN block which "runs every line !@#!#@!" how you are supposed to set a variable in AWK?
#!/usr/bin/awk -f
BEGIN{
FS=",";
recordsCursor=1;
number=0;
#spacer="_";
#copies=3;
}
{
while(number++ <=copies){
while(recordsCursor <= NF){
if(recordsCursor==1){
printf($recordsCursor);
printf(spacer);
printf("%.3d,",number);
}else if(NF != recordsCursor){
printf("%s," , $recordsCursor);
}else{
printf("%s" , $recordsCursor);
}
recordsCursor++;
}
recordsCursor=1;
print "";
}
print "";
number=0;
}
Command: ./test.awk -v copies=3 -v spacer=_ input
Given input:
madison_Leaderboard_728x90,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
Correct output:
madison_Leaderboard_728x90_001,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_002,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_003,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_004,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250_001,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_002,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_003,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_004,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155_001,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_002,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_003,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_004,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
text-processing scripting awk
add a comment |
up vote
4
down vote
favorite
This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations)
Could some one improve this script so I can see where I could improve?
Does some one want to show me some crazy bummed version of this?
Is setting variables in the BEGIN block which "runs every line !@#!#@!" how you are supposed to set a variable in AWK?
#!/usr/bin/awk -f
BEGIN{
FS=",";
recordsCursor=1;
number=0;
#spacer="_";
#copies=3;
}
{
while(number++ <=copies){
while(recordsCursor <= NF){
if(recordsCursor==1){
printf($recordsCursor);
printf(spacer);
printf("%.3d,",number);
}else if(NF != recordsCursor){
printf("%s," , $recordsCursor);
}else{
printf("%s" , $recordsCursor);
}
recordsCursor++;
}
recordsCursor=1;
print "";
}
print "";
number=0;
}
Command: ./test.awk -v copies=3 -v spacer=_ input
Given input:
madison_Leaderboard_728x90,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
Correct output:
madison_Leaderboard_728x90_001,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_002,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_003,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_004,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250_001,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_002,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_003,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_004,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155_001,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_002,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_003,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_004,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
text-processing scripting awk
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations)
Could some one improve this script so I can see where I could improve?
Does some one want to show me some crazy bummed version of this?
Is setting variables in the BEGIN block which "runs every line !@#!#@!" how you are supposed to set a variable in AWK?
#!/usr/bin/awk -f
BEGIN{
FS=",";
recordsCursor=1;
number=0;
#spacer="_";
#copies=3;
}
{
while(number++ <=copies){
while(recordsCursor <= NF){
if(recordsCursor==1){
printf($recordsCursor);
printf(spacer);
printf("%.3d,",number);
}else if(NF != recordsCursor){
printf("%s," , $recordsCursor);
}else{
printf("%s" , $recordsCursor);
}
recordsCursor++;
}
recordsCursor=1;
print "";
}
print "";
number=0;
}
Command: ./test.awk -v copies=3 -v spacer=_ input
Given input:
madison_Leaderboard_728x90,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
Correct output:
madison_Leaderboard_728x90_001,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_002,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_003,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_004,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250_001,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_002,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_003,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_004,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155_001,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_002,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_003,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_004,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
text-processing scripting awk
This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations)
Could some one improve this script so I can see where I could improve?
Does some one want to show me some crazy bummed version of this?
Is setting variables in the BEGIN block which "runs every line !@#!#@!" how you are supposed to set a variable in AWK?
#!/usr/bin/awk -f
BEGIN{
FS=",";
recordsCursor=1;
number=0;
#spacer="_";
#copies=3;
}
{
while(number++ <=copies){
while(recordsCursor <= NF){
if(recordsCursor==1){
printf($recordsCursor);
printf(spacer);
printf("%.3d,",number);
}else if(NF != recordsCursor){
printf("%s," , $recordsCursor);
}else{
printf("%s" , $recordsCursor);
}
recordsCursor++;
}
recordsCursor=1;
print "";
}
print "";
number=0;
}
Command: ./test.awk -v copies=3 -v spacer=_ input
Given input:
madison_Leaderboard_728x90,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
Correct output:
madison_Leaderboard_728x90_001,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_002,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_003,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_004,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250_001,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_002,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_003,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_004,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155_001,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_002,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_003,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_004,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
text-processing scripting awk
text-processing scripting awk
edited Nov 25 at 23:58
Rui F Ribeiro
38.3k1477127
38.3k1477127
asked Sep 3 '11 at 18:06
Prospero
1,78562545
1,78562545
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
awk -v copies=3 -v spacer=_ '
BEGIN {OFS=FS=","}
{
field1 = $1
for (i=1; i <= copies+1; i++) {
$1 = sprintf("%s%s%03d", field1, spacer, i)
print
}
print ""
}
'
This takes advantage of awk recalculating $0 if any of the fields change value.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
awk -v copies=3 -v spacer=_ '
BEGIN {OFS=FS=","}
{
field1 = $1
for (i=1; i <= copies+1; i++) {
$1 = sprintf("%s%s%03d", field1, spacer, i)
print
}
print ""
}
'
This takes advantage of awk recalculating $0 if any of the fields change value.
add a comment |
up vote
6
down vote
accepted
awk -v copies=3 -v spacer=_ '
BEGIN {OFS=FS=","}
{
field1 = $1
for (i=1; i <= copies+1; i++) {
$1 = sprintf("%s%s%03d", field1, spacer, i)
print
}
print ""
}
'
This takes advantage of awk recalculating $0 if any of the fields change value.
add a comment |
up vote
6
down vote
accepted
up vote
6
down vote
accepted
awk -v copies=3 -v spacer=_ '
BEGIN {OFS=FS=","}
{
field1 = $1
for (i=1; i <= copies+1; i++) {
$1 = sprintf("%s%s%03d", field1, spacer, i)
print
}
print ""
}
'
This takes advantage of awk recalculating $0 if any of the fields change value.
awk -v copies=3 -v spacer=_ '
BEGIN {OFS=FS=","}
{
field1 = $1
for (i=1; i <= copies+1; i++) {
$1 = sprintf("%s%s%03d", field1, spacer, i)
print
}
print ""
}
'
This takes advantage of awk recalculating $0 if any of the fields change value.
answered Sep 4 '11 at 2:09
glenn jackman
49.6k569106
49.6k569106
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f20058%2fimproving-my-awk-skills%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown