sed editing

xoxomos
xoxomos used Ask the Experts™
on
I have a file that contains
aaaaa
bbbbb
aaaaa
eeeee
begin junk
1234
5678
9012
3456

end junk
eeee
llll
bbbb
qqqq
I want to look for junk.  Once i locate 'begin junk' i want to replace everything between begin junk and end junk with the contents of a file filex
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I don't thing you can do this with see only!
Write a shell script can handle it without any problem.

just read in the file, skill all the lines bewteen "begin junk" and "end junk", as soon as you see "end junk" cat the file you want to add in , then keep going.

This little script should work for you:


#/bin/ksh
# usage: scrpname INFILE ADDFILE OUTFILE

INFILE=$1
ADDFILE=$2
OUTFILE=$3

IFS="\012"    #read in the whole line
exec 0<$1

SKIP="n"

while read INLINE ; do

   if [[ ${INLINE} = "end junk" ]] ; then
       SKIP="n"
       cat ${ADDFILE} >> $OUTFILE     #Insert the file
   fi

   if [ "${INLINE}" != "begin junk"  -a  "$SKIP" = "n" ] ; then
      echo $INLINE >> $OUTFILE
   else
      SKIP="y"
   fi
done
exit
# END OF SCRIPT
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
perl -i -ne 'print unless /begin junk/../end junk/; print `cat filex` if /end junk/' file
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Commented:
Well if you do want an all sed solution try this

% cat x.sed
/^begin junk/,/^end junk/ {
        /^end junk/ {
                p
        }
        /^begin junk/ {
                p
                r in.txt
        }
        d
}

This will operate on text between the two junk delimiters:

   if the end tag is matched, print it
   if the begin tag is matched, print it, read the filex
   else delete the line

 
%  cat in.txt
----------------------------------
adfadfadf
afafdadfaf
afafaf
afafaf
afafaf
afafafafasfalk;jaf;lafd;kljaf;lj
---------------------------------
% cat src.txt
aaaaa
bbbbb
aaaaa
eeeee
begin junk
1234
5678
9012
3456

end junk
eeee
llll
bbbb
qqqq
%

When run with

    sed -f x.sed src.txt

gets the result

% sed -f x.sed src.txt
aaaaa
bbbbb
aaaaa
eeeee
begin junk
----------------------------------
adfadfadf
afafdadfaf
afafaf
afafaf
afafaf
afafafafasfalk;jaf;lafd;kljaf;lj
---------------------------------
end junk
llll
bbbb
qqqq
%

For fine points to sed scripting check the O'Reilly text, sed & awk, D.Dougherty
perl already done, here as gawk:

gawk '/begin junk/{s=1}/end junk/{s=0;while(getline<"filex"){print};next;}(s==0){print}' file
# .. or simple sed:
sed -ne '/end junk/{' -e 'r filex' -e n -e '}' -e '/begin junk/{' -e n -e '}' -e p file

Commented:
Hi,
this script do what you want.

#
NL=`wc -l $1|cut -c1-8`
BeginShunk=`grep -i -n "begin shunk" $1|cut -f1 -d:`
EndShunk=`grep -i -n "end shunk" $1|cut -f1 -d:`
TailLines=`expr $NL - $EndShunk + 1`
head -$BeginShunk $1 >HeadPart
tail -$TailLines $1 >TailPart
cat HeadPart $2 TailPart >$1
#

you must call it with two parameters, the first is your
"file" and second is your "filex".

Author

Commented:
Looks good!  I'll try it this afternoon.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial