Home > OS >  Passing multiple lines in a text file to a single file
Passing multiple lines in a text file to a single file

Time:07-16

Please can someone guide on how to achieve the below? thanks

#!/bin/bash
filename='schemas.txt'
while read line; do
echo "-n $line"
done < $filename

that spits out

-n jon
-n match

and i want it as. How do i achieve this?

-n jon -n match 

CodePudding user response:

You can do this in bash using sed | tr:

# prefix -n before each line from input file and 
# converts line break to space
out="$(sed 's/^/-n /' schemas.txt | tr '\n' ' ')"
echo "${out% }" # removes trailing space

-n jon -n match

CodePudding user response:

With bash:

printf -- "-n %s " $(< "$filename")

Output with a trailing space as shown in your example:

-n jon -n match 
  •  Tags:  
  • bash
  • Related