Sponsored Content
Top Forums Shell Programming and Scripting Unix Cut or Awk from 'Right TO Left' Post 302479965 by durden_tyler on Monday 13th of December 2010 12:14:28 PM
Old 12-13-2010
Assuming that your data looks like this -

Code:
$
$ cat f35
FirstName_MiddleName1_LastName_ID
FirstName_LastName_ID
FirstName_LastName_XY
FirstName_LastName_XYZ
FirstName_MiddleName1_MiddleName2_LastName_ID
$

If you want to remove the "_ID" at the end of each line, then you could do this -

Code:
$
$ perl -plne 's/_ID$//' f35
FirstName_MiddleName1_LastName
FirstName_LastName
FirstName_LastName_XY
FirstName_LastName_XYZ
FirstName_MiddleName1_MiddleName2_LastName
$

But that doesn't work if you have something other than "ID" at the end, for example "_XY".

To remove the "_" followed by (any) two characters at the end, do this -

Code:
$
$ perl -plne 's/_..$//' f35
FirstName_MiddleName1_LastName
FirstName_LastName
FirstName_LastName
FirstName_LastName_XYZ
FirstName_MiddleName1_MiddleName2_LastName
$

That doesn't work if you have more than two characters after the "_" at the end, for example "_XYZ".

To remove "_" followed by any number of characters other than "_" at the end, do this -

Code:
$
$ perl -plne 's/_[^_]*$//' f35
FirstName_MiddleName1_LastName
FirstName_LastName
FirstName_LastName
FirstName_LastName
FirstName_MiddleName1_MiddleName2_LastName
$
$

This is a general substitution of which "_ID" is a special case.

Assuming that you want to go for the third one-liner, your Perl program would change like so -

Code:
...
my $tgt_user = $ARGV[0];
$tgt_user =~ s/_[^_]*$//;
...

HTH,
tyler_durden
This User Gave Thanks to durden_tyler For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk reporting. Format the output with left justification for every feild

Fallowing is the input file that is pipe seperated. is it possible to generated the report that is alligned left justifed as that of sample output. I apprecitae your help on this. InputFile (temp.txt): 108005555|001|christina.lipski||Submitter... (3 Replies)
Discussion started by: ainuddin
3 Replies

2. Shell Programming and Scripting

Left join on files using awk

nawk 'NR==FNR{a;next} {if($1 in a) print $1,"Found" else print}' OFS="," File_B File_A The above code is not working help is appreciated (6 Replies)
Discussion started by: pinnacle
6 Replies

3. Shell Programming and Scripting

using cut command right to left

Hi guys I have variable that contains a full directory path. var=/tmp/a/b/c/d I want to be able to extract different directories in the path right to left while using / as the delimiter. so for e.g. if in the above example if I want c then I want it to be in the middle of 1st and... (2 Replies)
Discussion started by: alinaqvi90
2 Replies

4. Shell Programming and Scripting

Left Join in Unix based on Key?

So I have 2 files: File 1: 111,Mike,Stipe 222,Peter,Buck 333,Mike,Mills File 2: 222,Mr,Bono 444,Mr,Edge I want output to be below, where 222 records joined and all none joined records still in output 111,Mike,Stipe 222,Peter,Buck,Mr,Bono 333,Mike,Mills 444,Mr,Edge (4 Replies)
Discussion started by: stack
4 Replies

5. Shell Programming and Scripting

Left padding in Unix

I am passing input string,length, and the pad character. input string=123 Pad char=# Length=6 then the output should be: ###123 How we can do this? Thanks (5 Replies)
Discussion started by: pandeesh
5 Replies

6. Shell Programming and Scripting

left join using awk

Hi guys, I need to use awk to join 2 files file_1 A 001 B 002 C 003 file_2 A XX1 B XX2 output desired A 001 XX1 B 002 missing C 003 XX2 thank you! (2 Replies)
Discussion started by: g1org1o
2 Replies

7. Shell Programming and Scripting

left join using awk

Hi guys, I need AWK to merge the following 2 files: file1 1 a 1 1 2 b 2 2 3 c 3 3 4 d 4 4 file2 a a/a c/c a/c c/c a/a c/t c c/t c/c a/t g/g c/c c/t desired output: 1 a 1 1 a/a c/c a/c c/c a/a c/t 2 b 2 2 x x x x x x 3 c 3 3 c/t c/c a/t g/g c/c c/t 4 d 4 4 x x x x x x (2 Replies)
Discussion started by: g1org1o
2 Replies

8. Shell Programming and Scripting

Left pad spaces using awk or sed

Hi,I've a unix pipe delimited file as below f1|f2|f3|f4|f5|f6 My requirement is to pad spaces on the left to fields f2, f3 and f5. Field Lengths according to file layout f2 - 4 char f3 - 5 char f5 - 3 char If my record is as below 1|43|bc|h0|34|a Output record should be as below 1| 43| bc|h0|... (4 Replies)
Discussion started by: Soujanya_K
4 Replies

9. Shell Programming and Scripting

awk to substitute ip without zero left padding

Hello All, I have this script to awk IP to new file. #awk '/myip|yourip/ {sub(/...\....\....\..../, newip)}1' newip=$IP existing.txt > new.txt When existing.txt has myip=192.168.123.123 and $IP has 192.168.12.12, the awk script is not working. But while I add zero left padding to $IP i.e,... (3 Replies)
Discussion started by: Shaan_Shaan
3 Replies

10. Shell Programming and Scripting

Difficulties in matching left bracket as literal in awk

I need to work with records having #AX in the EXP1 , please see my data sample and my attempt below: $ cat xx 08:30:33 KEY1 (1255) EXP1 VAL:20AX0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AX0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AW0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AW0030006 $ gawk '{... (1 Reply)
Discussion started by: migurus
1 Replies
All times are GMT -4. The time now is 05:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy