0

I have a variable named concat (result of a paste command between 2 variables). Concat is a multi-line variable containing strings:

strawberrie red
banana yellow
apple green

Result of typeset -p concat :

declare --concat ="strawberrie    red
banana    yellow
apple     green"

I need to append at the beginning of each line of this variable another variable named type :

fruit

The result i need to get after an echo the variable array (edit : it is not an array but i called it like that)

fruit strawberrie red
fruit banana yellow
fruit apple green

I tried with sed but it didn't worked :

echo "$concat" | sed "/^/$type"

error returned is sed command not found (even if sed work)

array=$(sed '/^/$type')

just don't work

And i find many other solutions on internet but none of them worked for me (i don't know how to use sed properly so i guess i may had written not adapted options).

Which command i need to use to do that (and could you explain me the option you use in it so i dont paste it without learning) ?

Edit : This is the code i used to append the variable fruit to the variable concat and save in in the variable array (a string too) :

array=$(sed "s/^/$type /g" <<< "$concat")
  • based on your description it's not clear (to me) if `${concat}` is a variable with a multi-line value or if it's a multi-entry array; please update the question with the output from `typeset -p concat` – markp-fuso Nov 02 '20 at 16:42
  • Your sed is okay, you just forgot the comand `s`: `echo "$concat" | sed "s/^/$type /"` should work. – jas Nov 02 '20 at 16:46
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Sabito stands with Ukraine Feb 15 '21 at 16:52

2 Answers2

0

Awk is an option:

awk -v var="fruit" '{ print var" "$0 }' <<< "$varble"

With the example data in the variable - varble, redirect the variable back into awk passing a variable (var) containing the prefix text to print. Then print the variable var along with each line of the redirected variable varble.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
0

Assumptions:

  • concat is a variable containing a multi-line set of data
  • OP wants to preface each line of concat with "${type}" and then store each line as a separate entry an array named array

Our data:

$ concat="strawberrie red
banana yellow
apple green"

Preface each line with type=fruit + (space):

$ type=fruit
$ sed "s/^/${type} /g" <<< "${concat}"
fruit strawberrie red
fruit banana yellow
fruit apple green

Store results in array array:

$ IFS=$'\n' read -d '' -r -a array < <(sed "s/^/${type} /g" <<< "${concat}")
$ typeset -p array
declare -a array=([0]="fruit strawberrie red" [1]="fruit banana yellow" [2]="fruit apple green")
$ for i in "${!array[@]}"
do
    echo "${i} - ${array[${i}]}"
done

0 - fruit strawberrie red
1 - fruit banana yellow
2 - fruit apple green
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • Thanks it worked ! For sed : Why did you use { } between the variable ? What does s/ do before the ^ ? And what does the /g ? –  Nov 03 '20 at 07:31
  • re: braces - see [When do we need curly braces around shell variables?](https://stackoverflow.com/q/8748831/7366100) and [$VAR vs ${VAR} ...](https://unix.stackexchange.com/q/4899/234539); re: `sed s/g` - see [sed - the s command](https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html) – markp-fuso Nov 03 '20 at 13:48