coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cat -n with different format?


From: Eric Blake
Subject: Re: cat -n with different format?
Date: Thu, 22 Dec 2011 10:26:32 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111115 Thunderbird/8.0

On 12/22/2011 10:14 AM, Peng Yu wrote:
> Hi,
> 
> I want to change how the line number is displayed
> 
> ~$ echo a | cat -n
>      1        a
> 
> For example, I want it to be shown as
> 
> 1:a
> 
> Although this can be easily done in anything other scripting language,
> I'm wondering if there is an even easier way to get it done with cat.

Nope, making cat do alternate output styles as an extension would be
adding bloat.  At this point, it's far more portable to use existing
standardized approaches.  My favorite is two invocations of sed (one to
insert line numberings between every line of input, the second to
convert every pair of lines into your desired output format).  For example:

$ seq 2 2 10 | sed = | sed 'N; s/\n/:/'
1:2
2:4
3:6
4:8
5:10

-- 
Eric Blake   address@hidden    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]