Split and concatenate (create command line arguments from input file)
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
How to concatenate text from file of lines in format:
line1
line2
...
to get results like
-o line1:1 -o line2:1 ...
I found solution how to concatenate with a separator like this:
ds=`cat list.txt`
$ds//$'n'/','
But I can't figure out how to add prefix to each entry.
linux shell-script split
New contributor
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
How to concatenate text from file of lines in format:
line1
line2
...
to get results like
-o line1:1 -o line2:1 ...
I found solution how to concatenate with a separator like this:
ds=`cat list.txt`
$ds//$'n'/','
But I can't figure out how to add prefix to each entry.
linux shell-script split
New contributor
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
For this task you should use sed or awk
– mrc02_kr
4 hours ago
@mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
– Kusalananda
2 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How to concatenate text from file of lines in format:
line1
line2
...
to get results like
-o line1:1 -o line2:1 ...
I found solution how to concatenate with a separator like this:
ds=`cat list.txt`
$ds//$'n'/','
But I can't figure out how to add prefix to each entry.
linux shell-script split
New contributor
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
How to concatenate text from file of lines in format:
line1
line2
...
to get results like
-o line1:1 -o line2:1 ...
I found solution how to concatenate with a separator like this:
ds=`cat list.txt`
$ds//$'n'/','
But I can't figure out how to add prefix to each entry.
linux shell-script split
linux shell-script split
New contributor
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 hours ago


