Tuesday, July 26, 2022

[SOLVED] Passing multiple lines in a text file to a single file

Issue

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 

Solution

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


Answered By - anubhava
Answer Checked By - Willingham (WPSolving Volunteer)