buzzin buzzin - 6 months ago 12
Bash Question

Inserting a list into a string

I think I wasn't clear in my last question, in case this looks familiar to someone.

#!/bin/bash
STATE= xml sel -t -v "/PATH/TO/VALUE/@state" ListOfStates.xml


This produces a list that looks kind of like this

TEXAS
COLORADO
NEW YORK
FLORIDA
CALIFORNIA


I want to take each line and insert it into echo command, or a .txt, like so:

FileName="STATE_${STATE}"
echo $FileName


The output looks like this:

TEXAS
COLORADO
NEW YORK
FLORIDA
CALIFORNIA
STATE_


Instead my output should look like this


STATE_TEXAS
STATE_COLORADO
STATE_NEW YORK
STATE_FLORIDA
STATE_CALIFORNIA

Answer
xml sel -t -v "/PATH/TO/VALUE/@state" ListOfStates.xml | sed 's/^/STATE_/'

and if you want to store it into an array:

IFS=$'\n' arr=($(xml sel -t -v "/PATH/TO/VALUE/@state" ListOfStates.xml | sed 's/^/STATE_/'))
Comments