Kusalananda
110k15214339
110k15214339
New contributor
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 5 hours ago
wikisky
82
82
New contributor
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
wikisky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
For this task you should use sed or awk
– mrc02_kr
4 hours ago
@mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
– Kusalananda
2 hours ago
add a comment |Â
2
For this task you should use sed or awk
– mrc02_kr
4 hours ago
@mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
– Kusalananda
2 hours ago
2
2
For this task you should use sed or awk
– mrc02_kr
4 hours ago
For this task you should use sed or awk
– mrc02_kr
4 hours ago
@mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
– Kusalananda
2 hours ago
@mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
– Kusalananda
2 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util
.
Here's a solution for /bin/sh
:
#!/bin/sh
listfile=$1
set --
while IFS= read -r line; do
set -- "$@" -o "$line:1"
done <$listfile
util "$@"
This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o
and LINE:1
where LINE
is the line read from the file.
After reading all the lines, it calls util
with the constructed list of command line arguments. By using "$@"
(with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.
With bash
and using a bash
array to hold the command line arguments that we create:
#!/bin/bash
listfile=$1
while IFS= read -r line; do
args+=( -o "$line:1" )
done <$listfile
util "$args[@]"
In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o
and each LINE:1
are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc.
, but this would have been interpreted as one single argument if used as util "$string"
and would have undergone word splitting and filename globbing if used as util $string
(this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).
Both scripts above would be used as
$ ./script.sh file
where script.sh
is the executable script file and file
is the input file name to read from.
Related:
- Understanding "IFS= read -r line"
add a comment |Â
up vote
1
down vote
sed 's/(.*)/-o 1:1/' file.txt | xargs
The sed
substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs
command takes those modified lines of text and concatenates them as appropriate for command line arguments
add a comment |Â
up vote
0
down vote
With a recent shell (e.g. bash
), try
mapfile -t TMP <file
TMP=($TMP[@]/%/:1)
echo $TMP[@]/#/-o
-o line1:1 -o line2:1 -o line3:1
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util
.
Here's a solution for /bin/sh
:
#!/bin/sh
listfile=$1
set --
while IFS= read -r line; do
set -- "$@" -o "$line:1"
done <$listfile
util "$@"
This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o
and LINE:1
where LINE
is the line read from the file.
After reading all the lines, it calls util
with the constructed list of command line arguments. By using "$@"
(with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.
With bash
and using a bash
array to hold the command line arguments that we create:
#!/bin/bash
listfile=$1
while IFS= read -r line; do
args+=( -o "$line:1" )
done <$listfile
util "$args[@]"
In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o
and each LINE:1
are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc.
, but this would have been interpreted as one single argument if used as util "$string"
and would have undergone word splitting and filename globbing if used as util $string
(this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).
Both scripts above would be used as
$ ./script.sh file
where script.sh
is the executable script file and file
is the input file name to read from.
Related:
- Understanding "IFS= read -r line"
add a comment |Â
up vote
3
down vote
accepted
This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util
.
Here's a solution for /bin/sh
:
#!/bin/sh
listfile=$1
set --
while IFS= read -r line; do
set -- "$@" -o "$line:1"
done <$listfile
util "$@"
This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o
and LINE:1
where LINE
is the line read from the file.
After reading all the lines, it calls util
with the constructed list of command line arguments. By using "$@"
(with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.
With bash
and using a bash
array to hold the command line arguments that we create:
#!/bin/bash
listfile=$1
while IFS= read -r line; do
args+=( -o "$line:1" )
done <$listfile
util "$args[@]"
In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o
and each LINE:1
are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc.
, but this would have been interpreted as one single argument if used as util "$string"
and would have undergone word splitting and filename globbing if used as util $string
(this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).
Both scripts above would be used as
$ ./script.sh file
where script.sh
is the executable script file and file
is the input file name to read from.
Related:
- Understanding "IFS= read -r line"
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util
.
Here's a solution for /bin/sh
:
#!/bin/sh
listfile=$1
set --
while IFS= read -r line; do
set -- "$@" -o "$line:1"
done <$listfile
util "$@"
This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o
and LINE:1
where LINE
is the line read from the file.
After reading all the lines, it calls util
with the constructed list of command line arguments. By using "$@"
(with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.
With bash
and using a bash
array to hold the command line arguments that we create:
#!/bin/bash
listfile=$1
while IFS= read -r line; do
args+=( -o "$line:1" )
done <$listfile
util "$args[@]"
In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o
and each LINE:1
are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc.
, but this would have been interpreted as one single argument if used as util "$string"
and would have undergone word splitting and filename globbing if used as util $string
(this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).
Both scripts above would be used as
$ ./script.sh file
where script.sh
is the executable script file and file
is the input file name to read from.
Related:
- Understanding "IFS= read -r line"
This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util
.
Here's a solution for /bin/sh
:
#!/bin/sh
listfile=$1
set --
while IFS= read -r line; do
set -- "$@" -o "$line:1"
done <$listfile
util "$@"
This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o
and LINE:1
where LINE
is the line read from the file.
After reading all the lines, it calls util
with the constructed list of command line arguments. By using "$@"
(with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.
With bash
and using a bash
array to hold the command line arguments that we create:
#!/bin/bash
listfile=$1
while IFS= read -r line; do
args+=( -o "$line:1" )
done <$listfile
util "$args[@]"
In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o
and each LINE:1
are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc.
, but this would have been interpreted as one single argument if used as util "$string"
and would have undergone word splitting and filename globbing if used as util $string
(this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).
Both scripts above would be used as
$ ./script.sh file
where script.sh
is the executable script file and file
is the input file name to read from.
Related:
- Understanding "IFS= read -r line"
edited 4 hours ago
answered 4 hours ago


Kusalananda
110k15214339
110k15214339
add a comment |Â
add a comment |Â
up vote
1
down vote
sed 's/(.*)/-o 1:1/' file.txt | xargs
The sed
substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs
command takes those modified lines of text and concatenates them as appropriate for command line arguments
add a comment |Â
up vote
1
down vote
sed 's/(.*)/-o 1:1/' file.txt | xargs
The sed
substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs
command takes those modified lines of text and concatenates them as appropriate for command line arguments
add a comment |Â
up vote
1
down vote
up vote
1
down vote
sed 's/(.*)/-o 1:1/' file.txt | xargs
The sed
substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs
command takes those modified lines of text and concatenates them as appropriate for command line arguments
sed 's/(.*)/-o 1:1/' file.txt | xargs
The sed
substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs
command takes those modified lines of text and concatenates them as appropriate for command line arguments
edited 15 mins ago
RudiC
2,242110
2,242110
answered 4 hours ago
diametralpitch
20414
20414
add a comment |Â
add a comment |Â
up vote
0
down vote
With a recent shell (e.g. bash
), try
mapfile -t TMP <file
TMP=($TMP[@]/%/:1)
echo $TMP[@]/#/-o
-o line1:1 -o line2:1 -o line3:1
add a comment |Â
up vote
0
down vote
With a recent shell (e.g. bash
), try
mapfile -t TMP <file
TMP=($TMP[@]/%/:1)
echo $TMP[@]/#/-o
-o line1:1 -o line2:1 -o line3:1
add a comment |Â
up vote
0
down vote
up vote
0
down vote
With a recent shell (e.g. bash
), try
mapfile -t TMP <file
TMP=($TMP[@]/%/:1)
echo $TMP[@]/#/-o
-o line1:1 -o line2:1 -o line3:1
With a recent shell (e.g. bash
), try
mapfile -t TMP <file
TMP=($TMP[@]/%/:1)
echo $TMP[@]/#/-o
-o line1:1 -o line2:1 -o line3:1
answered 4 mins ago
RudiC
2,242110
2,242110
add a comment |Â
add a comment |Â
wikisky is a new contributor. Be nice, and check out our Code of Conduct.
wikisky is a new contributor. Be nice, and check out our Code of Conduct.
wikisky is a new contributor. Be nice, and check out our Code of Conduct.
wikisky is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476478%2fsplit-and-concatenate-create-command-line-arguments-from-input-file%23new-answer', 'question_page');
);
Post as a guest
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
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
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
2
For this task you should use sed or awk
– mrc02_kr
4 hours ago
@mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
– Kusalananda
2 hours ago