views:

159

answers:

2

If I have a list of data in a text file seperated by a new line, is there a way to append something to the start, then the data, then append something else then the data again?

EG a field X would become new X = X;

Can you do this with bash or sed or just unix tools like cut?

EDIT:

I am trying to get "ITEM_SITE_ID :{$row['ITEM_SITE_ID']}
" .

I am using this line awk '{ print "\""$1 " {:$row['$1']}
" }'

And I get this "ITEM_SITE_ID {:$row[]}

What have I missed?

+1  A: 

Awk is often the perfect tool for tasks like this. For your specific example:

awk '{ print "new " $1 " = " $1 ";" }'
Vebjorn Ljosa
+2  A: 

I think the problem is your single quotes are not properly escaped, which is actually impossible to do.

With sed:

sed "s/\(.*\)/\1 = \1;/"

Or in your case:

sed "s/\(.*\)/\"\1 :{\$row['\1']}\"/"

And with bash:

while read line
do
    echo "\"$line :{\$row['$line']}\""
done

And actually you can do it in awk using bashes $'' strings:

awk $'{ print "\\"" $1 " :{$row[\'" $1 "\']}\\"" }'
Steve Baker