awk - Print all values with a suffix
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I want to use awk to print all values in a delimited string with a static suffix appended to each one.
Input:
stack,over,flow
Delimiter:
Comma
Output:
stack suffix, over suffix, flow suffix
text-processing awk csv
add a comment |
up vote
0
down vote
favorite
I want to use awk to print all values in a delimited string with a static suffix appended to each one.
Input:
stack,over,flow
Delimiter:
Comma
Output:
stack suffix, over suffix, flow suffix
text-processing awk csv
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to use awk to print all values in a delimited string with a static suffix appended to each one.
Input:
stack,over,flow
Delimiter:
Comma
Output:
stack suffix, over suffix, flow suffix
text-processing awk csv
I want to use awk to print all values in a delimited string with a static suffix appended to each one.
Input:
stack,over,flow
Delimiter:
Comma
Output:
stack suffix, over suffix, flow suffix
text-processing awk csv
text-processing awk csv
edited Nov 26 at 22:22
Jeff Schaller
37k1052121
37k1052121
asked Nov 26 at 21:24
SQLadmin
1095
1095
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
For the input
unix,and,linux
this would produce
prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix
The awk
program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk
programs through assignments to the pre
and suf
variables on the command line.
To add a space as prefix and the string suffix
(with an initial space) as suffix, you would use
awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
which produces
unix suffix, and suffix, linux suffix
Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? likestack suffix over suffix flow suffix
or replace with some other value
– SQLadmin
Nov 26 at 21:41
@SQLadmin Modify the output field separator (OFS
) on the command line. For example, remove-v OFS
completely to get space-separated output.
– Kusalananda
Nov 26 at 21:45
add a comment |
up vote
2
down vote
Another Awk variant - making use of the internal output separators to avoid an explicit loop:
awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
ex.
echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
stack suffix, over suffix, flow suffix
For space separated output you can append the default output field separator to the suffix
$ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
stack suffix over suffix flow suffix
add a comment |
up vote
1
down vote
awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix
awk -F ,
Sets the field separator as ,
print $1" suffix",
and so on and so forth prints a space and then suffix after the value
OFS=", "
Sets the output field separator as a comma with a space.
Output:
stack suffix, over suffix, flow suffix
Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
– SQLadmin
Nov 26 at 21:37
@SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
– Nasir Riley
Nov 26 at 21:54
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
For the input
unix,and,linux
this would produce
prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix
The awk
program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk
programs through assignments to the pre
and suf
variables on the command line.
To add a space as prefix and the string suffix
(with an initial space) as suffix, you would use
awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
which produces
unix suffix, and suffix, linux suffix
Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? likestack suffix over suffix flow suffix
or replace with some other value
– SQLadmin
Nov 26 at 21:41
@SQLadmin Modify the output field separator (OFS
) on the command line. For example, remove-v OFS
completely to get space-separated output.
– Kusalananda
Nov 26 at 21:45
add a comment |
up vote
2
down vote
accepted
awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
For the input
unix,and,linux
this would produce
prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix
The awk
program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk
programs through assignments to the pre
and suf
variables on the command line.
To add a space as prefix and the string suffix
(with an initial space) as suffix, you would use
awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
which produces
unix suffix, and suffix, linux suffix
Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? likestack suffix over suffix flow suffix
or replace with some other value
– SQLadmin
Nov 26 at 21:41
@SQLadmin Modify the output field separator (OFS
) on the command line. For example, remove-v OFS
completely to get space-separated output.
– Kusalananda
Nov 26 at 21:45
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
For the input
unix,and,linux
this would produce
prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix
The awk
program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk
programs through assignments to the pre
and suf
variables on the command line.
To add a space as prefix and the string suffix
(with an initial space) as suffix, you would use
awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
which produces
unix suffix, and suffix, linux suffix
awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
For the input
unix,and,linux
this would produce
prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix
The awk
program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk
programs through assignments to the pre
and suf
variables on the command line.
To add a space as prefix and the string suffix
(with an initial space) as suffix, you would use
awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix"
' for (i=1;i<=NF;++i) $i = pre $i suf; print '
which produces
unix suffix, and suffix, linux suffix
answered Nov 26 at 21:37
Kusalananda
118k16223364
118k16223364
Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? likestack suffix over suffix flow suffix
or replace with some other value
– SQLadmin
Nov 26 at 21:41
@SQLadmin Modify the output field separator (OFS
) on the command line. For example, remove-v OFS
completely to get space-separated output.
– Kusalananda
Nov 26 at 21:45
add a comment |
Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? likestack suffix over suffix flow suffix
or replace with some other value
– SQLadmin
Nov 26 at 21:41
@SQLadmin Modify the output field separator (OFS
) on the command line. For example, remove-v OFS
completely to get space-separated output.
– Kusalananda
Nov 26 at 21:45
Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like
stack suffix over suffix flow suffix
or replace with some other value– SQLadmin
Nov 26 at 21:41
Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like
stack suffix over suffix flow suffix
or replace with some other value– SQLadmin
Nov 26 at 21:41
@SQLadmin Modify the output field separator (
OFS
) on the command line. For example, remove -v OFS
completely to get space-separated output.– Kusalananda
Nov 26 at 21:45
@SQLadmin Modify the output field separator (
OFS
) on the command line. For example, remove -v OFS
completely to get space-separated output.– Kusalananda
Nov 26 at 21:45
add a comment |
up vote
2
down vote
Another Awk variant - making use of the internal output separators to avoid an explicit loop:
awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
ex.
echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
stack suffix, over suffix, flow suffix
For space separated output you can append the default output field separator to the suffix
$ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
stack suffix over suffix flow suffix
add a comment |
up vote
2
down vote
Another Awk variant - making use of the internal output separators to avoid an explicit loop:
awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
ex.
echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
stack suffix, over suffix, flow suffix
For space separated output you can append the default output field separator to the suffix
$ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
stack suffix over suffix flow suffix
add a comment |
up vote
2
down vote
up vote
2
down vote
Another Awk variant - making use of the internal output separators to avoid an explicit loop:
awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
ex.
echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
stack suffix, over suffix, flow suffix
For space separated output you can append the default output field separator to the suffix
$ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
stack suffix over suffix flow suffix
Another Awk variant - making use of the internal output separators to avoid an explicit loop:
awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
ex.
echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
stack suffix, over suffix, flow suffix
For space separated output you can append the default output field separator to the suffix
$ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
stack suffix over suffix flow suffix
answered Nov 26 at 22:14
steeldriver
33.8k34983
33.8k34983
add a comment |
add a comment |
up vote
1
down vote
awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix
awk -F ,
Sets the field separator as ,
print $1" suffix",
and so on and so forth prints a space and then suffix after the value
OFS=", "
Sets the output field separator as a comma with a space.
Output:
stack suffix, over suffix, flow suffix
Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
– SQLadmin
Nov 26 at 21:37
@SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
– Nasir Riley
Nov 26 at 21:54
add a comment |
up vote
1
down vote
awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix
awk -F ,
Sets the field separator as ,
print $1" suffix",
and so on and so forth prints a space and then suffix after the value
OFS=", "
Sets the output field separator as a comma with a space.
Output:
stack suffix, over suffix, flow suffix
Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
– SQLadmin
Nov 26 at 21:37
@SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
– Nasir Riley
Nov 26 at 21:54
add a comment |
up vote
1
down vote
up vote
1
down vote
awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix
awk -F ,
Sets the field separator as ,
print $1" suffix",
and so on and so forth prints a space and then suffix after the value
OFS=", "
Sets the output field separator as a comma with a space.
Output:
stack suffix, over suffix, flow suffix
awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix
awk -F ,
Sets the field separator as ,
print $1" suffix",
and so on and so forth prints a space and then suffix after the value
OFS=", "
Sets the output field separator as a comma with a space.
Output:
stack suffix, over suffix, flow suffix
answered Nov 26 at 21:35
Nasir Riley
2,231239
2,231239
Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
– SQLadmin
Nov 26 at 21:37
@SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
– Nasir Riley
Nov 26 at 21:54
add a comment |
Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
– SQLadmin
Nov 26 at 21:37
@SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
– Nasir Riley
Nov 26 at 21:54
Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
– SQLadmin
Nov 26 at 21:37
Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
– SQLadmin
Nov 26 at 21:37
@SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
– Nasir Riley
Nov 26 at 21:54
@SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
– Nasir Riley
Nov 26 at 21:54
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%2f484303%2fawk-print-all-values-with-a-suffix%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