Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add prefix to each column with sed

Tags:

sed

I have a header and what to add the prefix "f4_" to each column.

I tried this:

echo -e  p.value"\t"beta | sed "s/\</f4_/g"

and got this:

f4_p.f4_value   f4_beta

I want this:

f4_p.value   f4_beta

I think the special character . in the header is messing this up but I am not sure how to fix

like image 674
curious Avatar asked Oct 25 '25 10:10

curious


1 Answers

You can try this awk:

printf 'p.value\tbeta\n' |
awk 'BEGIN{FS=OFS="\t"}
{for (i=1; i<=NF; ++i) ($i != "") && $i = "f4_" $i} 1'

f4_p.value  f4_beta

For a no loop solution suggest you to use this gnu-awk solutiom:

printf 'p.value\tbeta\n' |
awk -v RS='[\t\n]+' 'NF{$0 = "f4_" $0} {ORS=RT} 1'

f4_p.value  f4_beta
like image 192
anubhava Avatar answered Oct 27 '25 06:10

anubhava