2

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.

Kusalananda
  • 333,661
wikisky
  • 123
  • @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 Oct 19 '18 at 08:33

4 Answers4

6

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:

Kusalananda
  • 333,661
2

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
RudiC
  • 8,969
1
sed 's/\(.*\)/-o \1:1/' file.txt | xargs /path/to/command

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

0

Input file:

line1
line2
line3

Command:

perl -lpe '$\=" ";$_="-o $_:1"' input.txt

Alternative command:

awk '{ORS=":1 ";print"-o "$0}' input.txt

Output (the same for both commands):

-o line1:1 -o line2:1 -o line3:1 
simlev
  • 1,495