The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to convert C source from 8bit chars to 16bit chars? siegfried Shell Programming and Scripting 0 09-26-2007 11:26 AM
catching some errors bebop1111116 Shell Programming and Scripting 5 09-29-2006 10:12 AM
Catching all Exit Codes Sivaswami J Shell Programming and Scripting 3 02-16-2006 04:13 PM
Catching signal and piping joseph_ng High Level Programming 7 11-14-2005 08:04 AM
catching interrupts toughguy2handle UNIX for Dummies Questions & Answers 1 09-19-2005 12:17 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-21-2005
Registered User
 

Join Date: Jun 2005
Location: Alexandria, Scotland
Posts: 46
Awk- catching the last two chars

Can anyone explain to me how to get the last two chars' from each row of Column (each row being variable in length) using awk, some of the lines will be blank, I'll be running a paste after awking. So I need to keep the blanks where they are..so I can paste back all columns in the correct order

I see posts about stripping the last chars but nothing on getting the last chars

ie
GASTRO A9
NEONATALAF
GER.ASAB

GASTRO A9
ORSUR D3
GASTRO A9
GER.AS AB
GER.AP AB
NEONATALAF

output :

A9
AF
AB

A9
D3
A9
AB
AB
AF
Reply With Quote
Forum Sponsor
  #2  
Old 11-21-2005
Registered User
 

Join Date: Oct 2005
Posts: 50
Question Suggestion

Maybe something like....
grep -C 2> filename
? im not sure im trying to learn grep, awk, and sed so im not sure. if anyone can explain what i did wrong please do. How i came up with my solution is grep for searching -C because in man grep it says -C means print num lines of output context and 2> for 2 before the end of the line. I am most likely wrong but if someone can show the right way and explain why im wrong it would be greatly appreciated. Thanks
Reply With Quote
  #3  
Old 11-21-2005
Registered User
 

Join Date: Oct 2005
Posts: 50
Another suggestion if the otherone doesnt work

sed -n '/STRING/,$p' filename|head -$n
Thats what i found while floating around on the forums now maybe you could change head to foot and possible make it so instead of the line and 2 more lines print, change it to make the last 2 character print? Idk maybe this will help you realize what you need or something? But can someone let me know whats wrong with this if it doesnt work maybe?
Reply With Quote
  #4  
Old 11-21-2005
Registered User
 

Join Date: Sep 2005
Posts: 45
I can't think of anything in awk I'm afraid. I would have thought it'd be custom made for that job (in fact I'm sure someone who knows more will give you a very simple command, but I've drawn a blank), but with sed you could use:

sed 's/^.*\(..\)$/\1/' input_file
Reply With Quote
  #5  
Old 11-21-2005
Registered User
 

Join Date: Jun 2005
Location: Alexandria, Scotland
Posts: 46
awk filtering the last two characters at end of line

thanks for that I'll give it a try in the morning when I get to work...
Reply With Quote
  #6  
Old 11-21-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
Code:
nawk '{print substr($0, length($0)-1)}' myFile.txt
Reply With Quote
  #7  
Old 11-21-2005
Registered User
 

Join Date: Nov 2005
Posts: 6
You may Laugh!

since I dont have much experience in any thing: I will do this work in a simple but long way: so laughing is allowed looking at my code.

for file in `cat t40.txt|nawk '{print $3}'`;do
n=`echo $file|wc -C`
n=`expr $n - 1`
n1=`expr $n - 1`
echo $file|cut -c $n1-$n

I dont know why wc -C brings one more number than the real number of characters are there.

echo "this"|wc -C

this gives value 5 instead of 4.

thanks.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0