extraction of last but one char


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extraction of last but one char
# 1  
Old 02-14-2008
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  
Old 02-14-2008
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  
Old 02-14-2008
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  
Old 02-14-2008
A more general solution using sed is

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

# 5  
Old 02-14-2008
Quote:
Originally Posted by fpmurphy
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  
Old 02-14-2008
Thanks for your valuable suggestions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

2. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

3. Shell Programming and Scripting

extraction

I have following input @xxxxxx@ I want to extract what's between @....@ that is : xxxx using SED command (6 Replies)
Discussion started by: xerox
6 Replies

4. Shell Programming and Scripting

how first char in odd line and second char in even line

Hi I m having ifconfig -a o/p like sbanlab1:ksh# ifconfig -a | egrep "flags|inet" | awk -F' ' '{print $1,$2}' lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 bge0:... (1 Reply)
Discussion started by: tarunn.dubeyy
1 Replies

5. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

6. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

7. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

8. Shell Programming and Scripting

Regex extraction

Hello, I need your help to extract text from following: ./sherg_fyd_rur:blkabl="R23.21_BL2008_0122_1" ./serge_a75:rlwual="/main/r23.21=26-Mar-2008.05:00:20UTC@R11.31_BL2008_0325" ./serge_a75:blkabl="R23.21_BL2008_0325" ./sherg_proto_npiv:bkguals="R23.21_BL2008_0302 I80_11.31_LR" I... (11 Replies)
Discussion started by: abdurrouf
11 Replies

9. Shell Programming and Scripting

extraction of last but one char in a line

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. ... (1 Reply)
Discussion started by: hidnana
1 Replies

10. UNIX for Advanced & Expert Users

extraction from a tape

Hi, I have a backup on tape which was done by tar by someone else. How can I extract it now ? I know tar -xf for the tar file on disk but for the tar file on a tape, how should I do ? Many thanks before. PS : uname -a AIX server12 5 00545FFA4C00 (4 Replies)
Discussion started by: big123456
4 Replies
Login or Register to Ask a Question