Record length check fails due to '\' character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Record length check fails due to '\' character
# 1  
Old 03-27-2013
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.

Code:
Example records:

abcdefghij
abcd\efghij
abcdefghi

Expected output is:
10
11
9

Code:
But the output returned is 
10
10
9

Please help me on this issue.
# 2  
Old 03-27-2013
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

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

which gives you the desired output.
# 3  
Old 03-27-2013
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).
Code:
$ cat temp.x
abcdefghij
abcd\efghij
abcdefghij
$ wc -l temp.x
3 temp.x

Code:
$ cat temp.x
abcdefghij
$ wc -c temp.x
11 temp.x

Code:
$ 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.
# 4  
Old 03-27-2013
Sorry i used wc -c but if the record contains \ it gives 1 less than the actual record length.
# 5  
Old 03-27-2013
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.
# 6  
Old 03-27-2013
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.
Code:
$ 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.
# 7  
Old 03-27-2013
I pass a file as Input, read line by line and check for the length of a record using
HTML Code:
wc -c
command.
HTML Code:
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.
Code:
If any record contains \ character then wc -c displays the count 1 less 
HTML Code:
ex 249
and my script fails

Code:
Note:
I should not replace the \ character.

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh fails due to argument position.

I have a constraint to follow organization policy. So i do not have much liberty. ssh -i /opt/nonprod user1@hostone -t bash works while ssh -i /opt/nonprod -t bash user1@hostone fails How can I get this to work when I am enforced to put -t bash before the user@hostname ? Will share debug... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

3. Shell Programming and Scripting

Check for length which exceeds specified length in a line

Hi, I have a issue, I need to loop through a comma delimited file and check for the length which exceeds specified length , if Yes truncate the string. But my problem is , I do not have to check for all the fields and the field lenght is not same for all the fields. For ex: Say my line... (9 Replies)
Discussion started by: rashmisb
9 Replies

4. OS X (Apple)

Compiling fails due to space in path to home folder

I seem to have issues compiling software and I think I've narrowed it down to something having to do with having a space in the path name to my Home folder (which contains "Macintosh HD"). The reason I think this is shown here: $ echo $HOME /Volumes/Macintosh HD/Users/Tom $ cd $HOME -sh:... (7 Replies)
Discussion started by: tdgrant1
7 Replies

5. Shell Programming and Scripting

Add character based on record length

All, I can't seem to find exactly what I'm looking for, and haven't had any luck patching things together. I need to look through a file, and if the record length is not 874, then add 'E' in position 778. Your help is greatly appreciated. (4 Replies)
Discussion started by: CutNPaste
4 Replies

6. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

7. Shell Programming and Scripting

Check length of record

Hi, I have a problem, please help me, I have a flat file like this: P00000000088888888999999999 0000999903 000000000000000000 P00000000077777777000000000 0000999903 000000000000000000 P00000000044444444333333333 0000999903 00000000000000000079875 P00000000066666666111111111 0000999903 ... (5 Replies)
Discussion started by: DebianJ
5 Replies

8. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

9. Shell Programming and Scripting

Using Awk script to check length of a character

Hi All , I am trying to build a script using awk that checks columns of the înput file and displays message if the column length exceeds 35 char. i have tried the below code but it does not work properly (2 Replies)
Discussion started by: amit1_x
2 Replies
Login or Register to Ask a Question