Solved

Getting length of output in shell

Posted on 2010-11-24
9
435 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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34204684
See the first solution in the first post and the one in the second post.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Suggested Solutions

Title # Comments Views Activity
Determine Who is Runnig my Bash Shell Script 4 64
nagios monitor 3 44
route 2 traffic streams on single NIC 6 36
Linux hostname change 2 55
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 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…
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now