Operating System - HP-UX
1753970 Members
7087 Online
108811 Solutions
New Discussion юеВ

Re: Adding and removing control characters from a document

 
SOLVED
Go to solution
Amy Pondolfino
Occasional Contributor

Adding and removing control characters from a document

My first 'real' script is working nicely now, but I would really like to improve it. I need to remove a single form feed character (^L) from a file and add a single form feed character to the end. I found several ways to add or remove control characters throughout the document but the only way I found to just add or remove a single character just at the beginning or end involves an external file, FEEDFILE, which contains a single form feed character.

What works currently:

if [[ $(head -c -n 1 ${OUTPUTFILE}) = $(head -c -n 1 ${FEEDFILE}) ]]
then
#remove form feed character from the beginning
mv ${OUTPUTFILE} ${TEMPFILE}
tail -c +2 ${TEMPFILE} > ${OUTPUTFILE}
rm ${TEMPFILE}
fi
if [[ $(tail -n 1 ${NEWOUT}) != $(head -c -n 1 ${FEEDFILE}) ]]
then
#add a form feed character to the end
mv ${NEWOUT} ${TEMPFILE}
cat ${TEMPFILE} ${FEEDFILE} > ${NEWOUT}
rm ${TEMPFILE}
fi

These methods remove all the ^M's, not just one:

sed 's/^L/^g' ${OUTPUTFILE} > ${TEMPFILE}

tr -d "\14" < ${INPUTFILE} > ${TEMPFILE}

perl -p -i -e 's/^L//g' ${INPUTFILE}

With awk I can remove the first form feed but I don't want to do that unless it's the first character of the file:

awk '/\014/&&f<1{sub("\014","");1' ${OUTPUTFILE} > ${TEMPFILE}

Ideas appreciated!!!
OneontaChildrensMuseum.blogspot.com
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Adding and removing control characters from a document

Hi Amy:

If I understand your requirement, try this:

# perl -0777 -pi.old -e 's/^\014//;s/$/\014/' file

Regards!

...JRF...
OldSchool
Honored Contributor

Re: Adding and removing control characters from a document

and in awk:

$ cat awk.scr
==============================

BEGIN {
ff="\014"
}

{
if (NR == 1)
{
sub(ff,"");
}
print $0;

}


END{
print ff;
}

=======================================

awk -f awk.scr >

OldSchool
Honored Contributor
Solution

Re: Adding and removing control characters from a document

with sed:

as a "one-liner"

sed "1s/^L//1 ; $a^L" yourfile > your_mod_file


or

$ cat sed.scr
=================
1s/^L//1
$a^L


sed -f sed.scr > your_mod_file

gnu variants also support -i (in-place) option as I recall
OldSchool
Honored Contributor

Re: Adding and removing control characters from a document

one wonders if any of the above proved useful?
Amy Pondolfino
Occasional Contributor

Re: Adding and removing control characters from a document

Indeed, very useful! Thanks to everyone who replied! I love having all these options, and I'm still playing around with them a bit (there were some other deadlines that came up). I especially like the perl code, and I'm planning to learn more about perl in general.
OneontaChildrensMuseum.blogspot.com
Amy Pondolfino
Occasional Contributor

Re: Adding and removing control characters from a document

Thanks again for the invaluable help with my script!
OneontaChildrensMuseum.blogspot.com