The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to serach if a new line character exists on the last line in a file sunilbm78 UNIX for Dummies Questions & Answers 10 02-29-2008 02:15 PM
how to add new line character ravi.sadani19 Shell Programming and Scripting 3 11-23-2007 03:26 AM
delete a line based on first character of the line borncrazy UNIX for Dummies Questions & Answers 2 12-06-2005 03:27 PM
end of the line character zomboo Shell Programming and Scripting 5 12-30-2004 05:30 PM
Remove Last Character of Line danhodges99 Shell Programming and Scripting 4 05-21-2003 09:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-14-2008
Mr_Plow Mr_Plow is offline
Registered User
  
 

Join Date: Sep 2008
Location: Perth, Scotland
Posts: 8
Using shell to get the last character in a line

Good day ladies and gents,

I'm trying to write shell code that can give ignore everything else in a line bar the last character but not having much luck.

Any ideas? I have been looking at cut and sed but not making any progress.
  #2 (permalink)  
Old 10-14-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Can you use this approach?

Code:
> echo "hello" | awk '{print substr($0,length,1)}'
o
> echo "hello smiley" | awk '{print substr($0,length,1)}'
y
  #3 (permalink)  
Old 10-14-2008
Mr_Plow Mr_Plow is offline
Registered User
  
 

Join Date: Sep 2008
Location: Perth, Scotland
Posts: 8
Quote:
Originally Posted by joeyg View Post
Code:
> echo "hello" | awk '{print substr($0,length,1)}'
o
> echo "hello smiley" | awk '{print substr($0,length,1)}'
y
Works a treat, thanks very much.

I usually look at awk and feel a bit ill
  #4 (permalink)  
Old 10-14-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,860
Code:
sed 's/.*\(.\)$/\1/' input
[GNU and some implementations of New AWK]

Code:
awk '{print $NF}' FS= input
Code:
perl -nle'print/(.)$/' input
or

Code:
perl -lne'print chop' input
Code:
ruby -ne'puts$_[/.$/].to_s' input
Code:
while IFS= read -r; do printf "%s\n" "${REPLY#${REPLY%?}}";done<input
With recent versions of bash:

Code:
while IFS= read -r; do printf "%s\n" "${REPLY[ -1]}";done<input
With zsh:

Code:
while IFS= read -r;do print -- $REPLY[-1];done<input
If rev and process substitution are available:

Code:
cut< <(rev input) -c1
Or with GNU grep (this won't display empty lines):

Code:
grep -Eo '.$' input

Last edited by radoulov; 10-14-2008 at 02:55 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0