fork(1) download
  1. #!/bin/bash
  2.  
  3. # ideone boilerplate: run in temp dir
  4. t=$(mktemp -d -t ideone.XXXXXXXX) || exit
  5. trap 'rm -rf "$t"' ERR EXIT
  6. cd "$t"
  7.  
  8. touch 1.html 2.html
  9.  
  10. # create test files
  11. rm *.html
  12. echo '<html><body>' > 1.html
  13. echo '<div id="myID" style="display:none">1_1</div>' >> 1.html
  14. echo '<div id="myID" style="display:none">another</div>' >> 1.html
  15. echo '</body></html>' >> 1.html
  16. echo '<div id="myID" style="display:none">1_2</div>' > 2.html
  17. echo '<div id="myID" style="display:none">trouble</div>' > 'hard * problem .html'
  18.  
  19. # loop throught matching files
  20. while IFS= read -r l_line
  21. do
  22. # get file name using exact match -o
  23. l_name=$( echo "$l_line" | egrep -o ".*html" )
  24.  
  25. # get tag value using perl reg exp : values between >< ,
  26. # \K (?=) remove brackets so without them we will get simple reg exp ">.*<"
  27. # details https://u...content-available-to-author-only...e.com/questions/13466/can-grep-output-only-specified-groupings-that-match
  28. l_new_name="$( echo "$l_line" | grep -oP ">\K.*(?=<)" ).html"
  29.  
  30. # for echoing let use this variable
  31. l_cmd="mv -v $l_name $l_new_name"
  32.  
  33. # rename if file does not exist
  34. if [[ -f $l_new_name ]]; then
  35. echo "file $l_new_name already exists, no rename" >&2
  36. else
  37. echo "$l_cmd"
  38. # execute and inform if works
  39. eval $l_cmd || echo "ERROR " >&2
  40. fi
  41. done <<< $(grep -o '<div id="myID" style="display:none">.*</div>' ./*.html)
Success #stdin #stdout #stderr 0.03s 5540KB
stdin
Standard input is empty
stdout
mv -v ./1.html 1_1.html
renamed './1.html' -> '1_1.html'
mv -v ./1.html another.html
mv -v ./2.html 1_2.html
renamed './2.html' -> '1_2.html'
mv -v ./hard * problem .html trouble.html
stderr
mv: cannot stat './1.html': No such file or directory
ERROR 
mv: target 'trouble.html' is not a directory
ERROR