Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Using printf in bash

UNIX for Dummies Questions & Answers


Tags
bash, printf

 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-04-2013
lavender lavender is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 14 June 2013, 12:00 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Using printf in bash


Code:
printf "%5.5\n" "1234567890"

will print 12345 . How do I get it to print
67890
Essentially, I just want the last 5 characters rather than the first 5.

Last edited by Scrutinizer; 01-04-2013 at 07:19 AM..
Sponsored Links
    #2  
Old Unix and Linux 01-04-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 20 January 2017, 5:52 PM EST
Location: Galactic Empire
Posts: 3,530
Thanks: 250
Thanked 1,282 Times in 1,204 Posts
Substring Extraction


Code:
num="1234567890"
printf "%d\n" ${num: -5}

Sponsored Links
    #3  
Old Unix and Linux 01-04-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 January 2017, 5:19 PM EST
Location: Aachen, Germany
Posts: 10,156
Thanks: 230
Thanked 3,073 Times in 2,849 Posts
This works for numbers. Use %sformat specifier for characters:
Code:
$ var="ABCDEFGHIJK"
$ printf "%5s\n" ${var:5}
FGHIJK
or
$ printf "%5s\n" ${var: -3}
  IJK

    #4  
Old Unix and Linux 01-04-2013
lavender lavender is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 14 June 2013, 12:00 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!
Sponsored Links
    #5  
Old Unix and Linux 01-04-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 20 January 2017, 1:59 PM EST
Location: Amsterdam
Posts: 11,196
Thanks: 456
Thanked 3,169 Times in 2,799 Posts

Code:
num="1234567890"
echo "${num#?????}"

Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
floating point not recognized by printf in bash josegr Shell Programming and Scripting 2 03-07-2010 12:36 AM
[bash]printf octal instead of decimal dolphin06 Shell Programming and Scripting 3 04-14-2009 07:38 AM
printf in bash shell not printing negative values npatwardhan Shell Programming and Scripting 2 01-14-2009 05:51 PM
printf arunviswanath Programming 2 09-19-2007 10:31 PM



All times are GMT -4. The time now is 08:03 PM.