Using shell to get the last character in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using shell to get the last character in a line
# 1  
Old 10-14-2008
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  
Old 10-14-2008
Hammer & Screwdriver 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  
Old 10-14-2008
Quote:
Originally Posted by joeyg
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 Smilie
# 4  
Old 10-14-2008
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 03:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to understand special character for line reading in bash shell?

I am still learning shell scripting. Recently I see a function for read configuration. But some of special character make me confused. I checked online to find answer. It was not successful. I post the code here to consult with expert or guru to get better understanding on these special characters... (3 Replies)
Discussion started by: duke0001
3 Replies

2. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

3. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

4. Shell Programming and Scripting

Is shell's next line character '\' the cause of this issue ?

Oracle version: 11.2.0.1 /Red Hat Enterprise Linux Server release 5.4 (Tikanga) Hi, impdp is a command line utility in oracle to import data into a DB schema. Typically impdp command along with its parameters is run in a single line like impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR... (8 Replies)
Discussion started by: omega3
8 Replies

5. UNIX for Dummies Questions & Answers

Need help removing last character of every line if certain character

I need help removing the last character of every line if it is a certain character. For example I need to get rid of a % character if it is in the last position. Input: aaa% %bbb ccc d%dd% Output should be: aaa %bbb ccc d%dd I tried this but it gets rid of all of the % characters.... (5 Replies)
Discussion started by: raptor25
5 Replies

6. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

7. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

8. Shell Programming and Scripting

delete new line character ( - ) , korn shell

Hi guys , i need help so bad on this issue.. Basically i have to delete the line continuation symbol of first column variable and add the truncated part of that word in next line to first line. here i written sample 3 lines but originally i have bunch of lines in that file. client1_day- ... (3 Replies)
Discussion started by: chrismorgan
3 Replies

9. Shell Programming and Scripting

Help with shell script: moving end of line character

Hello. I have a file (old.txt) that I need to copy into another file (new.txt). Each line on old.txt ends with CR/LF but the position of CR/LF varies from one record to another. I need to copy each line of record to new.txt and move CR/LF in pos 165. Can I use awk to achieve this? How?... (8 Replies)
Discussion started by: udelalv
8 Replies

10. Shell Programming and Scripting

Awk not working due to missing new line character at last line of file

Hi, My awk program is failing. I figured out using command od -c filename that the last line of the file doesnt end with a new line character. Mine is an automated process because of this data is missing. How do i handle this? I want to append new line character at the end of last... (2 Replies)
Discussion started by: pinnacle
2 Replies
Login or Register to Ask a Question