Remove last 2 portions of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove last 2 portions of a string
# 1  
Old 10-23-2015
Remove last 2 portions of a string

I need to remove that last two parts of a string separated by an underscore

for example:
hello_g0f0_b6f5 Result should be hello
hello_come_here_g0f0_ffg0 Result should be hello_come_here
Hello_h_be_an_f_g1f1_ffe0 Result should be Hello_h_be_an_f
Ideal is we need to "cut" the last two parts of the string and print the rest
Code:
`echo $1|rev|cut -d"_" -f1-2 |rev`

This just prints the last 2 parts of the string.. I need the remaining part of the string..

Thanks!

Last edited by vgersh99; 10-23-2015 at 06:58 PM.. Reason: code tags, please!
# 2  
Old 10-23-2015
Hi razor1979,

This it seems to work

Code:
$ echo "hello_come_here_g0f0_ffg0" | awk '{print gensub(/(.+)(_.+_.+)/, "\\1","g")}'
hello_come_here

Regards
# 3  
Old 10-23-2015
Sorry I get a syntax error with ur command


awk: syntax error near line 1
awk: illegal statement near line 1
# 4  
Old 10-23-2015
what OS are you on?
Code:
echo 'Hello_h_be_an_f_g1f1_ffe0' | awk 'match($0,"_[^_][^_]*_[^_][^_]*$") {print substr($0,1, RSTART-1)}'

# 5  
Old 10-23-2015
Try:
Code:
echo "${1%_*_*}"




--
Quote:
Originally Posted by razor1979
Sorry I get a syntax error with ur command
awk: syntax error near line 1
awk: illegal statement near line 1
On Solaris use /usr/xpg4/bin/awk or nawk
# 6  
Old 10-24-2015
Try also
Code:
sed 's/\(_[^_]*\)\{2\}$//' file
hello
hello_come_here
Hello_h_be_an_f

# 7  
Old 10-25-2015
Quote:
Originally Posted by razor1979
I need to remove that last two parts of a string separated by an underscore

for example:
hello_g0f0_b6f5 Result should be hello
hello_come_here_g0f0_ffg0 Result should be hello_come_here
Hello_h_be_an_f_g1f1_ffe0 Result should be Hello_h_be_an_f
Ideal is we need to "cut" the last two parts of the string and print the rest
Code:
`echo $1|rev|cut -d"_" -f1-2 |rev`

This just prints the last 2 parts of the string.. I need the remaining part of the string..

Thanks!


Hi
you were quite nearSmilie, use below code

Code:
echo "hello_come_here_g0f0_ffg0" | rev | cut -d"_" -f3- | rev

Code:
echo "Hello_h_be_an_f_g1f1_ffe0" | rev | cut -d"_" -f3- | rev

This User Gave Thanks to looney For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match Fields between two files, print portions of each file together when matched in ([g]awk)'

I've written an awk script to compare two fields in two different files and then print portions of each file on the same line when matched. It works reasonably well, but every now and again, I notice some errors and cannot seem to figure out what the issue may be and am turning to you for help. ... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

How to grep and print portions of a very loooong line of text.?

Hi, A bit stumped here trying to grep and wanting to display portion of a text Using the grep below, I am able to find the line containing the string select ~]$ ~]$ srvctl -h | grep -i "select" Usage: srvctl add service -db <db_unique_name> -service <service_name> {-preferred... (1 Reply)
Discussion started by: newbie_01
1 Replies

3. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 Replies

4. Shell Programming and Scripting

Remove not only the duplicate string but also the keyword of the string in Perl

Hi Perl users, I have another problem with text processing in Perl. I have a file below: Linux Unix Linux Windows SUN MACOS SUN SUN HP-AUX I want the result below: Unix Windows SUN MACOS HP-AUX so the duplicate string will be removed and also the keyword of the string on... (2 Replies)
Discussion started by: askari
2 Replies

5. Shell Programming and Scripting

Remove multiple lines from a particular string to particular string

Hi, I have a file containing the DDLs of tables in a schema. From that I need to remove all the lines from a starting string till a specific string. Here is an example. File1.txt ------------- CREATE TABLE "SCHEMA1"."LKP11_TBL_USERS" ( "ID" NUMBER(8,0) NOT NULL ENABLE, "USER_ID"... (3 Replies)
Discussion started by: satyaatcgi
3 Replies

6. Shell Programming and Scripting

Remove first string

Hello, I have below data in a file and i want to remove 0 in front every digit from 1 to 9 16 08 08 16 16 16 16 08 08 16 out put should be 16 8 (2 Replies)
Discussion started by: mirwasim
2 Replies

7. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

8. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

9. Shell Programming and Scripting

Remove path string from file (string contains "/")

This is the way sed -i 's/home/$USER/.config/hello_there//g' /home/$USER/.gnomerc But, as far as I saw you cannot add any "/" in the string you want to remove.... So, what should I do in order to remove this path (which contains "/") ?:confused: (7 Replies)
Discussion started by: hakermania
7 Replies

10. Shell Programming and Scripting

Renaming Movies (or Flipping Portions of Filenames Using sed or awk)

Hey folks My problem is simple. For my first stash of movies, I used a naming convention of YEAR_MOVIE_NAME__QUALITY/ for each movie folder. For example, if I had a 1080p print of Minority Report, it would be 2002_Minority_Report__1080p/. The 2nd time around, I changed the naming convention... (4 Replies)
Discussion started by: ksk
4 Replies
Login or Register to Ask a Question