Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting length of output in shell

Posted on 2010-11-24
9
437 Views
Last Modified: 2013-12-26
Hi all,

I need a command in shell, which will return the length of the output as well as the actual output.

So, for example, if I run "date" command, it returns something like:

$date
Wed Nov 24 22:46:53 IST 2010

What I need is, some manipulation to this, so that I get output like:

$....
29
Wed Nov 24 22:46:53 IST 2010

Here 29 is the length of the output string (Wed Nov 24 22:46:53 IST 2010).

I can do it this way:

$date | wc -c ; date

But this runs "date" command twice, which I want to avoid. I tried to use "tee" without success. Is there any way to do this?
0
Comment
Question by:Harisha M G
  • 6
  • 2
9 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 34204619
Hi,

date | awk '{printf "%d\n%s", length, $0}'

or simpler, all output in one line:

date | awk '{print length, $0}'

wmp
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 34204640
If you don't like "printf" and need the output in two lines nonetheless:

date | awk '{print length "\n" $0}'

Btw. awk gives length 28, because it doesn't count the terminating linefeed.
0
 
LVL 37

Author Comment

by:Harisha M G
ID: 34204673
Thanks woolmilkporc. Adding one shouldn't be a problem.

Is there any way to get it in two different lines?

I tried

date | awk '{print length"\n",$0}'

But it is prepending one extra space in second line:

28
 Wed Nov 24 23:04:17 IST 2010
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34204684
See the first solution in the first post and the one in the second post.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 34204705
Explanation:
"comma" between output field specifications stands for "insert a space" in awk, whereas a space stands for "concatenate".
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34204749
Just for fun - a silly version with "tee":

date | tee xxx.$$ | wc -c | tr -d " "; cat xxx.$$; rm xxx.$$

Adding one should not be a problem, indeed:

date | awk '{print length+1"\n",$0}'
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 34204771
Now I copied and pasted your version instead of mine. Sorry!

date | awk '{print length+1 "\n" $0}'

0
 
LVL 48

Expert Comment

by:Tintin
ID: 34209330
Here's an alternative solution with only one fork

d=$(date)
echo -e "${#d}\n$d"
0
 
LVL 37

Author Comment

by:Harisha M G
ID: 34210500
TinTin, that's around 45% faster!!

I can't assign points to you now, but thank you so much!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

856 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