Solved

Getting length of output in shell

Posted on 2010-11-24
9
439 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.

696 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