Iterate over files and search for a pattern

This script iterates over all XML files in the current directory, searches for the pattern "Name=" and extracts the XML value. Fnally renames the original XML file with the recovered value.

Script

#!/bin/bash

for filename in ./*.xml; do
    echo Checking $filename
    aux1=`grep -oE ' Name=\"(.*)\"' < $filename | cut -f 2 -d '"'`
    cp $filename $aux1.xml
done

echo Done!

Remarks

  • grep -oE searches ONLY for a pattern
  • grep -oE < $fileName is more efficient than cat $filename | grep -oE
  • See this StackOverflow question

# Perform a HTTP request with CURL and extract the status code

URL=http://www.google.es

HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" $URL)

if [ $HTTP_CODE -eq  200 ]
...