Add text to each column [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
This question already has an answer here:
Formatted shell script output
3 answers
I have the following lines
3, 3, 100
4, 2, 50
8, 5, 80
.
.
.
and I want the following output
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
.
.
.
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
bash text-processing awk sed
marked as duplicate by l0b0, Archemar, Kiwy, Stephen Kitt, Timothy Martin Apr 5 at 0:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
3
down vote
favorite
This question already has an answer here:
Formatted shell script output
3 answers
I have the following lines
3, 3, 100
4, 2, 50
8, 5, 80
.
.
.
and I want the following output
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
.
.
.
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
bash text-processing awk sed
marked as duplicate by l0b0, Archemar, Kiwy, Stephen Kitt, Timothy Martin Apr 5 at 0:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
â Franky
Apr 3 at 3:00
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
This question already has an answer here:
Formatted shell script output
3 answers
I have the following lines
3, 3, 100
4, 2, 50
8, 5, 80
.
.
.
and I want the following output
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
.
.
.
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
bash text-processing awk sed
This question already has an answer here:
Formatted shell script output
3 answers
I have the following lines
3, 3, 100
4, 2, 50
8, 5, 80
.
.
.
and I want the following output
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
.
.
.
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
This question already has an answer here:
Formatted shell script output
3 answers
bash text-processing awk sed
edited Apr 3 at 6:21
Sundeep
6,9511826
6,9511826
asked Apr 3 at 2:46
Franky
385
385
marked as duplicate by l0b0, Archemar, Kiwy, Stephen Kitt, Timothy Martin Apr 5 at 0:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by l0b0, Archemar, Kiwy, Stephen Kitt, Timothy Martin Apr 5 at 0:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
â Franky
Apr 3 at 3:00
add a comment |Â
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
â Franky
Apr 3 at 3:00
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
â Franky
Apr 3 at 3:00
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
â Franky
Apr 3 at 3:00
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
12
down vote
accepted
awk is good for this kind of formatted input - formatted output:
awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
This is absolutely beautiful!
â Franky
Apr 4 at 19:27
add a comment |Â
up vote
3
down vote
A shell while read loop with printf:
while IFS=', ' read c1 c2 c3; do
printf 'line starts at %s and ends at %s with value %sn'
"$c1" "$c2" "$c3"
done <file
By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.
Output:
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
add a comment |Â
up vote
2
down vote
It turns out that there is -e option in sed
sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'
see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
â Sundeep
Apr 3 at 6:22
2
@Franky, steeldriver has better answer
â WashichawbachaW
Apr 3 at 7:21
@WashichawbachaW I do agree! It is a much better line
â Franky
Apr 4 at 19:27
add a comment |Â
up vote
0
down vote
There is a easy and fast way to do this in shell itself:-
# cat f
3, 3, 100
4, 2, 50
8, 5, 80
# cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
Hope it helps
2
Why not split it up in the read itself instead of doing the extra step of creating an array?
â Kusalananda
Apr 3 at 9:02
I hope you're not actually running this as rootâ¦
â Kevin
Apr 3 at 17:29
Oh yes, that is much better
â ananTgarg
Apr 4 at 11:45
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
awk is good for this kind of formatted input - formatted output:
awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
This is absolutely beautiful!
â Franky
Apr 4 at 19:27
add a comment |Â
up vote
12
down vote
accepted
awk is good for this kind of formatted input - formatted output:
awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
This is absolutely beautiful!
â Franky
Apr 4 at 19:27
add a comment |Â
up vote
12
down vote
accepted
up vote
12
down vote
accepted
awk is good for this kind of formatted input - formatted output:
awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
awk is good for this kind of formatted input - formatted output:
awk -F, 'printf("line starts at %d and ends at %d with value %dn", $1, $2, $3)' file
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
answered Apr 3 at 3:33
steeldriver
31.5k34978
31.5k34978
This is absolutely beautiful!
â Franky
Apr 4 at 19:27
add a comment |Â
This is absolutely beautiful!
â Franky
Apr 4 at 19:27
This is absolutely beautiful!
â Franky
Apr 4 at 19:27
This is absolutely beautiful!
â Franky
Apr 4 at 19:27
add a comment |Â
up vote
3
down vote
A shell while read loop with printf:
while IFS=', ' read c1 c2 c3; do
printf 'line starts at %s and ends at %s with value %sn'
"$c1" "$c2" "$c3"
done <file
By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.
Output:
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
add a comment |Â
up vote
3
down vote
A shell while read loop with printf:
while IFS=', ' read c1 c2 c3; do
printf 'line starts at %s and ends at %s with value %sn'
"$c1" "$c2" "$c3"
done <file
By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.
Output:
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
add a comment |Â
up vote
3
down vote
up vote
3
down vote
A shell while read loop with printf:
while IFS=', ' read c1 c2 c3; do
printf 'line starts at %s and ends at %s with value %sn'
"$c1" "$c2" "$c3"
done <file
By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.
Output:
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
A shell while read loop with printf:
while IFS=', ' read c1 c2 c3; do
printf 'line starts at %s and ends at %s with value %sn'
"$c1" "$c2" "$c3"
done <file
By setting the IFS variable to a space and a comma, the read command will use those characters as field delimiters.
Output:
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
answered Apr 3 at 10:26
Kusalananda
102k13201317
102k13201317
add a comment |Â
add a comment |Â
up vote
2
down vote
It turns out that there is -e option in sed
sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'
see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
â Sundeep
Apr 3 at 6:22
2
@Franky, steeldriver has better answer
â WashichawbachaW
Apr 3 at 7:21
@WashichawbachaW I do agree! It is a much better line
â Franky
Apr 4 at 19:27
add a comment |Â
up vote
2
down vote
It turns out that there is -e option in sed
sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'
see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
â Sundeep
Apr 3 at 6:22
2
@Franky, steeldriver has better answer
â WashichawbachaW
Apr 3 at 7:21
@WashichawbachaW I do agree! It is a much better line
â Franky
Apr 4 at 19:27
add a comment |Â
up vote
2
down vote
up vote
2
down vote
It turns out that there is -e option in sed
sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'
It turns out that there is -e option in sed
sed -e 's/^/line starts at /g' -e 's/, / and ends at /' -e 's/, / with value at /'
answered Apr 3 at 3:20
Franky
385
385
see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
â Sundeep
Apr 3 at 6:22
2
@Franky, steeldriver has better answer
â WashichawbachaW
Apr 3 at 7:21
@WashichawbachaW I do agree! It is a much better line
â Franky
Apr 4 at 19:27
add a comment |Â
see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
â Sundeep
Apr 3 at 6:22
2
@Franky, steeldriver has better answer
â WashichawbachaW
Apr 3 at 7:21
@WashichawbachaW I do agree! It is a much better line
â Franky
Apr 4 at 19:27
see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
â Sundeep
Apr 3 at 6:22
see also: gnu.org/software/sed/manual/sed.html#Multiple-commands-syntax
â Sundeep
Apr 3 at 6:22
2
2
@Franky, steeldriver has better answer
â WashichawbachaW
Apr 3 at 7:21
@Franky, steeldriver has better answer
â WashichawbachaW
Apr 3 at 7:21
@WashichawbachaW I do agree! It is a much better line
â Franky
Apr 4 at 19:27
@WashichawbachaW I do agree! It is a much better line
â Franky
Apr 4 at 19:27
add a comment |Â
up vote
0
down vote
There is a easy and fast way to do this in shell itself:-
# cat f
3, 3, 100
4, 2, 50
8, 5, 80
# cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
Hope it helps
2
Why not split it up in the read itself instead of doing the extra step of creating an array?
â Kusalananda
Apr 3 at 9:02
I hope you're not actually running this as rootâ¦
â Kevin
Apr 3 at 17:29
Oh yes, that is much better
â ananTgarg
Apr 4 at 11:45
add a comment |Â
up vote
0
down vote
There is a easy and fast way to do this in shell itself:-
# cat f
3, 3, 100
4, 2, 50
8, 5, 80
# cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
Hope it helps
2
Why not split it up in the read itself instead of doing the extra step of creating an array?
â Kusalananda
Apr 3 at 9:02
I hope you're not actually running this as rootâ¦
â Kevin
Apr 3 at 17:29
Oh yes, that is much better
â ananTgarg
Apr 4 at 11:45
add a comment |Â
up vote
0
down vote
up vote
0
down vote
There is a easy and fast way to do this in shell itself:-
# cat f
3, 3, 100
4, 2, 50
8, 5, 80
# cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
Hope it helps
There is a easy and fast way to do this in shell itself:-
# cat f
3, 3, 100
4, 2, 50
8, 5, 80
# cat f | while read line ; do IFS=", " array=($line) ; echo "line starts at $array[0] and ends at $array[1] with value $array[2]"; done
line starts at 3 and ends at 3 with value 100
line starts at 4 and ends at 2 with value 50
line starts at 8 and ends at 5 with value 80
Hope it helps
answered Apr 3 at 8:28
ananTgarg
304
304
2
Why not split it up in the read itself instead of doing the extra step of creating an array?
â Kusalananda
Apr 3 at 9:02
I hope you're not actually running this as rootâ¦
â Kevin
Apr 3 at 17:29
Oh yes, that is much better
â ananTgarg
Apr 4 at 11:45
add a comment |Â
2
Why not split it up in the read itself instead of doing the extra step of creating an array?
â Kusalananda
Apr 3 at 9:02
I hope you're not actually running this as rootâ¦
â Kevin
Apr 3 at 17:29
Oh yes, that is much better
â ananTgarg
Apr 4 at 11:45
2
2
Why not split it up in the read itself instead of doing the extra step of creating an array?
â Kusalananda
Apr 3 at 9:02
Why not split it up in the read itself instead of doing the extra step of creating an array?
â Kusalananda
Apr 3 at 9:02
I hope you're not actually running this as rootâ¦
â Kevin
Apr 3 at 17:29
I hope you're not actually running this as rootâ¦
â Kevin
Apr 3 at 17:29
Oh yes, that is much better
â ananTgarg
Apr 4 at 11:45
Oh yes, that is much better
â ananTgarg
Apr 4 at 11:45
add a comment |Â
I tried the following: sed 's/^/line starts at /' then applying this command for the output: sed 's/, / and ends at /' then applying this command for the output sed 's/, / with value /'. Is there any way to do it in a single line?
â Franky
Apr 3 at 3:00