[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Length of Variable in Shell

Posted on 2011-10-03
8
Medium Priority
?
297 Views
Last Modified: 2012-05-12
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
Comment
Question by:new_perl_user
  • 3
  • 3
  • 2
8 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 36906484
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 36906498
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
 

Author Comment

by:new_perl_user
ID: 36906987
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
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
LVL 4

Expert Comment

by:Anacreo
ID: 36907045
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
 

Author Comment

by:new_perl_user
ID: 36907098
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
 
LVL 4

Expert Comment

by:Anacreo
ID: 36907108
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
 

Author Comment

by:new_perl_user
ID: 36907145
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
 
LVL 4

Accepted Solution

by:
Anacreo earned 2000 total points
ID: 36908180
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question