Sed

From Blue-IT.org Wiki

Revision as of 09:15, 15 February 2012 by Apos (talk | contribs)

Save search and replace

search="$(printf "%s\n" "src=\"${SUBDIR}" | sed 's/[][\.*^$/]/\\&/g')"
replace="$(printf "%s\n" "src=\"${ANOTHER_DIR}/${SUBDIR}" | sed 's/[][\.*^$/]/\\&/g')"
sed "s/${search}/${replace}/g" "${DOC}"

Cut a certain passage out of a file

Let's assume we have the following sample text:

<h1 class = title>
 Lorem ipsum
</h1> 
<div id=content>
  <div id=abstract>
   Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
   sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, 
   sed diam voluptua. 
  </div>
 At vero eos et accusam et justo duo dolores et ea rebum. 
 Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 
 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
 sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
 sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. 
 Stet clita kasd gubergren, 
 no sea takimata sanctus est Lorem ipsum dolor sit amet.
</div>

The next snippet searches for the first (!) - and only the first - occurence of the Expression "FIRST_TEXT_FRAGMENT" and then, from that line on to the first occourence of "LAST_TEXT_FRAGMENT"

ARTICLE="${1}"

cat "${ARTICLE}" > tmp1
LENGTH=$(wc "${ARTICLE}" | awk '{print $1}')
LENGTH=$(expr ${LENGTH} - 1)
echo "Length: ${LENGTH}"

FIRST_TEXT_FRAGMENT="<div id=abstract>"
LAST_TEXT_FRAGMENT="</div>"
 
BEGIN_CUT=$(cat tmp1 | fgrep -n --max-count=1 "${FIRST_TEXT_FRAGMENT}" | cut -d':' -f1)
BEGIN_CUT=$(expr ${BEGIN_CUT})
echo ${BEGIN_CUT}

END_CUT=$(cat tmp1 | sed -e "${BEGIN_CUT},${LENGTH}d" | fgrep -n --max-count=1 "${LAST_TEXT_FRAGMENT}" | cut -d':' -f1)
END_CUT=$(expr ${END_CUT} - 1 + ${BEGIN_CUT})
echo ${END_CUT}

cat tmp1 | sed -e "${BEGIN_CUT},${END_CUT}d" > tmp2