Cut the strings from end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut the strings from end
# 1  
Old 09-07-2017
Cut the strings from end

could you please help me.
I have below autosys jobs

Code:
01_enable_input_hol_dtpz1b
01_abc_copy_ld_sat_xxxz1
01_abc_mavcd_yyyyyxxxz1
01_abcdef_oa_xxxxxz1
01_fdgte_symbol_ddddz1
01_fsdfsd_clean_mmmhhhfz1
01_fsdfd_create_mut_marchtz1

I want to remove name after last "_" underscore
so that I get
Code:
01_enable_input_hol
001_abc_copy_ld_sat
01_abc_mavcd
01_abcdef_oa
01_fdgte_symbol
01_fsdfsd_clean
01_fsdfd_create_mut


I tried the awk using substr but not able to display required output
because these names can be long or short depending on the setup

Code:
cat abc.1 | awk '{print substr($0, length($0)-7)}'

# 2  
Old 09-07-2017
Code:
sed 's/_[^_]*$//' abc.1

If substr must be used in awk, try:
Code:
awk '{print substr($0, 1, (match($0, "_[^_]*$") ? RSTART - 1 : length))}' abc.1

or try another awk solution:
Code:
awk -F_ 'NF<2 || NF--' OFS=_ abc.1


Last edited by rdrtx1; 09-07-2017 at 07:16 PM..
These 2 Users Gave Thanks to rdrtx1 For This Post:
# 3  
Old 09-07-2017
Quote:
Originally Posted by rdrtx1
Code:
sed 's/_[^_]*$//' abc.1

If substr must be used in awk, try:
Code:
awk '{print substr($0, 1, (match($0, "_[^_]*$") ? RSTART - 1 : length))}' abc.1

or try another awk solution:
Code:
awk -F_ 'NF<2 || NF--' OFS=_ abc.1


Code:
awk -F_ 'NF<2 || NF--' OFS=_ abc.1

worked Smilie
This User Gave Thanks to sdosanjh For This Post:
# 4  
Old 09-08-2017
Note: decreasing NF to reduce the number of fields is not a documented feature and only works with some awk versions.
# 5  
Old 09-08-2017
Code:
rev file | cut -d_ -f2- | rev
01_enable_input_hol
01_abc_copy_ld_sat
01_abc_mavcd
01_abcdef_oa
01_fdgte_symbol
01_fsdfsd_clean
01_fsdfd_create_mut

This User Gave Thanks to RudiC For This Post:
# 6  
Old 09-09-2017
Hi
How about
Code:
while read line;do echo ${line%_*};done<lefile

# 7  
Old 09-11-2017
With any standards-conforming version of awk, one could also use:
Code:
awk -F_ '{print substr($0, 1, length - length($NF) - 1)}' file

which produces the output:
Code:
01_enable_input_hol
01_abc_copy_ld_sat
01_abc_mavcd
01_abcdef_oa
01_fdgte_symbol
01_fsdfsd_clean
01_fsdfd_create_mut

Note, however, that the 2nd line of the output differs from the output you requested:
Code:
001_abc_copy_ld_sat

If you really want the output you requested in post #1 of this thread, you're going to have to explain the conditions under which the extra leading zero is to be inserted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Cut a word between two strings and repeat the same in the entire file

in the below data i need to search for the word typeMismatch and then traverse back to find the filename of that particular mismatch. Like this we have to get all the file names which has error in them. How can i acheive this. I tried use sed or awk but not able to achevie the same. Sample... (2 Replies)
Discussion started by: ATWC
2 Replies

2. Programming

search a file between two begin and end strings in c

Can any one help me out with following problem... I want to search in a file which has two strings repeat each time(like start and end) i want to search between these two string in C programming. please help me with the solution. thanks in advance. (8 Replies)
Discussion started by: uday.sena.m
8 Replies

3. Shell Programming and Scripting

Cut line from file till the end

Hi I have a 57 line text file and I want to cut first 6 line assigned it to a veriable and again cut next 6 line assigned to same variable till the time file have left 0 line. Please let me know how I can do in scripting. Thanks Sanjay (6 Replies)
Discussion started by: Sanjay2121
6 Replies

4. Shell Programming and Scripting

delete to end of formatted warnings using sed or cut...

hi, i've searched the forums' entries and have tried some of the examples -- to no avial -- this is my first post -- thanks in advance for your help... As part of an installation program -- i'm receiving two(2) extraneous "libcxb WARNING!" statements -- i want to use sed to eliminate the... (12 Replies)
Discussion started by: rickkar
12 Replies

5. Shell Programming and Scripting

cut but include delimiter at end

Can someone please tell me how to do this... input file - /etc/group: wheel:*:0:root,timber daemon:*:1: mysql:*:88: ...etc... giants:*:1001:dalton,bandit dalton:*:1002: bandit:*:1003: output file (my goal): giants:*:1001: dalton:*:1002: bandit:*:1003:I've come up with this: ... (3 Replies)
Discussion started by: Lewcifer
3 Replies

6. Shell Programming and Scripting

ksh :: want to cut the strings

I have contents like 423562143124/53125351276 sdgas/347236 sjhdk;ls'ald/y62783612763 I need a command that would make the string before / and after / as separate output as (A should contain 423562143124 )and B should contain 53125351276). I tried but in vain. Please help. (19 Replies)
Discussion started by: rollthecoin
19 Replies

7. Shell Programming and Scripting

cut from a particular character to the end of the string

Hi All, I have a string like "9633C01302_2". I need to extract the number(02) after "13" and before "_" and the number coming after "13" and before "_" is not constant, it keeps on changing... Can some one plz help me wth the command.. i tried this echo "9633C01302_2" | cut -d'_' -f1 ..But... (2 Replies)
Discussion started by: grajesh_955
2 Replies

8. Shell Programming and Scripting

How to Select or cut from the certain filed to the end of the line

Hi: I have few rows in file..Like suppose... 9063C0 44 00051363603253033253347 3333 070248 06 9063C0 5G PAN00013 9063C0 44 00061030305040404250724 0506 100248 08 9063C0 43 01 00000089 I need to cut the row starting after... (5 Replies)
Discussion started by: grajesh_955
5 Replies

9. AIX

CUT command - cutting characters from end of string

Hello, I need to delete the final few characters from a parameter leaving just the first few. However, the characters which need to remain will not always be a string of the same length. For instance, the parameter will be passed as BN_HSBC_NTRS/hub_mth_ifce.sf. I only need the bit before the... (2 Replies)
Discussion started by: JWilliams
2 Replies

10. Shell Programming and Scripting

To cut end string from line

HI, I want to cut end string from line. e.g. i have following input line /users/home/test.txt I want to get end string 'test.txt' from above line and length of that end string will change and it always start after '/'. Thanks, Visu (7 Replies)
Discussion started by: visu
7 Replies
Login or Register to Ask a Question