user3139207
user3139207

Reputation: 121

Using a variable in replacing instead of a string in awk

I'm using this command in a bash script in order to replace the string "NOTHING_HERE" with "$EMAIL" if it fins the URL "$findURL".

The problem is that I don't know how to tell awk to use the value of the variable $EMAILS instead of using the stirng "$EMAIL".

awk -v RS="</Row>" '/'$findURL'/{sub(/NOTHING_HERE/,"$EMAIL")}1' ORS="</Row>" /home/pi/testJMC/JustLinksJMC2.xml | sed '$d'

Any ideas?

Thanks!

Edit: to provide sample input:

<Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s65" ss:HRef="http://www.mapeo-rse.info/promotor/fundaci%C3%B3n-ecolog%C3%AD-y-desarrollo-ecodes"><Data
      ss:Type="String">Fundación Ecología y Desarrollo (ECODES)</Data></Cell>
    <Cell><Data ss:Type="String">NOTHING_HERE</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s65" ss:HRef="http://www.mapeo-rse.info/promotor/fundaci%C3%B3n-iberoamericana-para-la-gesti%C3%B3n-de-la-calidad-fundibeq"><Data
      ss:Type="String">Fundación Iberoamericana para la Gestión de la Calidad (Fundibeq)</Data></Cell>
    <Cell><Data ss:Type="String">NOTHING_HERE</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s65" ss:HRef="http://www.mapeo-rse.info/promotor/fundaci%C3%B3n-interamericana-iaf"><Data
      ss:Type="String">Fundación Interamericana (IAF)</Data></Cell>
    <Cell><Data ss:Type="String">NOTHING_HERE</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s65" ss:HRef="http://www.mapeo-rse.info/promotor/fundaci%C3%B3n-nuevo-periodismo-iberoamericano-fnpi"><Data
      ss:Type="String">Fundación Nuevo Periodismo Iberoamericano (FNPI)</Data></Cell>
    <Cell><Data ss:Type="String">NOTHING_HERE</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s65" ss:HRef="http://www.mapeo-rse.info/promotor/fundaci%C3%B3n-para-el-desarrollo-sostenible-fundes"><Data
      ss:Type="String">Fundación para el Desarrollo Sostenible (FUNDES)</Data></Cell>
    <Cell><Data ss:Type="String">NOTHING_HERE</Data></Cell>
   </Row>

Upvotes: 1

Views: 83

Answers (2)

Tiago Lopo
Tiago Lopo

Reputation: 7959

You can do the same way you did for $findURL:

awk -v RS="</Row>" '/'$findURL'/{sub(/NOTHING_HERE/,'"$EMAIL"')}1' ORS="</Row>" /home/pi/testJMC/JustLinksJMC2.xml | sed '$d'

This should work but I couldn't test as you didn't provide a input snippet.

Upvotes: 0

anubhava
anubhava

Reputation: 785196

You need to awk's way of passing shell variable to awk using -v name=value syntax:

awk -v RS="</Row>" -v u="$findURL" -v email="$EMAIL" '$~u{sub(/NOTHING_HERE/, email)}1' ORS="</Row>" /home/pi/testJMC/JustLinksJMC2.xml | sed '$d'

Upvotes: 1

Related Questions