Solved

Getting length of output in shell

Posted on 2010-11-24
9
434 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

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…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

706 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

22 Experts available now in Live!

Get 1:1 Help Now