The UNIX and Linux Forums  


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
How to replace any char with newline char. mightysam Shell Programming and Scripting 5 09-18-2008 09:15 PM
extraction of last but one char in a line hidnana Shell Programming and Scripting 1 02-14-2008 08:48 AM
AWK extraction harris2107 Shell Programming and Scripting 2 01-15-2008 11:44 AM
AWK extraction harris2107 Shell Programming and Scripting 3 08-17-2007 06:02 AM
Help with tar extraction! manthasirisha Shell Programming and Scripting 4 03-17-2006 07:17 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 02-14-2008
hidnana hidnana is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 14
extraction of last but one char

I need to extract the character before the last "|" in the following lines, which are 'N' and 'U'. The last "|" shouldn't be extracted. Also the no.s of "|" may vary in a line, but I need only the character before the last one.

TT-CAP-N241485-0005241485|ADMINISTRATOR|INC_ORGANISATION_NAME|SUPERIOR_TITLE_1|REV|FIRST_NAME_1|MIDDLE_NAME_1|SURNA ME_FIELD|INC ADDRESSS 1|INC ADDRESSS 2|INC ADDRESSS 3|INC ADDRESSS 4|INC ADDRESSS 5|INC ADDRESSS 6|IN123EC|MALTA|Y||M||Y|LIFEASSURED 2|Y|N||||||||||||||||||||||||Y||Y||||||||||||PRU|A||289832|||FCBGFBF|Y|Y|1|DC|1|1|3|Y|O||20300601||| CAP|5649369|VVUMU7SMT0|HCBCBFDJF|N|Y|N|Y|Y|N|Y|Y|N|3||6|C|20051205|555555555|||||20070604|4|15||C|0| 9|4527|0||20071109|4|C|982062|23569837|36657407|9096||20071205|1|N|

TT-CAP-5241492-0005241492|ADMINISTRATOR1|INC_ORGANISATION_NAME|SUPERIOR_TITLE_1|MS|FIRST_NAME_1|MIDDLE_NAME_1|SURNA ME_FIELD|INC ADDRESSS 1|INC ADDRESSS 2|INC ADDRESSS 3|INC ADDRESSS 4|INC ADDRESSS 5|INC ADDRESSS 6|IN123EC||N||F||N|LIFE ASSURED 1|Y|N||||||||||||||||||||||||Y||Y||||||||||||PRU|BFF||200006|||EB|Y|Y|3|DC|2|1|3|Y|O||20301207|||CAP |5649376|ZLBFQDGVGIOIJDHKNDK|F|N|Y|Y|N|Y|N|N|Y|N|4||7|C|20051205|555555555|||||20070604|2|1600770||B |281215|66|124002|923545||20060405|1|C|1446100|92811|64148521|5||20071205|1|U|

I'm not able to achieve it by cut command. How can it be done ?

Last edited by hidnana; 02-14-2008 at 06:06 AM..
  #2 (permalink)  
Old 02-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,555
you can use awk, set the field separator FS to "|", then use the NF inbuilt variable to get the last field. for the last second field, use NF-1. to get that value, prepend dollar sign, eg $(NF-1)
  #3 (permalink)  
Old 02-14-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,631
As the pipe symbol is the last char in the line it is matched by "|$". From there it is easy to construct the following regexp:

sed 's/.*\([NU]\)|$/\1/'

i hope this helps

bakunin
  #4 (permalink)  
Old 02-14-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,935
A more general solution using sed is


Code:
sed 's/.*\(.\)|$/\1/' file

  #5 (permalink)  
Old 02-14-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,631
Quote:
Originally Posted by fpmurphy View Post
A more general solution using sed is
You are right of course. I wouldn't have limited it if the thread-starter wouldn't have explicitly stated that the character could only be "N" or "U".

Perhaps it is a matter of programming philosophy (which is not the topic of this thread - at least not directly), i would prefer my solution over the general one followed by a line catching all the lines not matched by my solution to point them out as suspicious - lines which "shouldn't exist" at all.

The point is: if a specification says that the specific character can only be "N" or "U", then i would like to be informed if a line with neither "N" or "U" on it comes up. Maybe the specification has changed, maybe my script is faulty, maybe some pre-processing steps producing the lines are faulty - whatever. At least there is *something* that might have to be dealt with.

With all due respect
bakunin
  #6 (permalink)  
Old 02-14-2008
hidnana hidnana is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 14
Thanks for your valuable suggestions.
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 04:54 AM.


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