Rostyslav Malenko
Rostyslav Malenko

Reputation: 579

How to output lines into one string separated by space (bash)

I have this construction in Bash

while read line
do
    echo $line | while read  a b c d e f g
    do
        if [ $e != "0" ]; then
        echo -en "|'$a'=;$g;$f;;"
        fi
    done
done < /tmp/res6

Which cat this text

mem_total_app_1 5034876928  + 384000 = 5035260928       mysqld
mem_total_app_2 2994008064  + 108532736 = 3102540800    /usr/sbin/amavi(91)
mem_total_app_3 648744960  + 103424 = 648848384 redis-server
mem_total_app_4 541454336  + 58354688 = 599809024       php-fpm7.2(26)

and output to this

|'mem_total_app_1'=;mysqld;5035260928;;
|'mem_total_app_2'=;/usr/sbin/amavi(91);3102540800;;
|'mem_total_app_3'=;redis-server;648848384;;
|'mem_total_app_4'=;php-fpm7.2(26);599809024;;

I would like to have the output in one string separated by space. Like this

|'mem_total_app_1'=;mysqld;5035260928;; 'mem_total_app_2'=;amavi(91);3102540800;; 'mem_total_app_3'=;redis-server;648848384;; 'mem_total_app_4'=;php-fpm7.2(26);599809024;;

Could somebody prompt me, please?

Upvotes: 3

Views: 7678

Answers (6)

anoopknr
anoopknr

Reputation: 3355

Try printf

The printf command provides a method to print preformatted text similar to the printf() system interface (C function). It's meant as successor for echo and has far more features and possibilities.

printf "|"
while read line
do
    echo $line | while read  a b c d e f g
    do
        if [ $e != "0" ]; then
        printf " '$a'=;$g;$f;;"
        fi
    done
done  < /tmp/res6

Output :-

| 'mem_total_app_1'=;mysqld;5035260928;; 'mem_total_app_2'=;/usr/sbin/amavi(91);3102540800;; 'mem_total_app_3'=;redis-server;648848384;; 'mem_total_app_4'=;php-fpm7.2(26);599809024;;

Upvotes: 1

halfelf
halfelf

Reputation: 10107

Simply cat and echo

cat input_filename | xargs echo

or with paste

paste -sd " " input_filename

Upvotes: 2

oliv
oliv

Reputation: 13259

If you can use awk:

awk 'BEGIN{OFS=";";ORS=";; ";printf "|"}{print "\047"$1"\047",$NF,$(NF-1)}' file

The script sets the output delimiter OFS and ORS such that it can print the content using ;. Note that \047 is the single quote character.

Upvotes: 0

liborm
liborm

Reputation: 2734

tr '\n' ' ' is a nice way, but if you want to avoid having a space at the end, use

<input_file paste -sd' '

This is handy eg for calculations with bc:

<one_number_on_line paste -sd+ | bc

gives you a sum of all lines (with one number per line).

Upvotes: 2

nixlarfs
nixlarfs

Reputation: 104

It might not be the prettiest way, but you could pipe the entire loop to tr like this:

{
    while read line
    do
        echo $line | while read  a b c d e f g
        do
            if [ $e != "0" ]; then
            echo -en "|'$a'=;$g;$f;;"
            fi
        done
    done < /tmp/res6
} | tr '\n' ' '

(Note spaces around mustache braces.)

Upvotes: 0

Davide Spataro
Davide Spataro

Reputation: 7482

Simply substitute newline with space:

tr '\n' ' ' < input_filename

Upvotes: 2

Related Questions