Home > Back-end >  How to convert a multiple line input into single/multiple line output | BASH
How to convert a multiple line input into single/multiple line output | BASH

Time:04-28

What I really want

Input:

<int> 
45
</int>
<string>
Name
</string>

output:

<int> 45 </int>
<string> Name </string>

Here is what I've come up w/ so far(needs tweaking) for the desired output above:

printf "please enter some values \n"
readarray -t arr 

echo "" ${arr[@]} && cat >> input.txt

my output: <int> 45 </int> <string> Name </string>

CodePudding user response:

Using printf

$ printf '%s %s %s\n' $(cat input.txt)
<int> 45 </int>
<string> Name </string>

CodePudding user response:

When you want to combine each 3 lines, you can use

cat inputfile | paste -d " " - - -

When you can have values with more than one line, you might use

tr -d '\n' < inputfile | sed 's#</[^>]*>#&\n#g'

Or with awk :

awk '/<\/.*>/ {print; next} {printf $0}' inputfile
  • Related