Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing for special characters 3

Status
Not open for further replies.

unixdummy

MIS
Oct 8, 2004
6
US
Hi,
In ksh (hp-ux), we are concatenating several files before sending them along to another system.
Each line in the file is supposed to end with a cr/lf (\015\012). However we have an occasional file where the cr/lf is missing from the last line of the file. This causes the first line in the next file to be appended to that line, which is then ultimately truncated.
I would like to modify the script to concatenate the characters to the end of the file only if they are missing,
prior to the concatenation step.
I have a script where I compare the results of tail to the contents of a file that contains the correct characters, but this seems rather indirect. Is there a way to directly test for this? What is the syntax?

Here is the code that I have been playing with:
testfile="XYZ"
testfile1="$testfile"1

var=`tail -c -2 $testfile`
crlf=`tail -c -2 CRLFFILE`

if [[ $var = $crlf ]]
then
echo "$var = $crlf"
else
echo "$var not = $crlf"
cat $testfile CRLFFILE > $testfile1
fi
exit

Thanks,
Joan
 
Would it matter if you added a crlf to all files whether they have one or not? At least in that way you're not losing anything.
 
Judith:

You could check to see if each file's last two characters are a CR/LF. Here's my take:

#!/bin/ksh

myfile=yourfile
mychar=$(tail -2c $myfile) # get the file's last two characters

# determine if the last two characters are a CR/LF
if [[ -z $(echo "$mychar"|tr -d '\015\012') ]]
then
: # has a CR
else
sed '$ s/$/'"$(echo '\015')"'/' $myfile > mynewfile
fi
# end script.

Dealing with special characters in the shell is problematic. The above script echos the last two characters and attempts to delete them. If the result is null, nothing happens because there's a CR/LF.

Otherwise, the sed one-liner places a CR at the end of the last line of the file.

Regards,

Ed
 
Hi,
This little job got pushed aside, so I'm just testing this now. Your code works up to the SED part, which does not work due to the way SED works - if there is no newline at the end of the file, it does not read the last line. I'll plug away at this a little more.
Thanks!
Joan
 
Ken,
I think this would cause an extra blank line on the system that this file gets passed to. The test that Ed came up with works, so I don't have to test that...
Thanks,
Joan
 
Have you tried something like this ?
testfile="XYZ"
var=$(tail -c -2 $testfile)
[ -z $(echo "$var" | tr -d '\015\012') ] || echo "\015" >> $testfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi again,
Here's how I am appending the CR (the shell adds the newline):

echo '\015' > crlffile
cat $myfile crlffile > mynewfile

Thanks again,
Joan
 
This is an easy problem for Awk.

Assuming that your Awk ends each line with a linefeed but no carriage return, and assuming that you want to concatenate all files whose names start with "XYZ":

Code:
nawk '{sub(/\r/,""); print $0 "\r" }' XYZ* >bigfile
 
Hi futurelet, [2thumbsup]
Your awk program worked also, and also performs the concatenation, so is very handy for the job at hand.
Thanks!
Joan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top