Computing.Net > Forums > Unix > How to compare form feed?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to compare form feed?

Reply to Message Icon

Name: nelsmy
Date: April 2, 2005 at 10:29:17 Pacific
OS: redhat
CPU/Ram: pentium, 512mb
Comment:

I would like to know how do i compare the last line in a file is it equal to form feed using shell script?
Below is my script but not work.

if [ `tail -n 1 $1` -eq "\f" ] then .....

so i change it to below which ff is a filejust with a line which contain form feed

if [ `tail -n 1 $1` ] -eq [ `tail -n 1 ff` ]then ...




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 3, 2005 at 16:39:06 Pacific
Reply:

Unfortunately, the syntax you are trying above doesn't work with bash or ksh. However, this does:

#!/bin/bash

if [[ -z $(tail -n 1 $1| tr -d '\014') ]]
then
echo "file ends with the Form Feed "
fi

The octal representation for a form feed, control-L, is \014. The above script deletes all the form feeds of the last line. if the resulting string is null, -z, or zero length, then that line had only form feeds.

I hope that explanation made sense.

Regards,

Nails


0

Response Number 2
Name: nelsmy
Date: April 3, 2005 at 17:04:28 Pacific
Reply:

Dear Nails,

Finally i get it...
Really thanks for you!!

But I really don't know why I can't compare the form feed with the \f as my script? it's because end of the file other than form feed control key, there have other control key?
So my compare fail?


Thanks for reply!!

Regards,
Nels


0

Response Number 3
Name: nails
Date: April 3, 2005 at 19:19:36 Pacific
Reply:

The form feed is a special character. In my experience the only way to compare special characters is use the method I just showed you or to compare the actual control character:

#!/bin/bash

if [[ $(tail -n 1 $1) = ^L ]]
then
echo "file ends with form feed"
fi

Note that ^L above is an actual control character. To create it in the vi editor, while in edit mode, press control-v and then control-L. I don't care for this method because you can't cut-and-paste easily.

Regards,

Nails


0

Response Number 4
Name: nelsmy
Date: April 3, 2005 at 22:11:16 Pacific
Reply:

Dear Nail...

Thanks for you...
Now i know where i'm stuck...
Thanks a lot...

BTW, may i know where can i get those
special character code?
It is same with ascii code?

Thanks a lot...

Regards,
Nels


0

Response Number 5
Name: nails
Date: April 3, 2005 at 23:30:46 Pacific
Reply:


About any compreshensive text will have it, and there's probably a million places on the web. Here's two:

http://www.bbsinc.com/iso8859.html
http://www.lookuptables.com/


0

Related Posts

See More



Response Number 6
Name: nelsmy
Date: April 3, 2005 at 23:55:18 Pacific
Reply:

Dear Nails

Really thanks for you help!!!

Thanks a lot!!

regards
Nels


0

Response Number 7
Name: Jim Boothe
Date: April 4, 2005 at 09:48:19 Pacific
Reply:

nels,

First, an if-statement should use -eq for numeric compares, but for string compares like this, it should use =.  And "then" is a separate command, so it should begin a new line (or separate with semicolon).

Secondly, the expression "\f" will be interpreted by the shell as just a simple f character.  You precede any character with backslash to make it lose its special meaning to the shell and be taken as just itself.  Example:

echo I won \$5.

The following code demonstrates this, and shows that the echo command will interpret "\f" as the formfeed character.

f1="f"
f2="\f"
eval f3="\f"
f4=$(echo "\f")

expr length $f1
expr length $f2
expr length $f3
expr length $f4

cat > f.out << !
f1=$f1
f2=$f2
f3=$f3
f4=$f4
!

echo '========'
cat f.out
echo '========'

1
2
1
1
========
f1=f
f2=\f
f3=f
f4=

========

f4 is a formfeed character, but it displays to the screen (as above) as a line break.  But if you read f.out into vi, you will see the ^L.

So with all that said, your command is actually close to what you need. Instead of:

if [ `tail -n 1 $1` -eq "\f" ] then .....

you can use this instead:

if [ "`tail -n 1 $1`" = `echo "\f"` ] ; then .....

or I prefer the ksh syntax:

if [ "$(tail -n 1 $1)" = $(echo "\f") ] ; then .....

And you should enclose the first argument in double-quotes as shown.  If the last line of the file is a null line, it would cause a syntax error unless the argument were in double-quotes.

And while you do not need your other approach of using the ff file, that approach would have worked had you used the proper syntax.  Instead of:

if [ `tail -n 1 $1` ] -eq [ `tail -n 1 ff` ]then ...

you could use this instead:

if [ "`tail -n 1 x1`" = `tail -n 1 ff` ] ; then ...

The solution provided by nails would return a positive indication if the last line were null.


0

Response Number 8
Name: nelsmy
Date: April 4, 2005 at 11:13:24 Pacific
Reply:

Dear Jim,

I really can't imagine that you provided me that full and clear explanation! Especially the exception case!

What I can say is really thanks for you help & explanation.

BTW, I still have a bit blur about the below code, can you give me some guideline or explaning??

What i know about the cat > f.out is trying to create a file of f.out, but how about the symbol !?? is it means echo or print???

cat > f.out << !
f1=$f1
f2=$f2
f3=$f3
f4=$f4
!

Second, what will cause a null at the end of a file?? is it a empty line??


Really thanks for your help!!!


Best Regards,
Nels



0

Response Number 9
Name: Jim Boothe
Date: April 4, 2005 at 12:43:23 Pacific
Reply:

You will sometimes see that referred to as a "here document". In a script, that is one way to feed lines into a command. It is common to use the exclamation mark, but any word will do, such as:

ftp -n myserver << ENDIT
user oracle mypasswd
prompt
cd /u01/prod/database
mget *
bye
ENDIT

The reason that I used cat to send the variables to either the screen or an output file is because in this demonstration I must avoid the echo command because it does its special interpretation and thus alters the results that I want to show - namely that the shell does not interpret "\f" as a formfeed.

And yes, an empty line is same as a null line - nothing on it, not even spaces. It's length is zero, whereas lines having one or more spaces would have non-zero length.

In the following, the first checks for empty (null) lines, the second checks for lines with no words on it, which would include empty lines and lines with spaces and tabs, and the third prints the length of each line:

awk 'length==0' myfile

awk 'NF==0' myfile

awk '{print length}' myfile


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to compare form feed?

how to compare www.computing.net/answers/unix/how-to-compare-/8201.html

how to do comparision www.computing.net/answers/unix/how-to-do-comparision/8208.html

How to compare two files in UNXI www.computing.net/answers/unix/how-to-compare-two-files-in-unxi/8278.html