Calculate byte size of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate byte size of string
# 1  
Old 03-17-2012
Calculate byte size of string

Hi,

I have task to merge multiple XML's to one big XML.
In doing this i have to calculate the byte size of each XML which i will be recieving as a string from a code and append all the recieved XML's to single file.

The reason being the byte size and the offset will help me to extract only the XML i require from the large file.

How can i do this ?

Thanks,
Chetan
# 2  
Old 03-17-2012
wc

Hi,
Try this,
Code:
echo $str | wc -c

Cheers,
Ranga:-)
# 3  
Old 03-17-2012
Better use double quotes or spaces get squeezed and use:
Code:
printf "%s" "$str" | wc -c

or the newline introduced by echo gets counted too..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-19-2012
Hi ,
Thanks for the reply.

The reason i want the byte size is to know how many bytes an XML can take.
In case the XML will have " i will have to escape it.

In that case will the byte size include the "\" which i'm using to escape "?

Thanks.
# 5  
Old 03-19-2012
You don't need to escape " for the wc -c command. If the " is needed in the XML then the \ is part of the XML and it should be counted, no?
# 6  
Old 03-19-2012
Hi,

I need the " inside the XML so \ will also be counted to give the byte size then right?
Currently in the XML we recieve \ is not a part of it so we have to \ explicitly.


Thanks,
Chetan.C
# 7  
Old 03-19-2012
Why do you need to escape the double quote? If it is inside the string than that should not be necessary, I would think. Have you tried it without the \ . What error message did you get and could you show us what you were trying?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Ls directory size reporting byte size instead of file count

I have been searching both on Unix.com and Google and have not been able to find the answer to my question. I think it is partly because I can't come up with the right search terms. Recently, my virtual server switched storage devices and I think the problem may be related to that change.... (2 Replies)
Discussion started by: jmgibby
2 Replies

2. Programming

[c] How to calculate size of the file from size of the buffer?

Hi, Can I find size of the file from size of the buffer written? nbECRITS = fwrite(strstr(data->buffer, ";") + 1, sizeof(char), (data->buffsize) - LEN_NOM_FIC, fic_sortie); Thank You :) (1 Reply)
Discussion started by: ezee
1 Replies

3. Shell Programming and Scripting

Shell script to calculate the size of files

Dear all, Please help me to write a script that can calculate the size of files. For example: I have a directory which contain thousands of files. I need to know the size of files that their name begin with abc_123 Thank all!! (4 Replies)
Discussion started by: hainguyen1402
4 Replies

4. Shell Programming and Scripting

Preserve byte size of fields while pasting it to other file in unix

Preserve byte size of fields while pasting it to other file Hello I want to set two fields in a text file to be of size 20. how to do it using unix ? eg: ABC.txt Name | City I want Name and City both to be of size 20. Also If I am pasting it in other file the byte size should be... (1 Reply)
Discussion started by: dashing201
1 Replies

5. UNIX for Dummies Questions & Answers

Preserve byte size of fields while pasting it to other file

Hello I want to set two fields in a text file to be of size 20. how to do it using unix ? eg: ABC.txt Name | City I want Name and City both to be of size 20. Also If I am pasting it in other file the byte size should be preserved.i.e. If I want to append content of ABC.txt to other... (0 Replies)
Discussion started by: dashing201
0 Replies

6. Shell Programming and Scripting

zero byte with String

My program would be creating a file, incase of non data from database it would only able to produce 'END, logically it would be file created by oracle in unix. this file is showing zero byte but actully there are 4 char in file.This is not required, therefore what is command line should be... (0 Replies)
Discussion started by: u263066
0 Replies

7. UNIX for Dummies Questions & Answers

calculate directory size by year of file

I need to calcualte the size of a directory by the year the files in that directory were created . For example the script will sum up, by year, the number of blocks for that directory and its' subdirectories for files created / accessed in that year. I need a report that would look like... (11 Replies)
Discussion started by: igidttam
11 Replies

8. Shell Programming and Scripting

calculate size of some files

Hi, 1-I want to calculate the size of all files which are generated during last month in a directory. How can I do that ? Of cours, I find them by : $ls -l | grep jun but how to calculate the sum of their size ? 2- the same but for all files generated last month and before that. many thanks... (11 Replies)
Discussion started by: big123456
11 Replies

9. Shell Programming and Scripting

How to calculate file's size in directory and subdirectory

Hi, I have written one script to calculate total space of all file in one directory, ignoring subdirectory, it works fine. Now, I've been trying to calculate all files which includes files in any subdirectories. I use recursive function to do this, but it can work only if there is only one... (4 Replies)
Discussion started by: KLL
4 Replies

10. UNIX for Dummies Questions & Answers

how to calculate the file size?

HI, Can somebody please tell me how many bytes make a KB & MB. ThANKS. Rooh.:( (3 Replies)
Discussion started by: rooh
3 Replies
Login or Register to Ask a Question