Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Create a fixed-length strings

    Hi guys,
    I have a file containing different lengths of strings.
    I want all of them to be in the same length. For example 10 characters.
    Most of them are already in this length, so I want to fix those that are less than 10 to 10 by padding.
    for example,

    1234567890
    1234567890
    12345678
    1234567890

    so, I want the third string to be:
    12345678NN

    The others are OK, so I don't want to touch them..

    Any idea of doing it in a simple way?
    sed or awk ?

    Thanks,
    Pap




  2. #2
    Join Date
    Oct 2012
    Location
    Minsk, Belarus
    Posts
    15
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    0
    Code:
    perl -ni.bak -e 'chomp; printf "%.10s%s\n", $_, "N"x(10-length)' file.txt

  3. #3
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Quote Originally Posted by old_bolvan View Post
    Code:
    perl -ni.bak -e 'chomp; printf "%.10s%s\n", $_, "N"x(10-length)' file.txt
    Thanks!

  4. #4
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Code:
    awk '{printf "%010s\n", $0}' "$file"

 

 

Similar Threads

  1. Linux locate command search multiple strings
    By Sonal in forum General Linux
    Replies: 1
    Last Post: 08-23-2012, 07:01 PM
  2. Intel Remains Confident In Fixed-Up RC6
    By News in forum Linux News
    Replies: 0
    Last Post: 03-25-2012, 08:01 AM
  3. Replies: 0
    Last Post: 01-13-2012, 03:27 PM
  4. Replies: 0
    Last Post: 12-17-2011, 05:23 AM
  5. Five Kernel Vulnerabilities Fixed in RHEL 5
    By Fred in forum Linux News
    Replies: 0
    Last Post: 11-30-2011, 07:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
           








Check out Linux Central for Linux software and other goodies!





» Stats

Members: 3,581
Threads: 3,920
Posts: 9,447
Top Poster: Fred (1,486)
Welcome to our newest member, AmyJoan

» Links



Powered by vBadvanced CMPS