Record length check fails due to '\' character

When I check the length for the records in the file, it does not give me the correct value. I used wc -l command.

Example records:

abcdefghij
abcd\efghij
abcdefghi

Expected output is:
10
11
9

But the output returned is 
10
10
9

Please help me on this issue.

When you run wc -l , it gives you the number of lines in the file, not the record lengths. What other command did you use to get the record lengths?

---------- Post updated at 10:25 ---------- Previous update was at 10:24 ----------

One way to get the record lengths would be

$ awk '{ print length($0) }' inputfile
10
11
9

which gives you the desired output.

I don't think anything fails because of \ character.

You want to use wc -c to count the number of characters in a record. wc -l counts the number of records (number of lines).

$ cat temp.x
abcdefghij
abcd\efghij
abcdefghij
$ wc -l temp.x
3 temp.x
$ cat temp.x
abcdefghij
$ wc -c temp.x
11 temp.x
$ cat temp.x
abcd\efghij
$ wc -c temp.x
12 temp.x

Keep in mind that if just one character on the line, the result of wc -c is still more than one, because it counts the end of line marker.

Hope this helps.

Sorry i used wc -c but if the record contains \ it gives 1 less than the actual record length.

Sorry, wc -c doesn't print the record lengths either, it prints the file size in bytes.

Anyway, the awk-approach should solve your problem.

Hi,
how do you pass the records to wc? Btw. wc -l counts lines...
I think the \e sequence is translated by something else before wc sees it.

$ echo -ne 'abcd\efghij' |wc -m
10
$ echo -n 'abcd\efghij' |wc -m
11

In the first example echo interpretes the backslash escapes, in the second it does not.

I pass a file as Input, read line by line and check for the length of a record using

wc -c

command.

ex: 250

.

If the record is less than the given length the script fails.

If all the records in the file is of same length then my input file is moved to a required path.

My file as some thousands of records.

If any record contains \ character then wc -c displays the count 1 less ex 249 and my script fails
Note:
I should not replace the \ character.

Without actually seeing the script I can only guess, but I think you lose your one byte somewhere when reading the file line by line.
Also I'd be careful with the -c option of wc. It counts bytes, not characters. Multibyte characters may cause your script to fail:

$ echo -ne '�bc' |wc -c
4
$ echo -ne '�bc' |wc -m
3

It would help if you could post more details.

wc -c is not ignoring or missing the \ backslash character.
wc -c just counts the number of bytes that it sees.

If your byte count is too small, it's because some sequence such as \n or \t is getting changed to one byte BEFORE wc -c sees it. Here is an example showing one way this can happen. There are doubtless other ways:

$ echo "\t"            # First \, then t, then newline
\t
$ echo "\t" | wc -c
3
$ echo -e "\t"         # TAB character, then newline

$ echo -e "\t" | wc -c
2

Most likely what you are seeing as \e (or backslash whatever) when you view the file is not a backslash at all - it's your shell command or editor displaying an unprintable character (which is indeed 1 byte long).

Try using man od (posix) on the file to see what the actual character is.

Amrutha24, if you would like prompt and accurate assistance, do yourself a favor and post the exact commands that you are using. It is ridiculous that this thread has grown to two pages, that you have posted in it multiple times, and that you still have yet to share your code.

If you're using read , you're using it incorrectly. However, due to your inadequate posts, that is only a guess.

Regards,
Alister