padding zeros to the no of recs from the file

Posted on 2009-04-22
Last Modified: 2012-05-06
echo '0000000 expr `wc -l < filename` -2 '|sed 's/.*\(.......\)$/99\1/'

I am trying to get count of recs from afilename and  subtract 2 from the count and padding zeros to the  count after subtraction  and append 99 in the start of the line

I am getting some error
Question by:sumanth_ora
    LVL 16

    Expert Comment

    I couldn't understand what you want.
    Please, rewrite the procedure in a multi-line form with one step per line
    LVL 40

    Expert Comment

    please elaborate

    Author Comment

    I like get the no of records(count) from a file then subtract 2 from that

    ex:no of recs from a file =100
        subtract 100-2=98

    I need to padded with zeros always lenth of the padded value should be  be 7 charecters

    if value is 98 then 0000098
    if values is 8 then 0000008
    if values is 345 the 0000345

    Finally am appending 99 in the front of the string


    LVL 16

    Expert Comment

    I see. You should use a script (like the one attached, which takes a filename as input and prints the corresponding string)
    count=`cat $1|wc -l`;
    count=$(($count - 2));
    l2=`echo ${#count}`
    for ((i=$l2;i<7;i+=1)); do
    echo $count;

    Open in new window


    Author Comment

    I was looking for single  line statement insted of programing

    Jsut like this

    echo '0000000 expr `wc -l < filename` -2 '|sed 's/.*\(.......\)$/99\1/'
    LVL 48

    Accepted Solution


    printf "99%07d\n" $(eval expr "$(wc -l<filename) - 2")

    LVL 16

    Expert Comment

    What year are we in, forgive me? :)
    Do we still have the limit of 32KB of physical memory? One line statements are difficult to mantain and to understand. I prefer extended scripts ;)

    BTW, nice shot TinTin

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
    Join Greg Farro and Ethan Banks from Packet Pushers ( and Greg Ross from Paessler ( for a discussion about smart network …
    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…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

    755 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

    20 Experts available now in Live!

    Get 1:1 Help Now