• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Length of Variable in Shell

Hi,
I am grepping Data from log file and assigning it to a Variable. Now I need to know length or count of that Variable. Can anyone let me know how??

I mentioned Count because I am grepping the filenames into Var . For example when we print that Variable data would be:

FILE1
FILE2
FILE3..

0
new_perl_user
Asked:
new_perl_user
  • 3
  • 3
  • 2
1 Solution
 
Gerwin Jansen, EE MVETopic Advisor Commented:
There is a few ways you can do this. Let's say the variable that contains your grepped data is called result. You can determine the length of result like this:

user@VMMINT01 ~/tmp $ result="this is the result"
user@VMMINT01 ~/tmp $ echo $result
this is the result
user@VMMINT01 ~/tmp $ expr length "$result"
18
user@VMMINT01 ~/tmp $ echo ${#result}
18

Open in new window

0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Or assign the lengt to a variable:

user@VMMINT01 ~/tmp $ result_len=`expr length "$result"`
user@VMMINT01 ~/tmp $ echo $result_len
18

Open in new window

0
 
new_perl_userAuthor Commented:
I tried the above suggestion, but it is printing the wrong count/length.
 There are only 3 filenames in the file but it is showing 5.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AnacreoCommented:
Are you working in Perl or some shell scripting language like KSH, SH, CSH?

For most scripting languages you could do something like:

$length=`echo $var | wc -l`
(count lines)

$length=`echo $var | wc -c`
(count characters)
0
 
new_perl_userAuthor Commented:
Hi

Below is the script I am using.. I want to get the count into a variable so that I can populate that into an email subject showing the count of file names

#!/bin/bash

 i=$(egrep -v "^$|[/]$" /usr/home/Test/Files/test_`date +%Y%m%d`_mhl.txt);
echo "$i"\n;
$length ='echo $i|wc -l';

0
 
AnacreoCommented:
Try it this way:
 
i=$(egrep -v "^$|[/]$" /usr/home/Test/Files/test_`date +%Y%m%d`_mhl.txt);
echo "$i";
length=$(echo $i|wc -l);
echo $length;

Open in new window

0
 
new_perl_userAuthor Commented:
Above one is giving the count of file it is grepping instead of count of Filenames within the file.

So right now it is grepping "1" file to read  so the output is 1.
0
 
AnacreoCommented:
You may need to be a bit clearer for me then in your question...

Can you provide the contents of /usr/home/Test/Files/test_`date...

Maybe:

Maybe you could try running below script and giving the output?

If the desired results aren't right try changing first line to "#!/bin/bash -x" and run and send the output.

#!/bin/bash

totallength=0
for i in $(egrep -v "^$|[/]$" /usr/home/Test/Files/test_`date +%Y%m%d`_mhl.txt);
do
	length=$(echo $i|wc -l);
	totallength=$(($length+$totallength));
        echo "$i is $legnth lines long."
done
echo "Total length was $totallength."

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now