Help to find length of string avoiding trailing spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to find length of string avoiding trailing spaces
# 1  
Old 12-06-2012
Help to find length of string avoiding trailing spaces

Hi,
I have a record of length 200 bytes and values filled is only 100 bytes and remaining 100 spaces is occupied by spaces. In script wen i try to find the length of the entire record it should get as 200 not 100. i tried using length and wc -c but it doesnt work can anyone have any idea on this???
Tis was the code i tried:-
Code:
cat RPCSFILE.txt | grep '^D' | while read LINE
do
expr length '$LINE' | read LEN
if [$LEN -ne 200]; then 
echo "Incorrect line"
fi
done


Last edited by Pranaveen; 12-06-2012 at 08:35 AM.. Reason: missed to add file; mod: added code tags
# 2  
Old 12-06-2012
What about using awk..?

Code:
awk 'length($0) < 200' file

# 3  
Old 12-06-2012
In shell to preserve spaces, unset IFS, for example:
Code:
while IFS= read LINE


Depending on your type of input, you may also need to switch off backslash interpretation with the -r option:
Code:
while IFS= read -r LINE

# 4  
Old 12-06-2012
Code:
awk '/^D/ && length<200' RPCSFILE.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Fastest way to find the length of string in c

Hi all, We use strlen() fun provided by library to find the length of a string. Looking inside of it, it has some different mechanism to find the length of string. Normally, we scan the string byte by byte until the '\0' character. It takes a logn time to count length. The Library strlen()... (2 Replies)
Discussion started by: yogeshrl9072
2 Replies

2. Programming

How to find length of string and pass into char array in C?

Hi All I want to take a Hexadecimal number as input and i want to find lenth of the input and pass it to char s ( char s ). I have a program to convert hexadecial to binary but it is taking limited input but i want to return binary number based on input. How? (1 Reply)
Discussion started by: atharalikhan
1 Replies

3. Shell Programming and Scripting

[solved] Trailing spaces

Hello People How to check whether lines in a text file have trailing spaces or not and if a line have trailing spaces then how many trailing spaces line have? Regards ARvind kumar (5 Replies)
Discussion started by: arvindk.monu
5 Replies

4. Shell Programming and Scripting

How to add trailing spaces to have file with lines of the same length?

I have textfile (source.txt) with different length of lines in it. Can anybody help to compose a script under bash which would add suitable number of trailing spaces to the end of each line so that after the processing the each line would have the same (let's say 100 char) length? Output can be... (6 Replies)
Discussion started by: sameucho
6 Replies

5. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

6. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

7. Shell Programming and Scripting

find the string length in solaris

how to find the string length in solaris machine. (4 Replies)
Discussion started by: din_annauniv
4 Replies

8. Shell Programming and Scripting

Awk:Find length of string omitting quotes

Hi , I have a file named "sample" having the data as follows. "663005487","USD",0,1,"NR" If i give like a=`awk -F ',' '{printf length($2)}' sample` (Trying to find length of second field)I should get the output for the above as 3 (Omitting double quotes) not 5. How to do this..... (2 Replies)
Discussion started by: jayakumarrt
2 Replies

9. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question