Solved

Getting length of output in shell

Posted on 2010-11-24
9
436 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Fine Tune your automatic Updates for Ubuntu / Debian
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

777 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