How do I delete text before the last forward slash in a column? - excel

I have a spreadsheet with columns that show a filepath. They look like this:
/j/t/jtfdsrn-01r_1_1_19.jpg
/j/t/jtfdsrn-01r_1_1_18.jpg
/j/t/jtfdsrn-01r_1_1_17.jpg
/j/t/jtfdsrn-01r_1_1_16.jpg
/j/t/jtfdsrn-01r_1_1_15.jpg
/j/t/jtfdsrn-01r_1_1_14.jpg
/j/t/jtfdsrn-01r_1_1_13.jpg
/j/t/jtfdsrn-01r_1_1_12.jpg
I want to remove everything before the last slash so they look like this:
/jtfdsrn-01r_1_1_19.jpg
/jtfdsrn-01r_1_1_18.jpg
/jtfdsrn-01r_1_1_17.jpg
/jtfdsrn-01r_1_1_16.jpg
/jtfdsrn-01r_1_1_15.jpg
/jtfdsrn-01r_1_1_14.jpg
/jtfdsrn-01r_1_1_13.jpg
/jtfdsrn-01r_1_1_12.jpg
Can I do this with a formula or an built-in function? I use OpenOffice.
I have tried the TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",LEN(A1))),LEN(A1))) formula but I get a Error:501 on it.

If your target string is always the same length:
=RIGHT(A1,23)
Input: /j/t/jtfdsrn-01r_1_1_19.jpg Output: /jtfdsrn-01r_1_1_19.jpg
If you have variable length strings and always 3 backslashes in filepath:
="/" &RIGHT(A1, LEN(A1) -FIND("*", SUBSTITUTE(A1,"/","*",3), 1))
Input: /j/t/jtfdsrn-01r_1_1_1000.jpg Output: /jtfdsrn-01r_1_1_1000.jpg
If you have variable length strings and variable backslashes in filepath:
="/" &RIGHT(A1, LEN(A1) -FIND("*", SUBSTITUTE(A1,"/","*", LEN(A1)-LEN(SUBSTITUTE(A1,"/","") )), 1))
Input: a/b/c/j/t/jtfdsrn-01r_1_1_19.jpg Output: /jtfdsrn-01r_1_1_19.jpg

Let me know if this works for you if your values dont change there shouldnt be an issue but if so ill look into it.
=RIGHT(A2;LEN(A2)-FIND("/";A2;3)-1)

Simply Use Wildcard (*/) Without Brackets In Find and Replace

Related

Add hyphen in between letters and hyphen

I have a list of sample names:
TW1
UD1
SS1
S17
SS23
UD12
I wish to add a hyphen in between the letters and the numbers as such:
TW-1
UD-1
SS-1
S-17
SS-23
UD-12
UD786
I tried this:
=MID(A1,1,COUNT(1*MID(A1,{1,2,3,4,5,6,7,8,9,0},1)))&"-"&SUBSTITUTE(A1,MID(A1,1,COUNT(1*MID(A1,{1,2,3,4,5,6,7,8,9,0},1))),"")
The results were not consistent. It gives the following results:
T-W1
U-D1
S-1
S1-7
SS-23
UD-12
How may I achieve the desired output?
=LEFT(A1,2-ISNUMBER(--MID(A1,2,1)))&"-"&RIGHT(A1,LEN(A1)-(2-ISNUMBER(--MID(A1,2,1))))
Is another option. Trick in this is to convert your number as a string to a number before testing if its a number. Instead of -- you could have done 1* or 0+.
This solution only works as your sample data was 1 or 2 characters before the first digit.
Slightly shorter than #Forward Ed's A:
=REPLACE(A1,2+(CODE(MID(A1,2,1))>64),,"-")

How to extract the characters from a string in Excel

Hi I would like to extract dynamically the numbers from string in Excel.
I have the following strings and I would like to have only the numbers before ". pdf". taken out of the string into the next column.
As you can see the number of characters varies from line to line.
I have invented something like this:
=MID(M20;SEARCH("_";M20);20)
But this takes out only the numbers after "_" and .pdf after this....
How to make it the way I like?
D:\Users\xxxx\Desktop\1610\ts25b_4462.pdf
D:\Users\xxx\Desktop\1610\ts02b_39522.pdf
D:\Users\xxxxx\Desktop\1610\ts02b_except_39511.pdf
D:\Users\xxxx\Desktop\1610\ts02b_except_39555.pdf
D:\Users\xxxx\Desktop\1610\ts22b_6118.pdf
So that I have just :
4462
39522
39511
39555
6118
and so on...
Thank you!!!
With VBA, try to do it like this:
Public Function splitThings(strInput As String) As String
splitThings = Split(Split(strInput, "_")(1), ".")(0)
End Function
Concerning your formula, try to use =LEFT(MID(M20;SEARCH("_";M20);20),K), where K is the difference of the length of ts22b_6118.pdf and 4 (.pdf). 4 is the length of .pdf.
Something like this should do the work:
=LEFT(MID(I3,SEARCH("_",I3)+1,LEN(I3)),LEN(MID(I3,SEARCH("_",I3),LEN(I3)))-5)
You should do it using Excel formula. For example:
=SUBSTITUTE(LEFT(A1,FIND(".pdf",A1)-1),LEFT(A1,FIND("_",A1)),"")
Using the first line as an example, with LEFT(A1,FIND(".pdf",A1)-1) you will have D:\Users\xxxx\Desktop\1610\ts25b_4462 and with the LEFT(A1,FIND("_",A1)) D:\Users\xxxx\Desktop\1610\ts25b_, if you SUBSTITUTE the first part by "" you will have 4462.
Hope this can help.
With this formula, you should be able to get the numbers you want:
=MID(A1,FIND("|",SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))+1,FIND(".",A1)-FIND("|",SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))-1)
Basically, this is the initial fomula:
=MID(A1,FIND("_",A1)+1,FIND(".",A1)-FIND("_",A1)-1)
But since there may be two _ in the string so this is the one to find the 2nd _:
=SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_","")))
Now just replace this SUBSTITUTE with A1 above and you get that long formula. Hope this helps.
This will return the number you want regardless of extension (could be .pdf, could be .xlsx, etc) and regardless of the number of underscores present in the filename and/or filepath:
=TRIM(LEFT(RIGHT(SUBSTITUTE(SUBSTITUTE(M20,".",REPT(" ",LEN(M20))),"_",REPT(" ",LEN(M20))),LEN(M20)*2),LEN(M20)))

Insert bracket before a =TEXT()

I have a function that shows a begin- and end date. I wanted to have it between brackets in a cell:
=TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
I have the outher bracket already:
Example ->01/01/2017 - 02/03/2017)
But can't seem to try around and insert a bracket in the beginning.
I have tried:
=TEXT((A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
=TEXT("("A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
=TEXT(&"("A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
"("=TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
Why isn't this working?
Use the concatenate function.
Try this: (Convention Changed)
=concatenate("("; TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj"); ")")
You can avoid string concatenation by making the extra characters part of the format mask used by the TEXT function. When using reserved characters or symbols that a format mask usually has another purpose for, precede them with a backslash to convert them to a 'string literal'.
=TEXT(A1; "\(dd/mm/jjjj - ")&TEXT(B1; "dd/mm/jjjj\)")

splitting directory fileparts into sections using matlab / octave

I would like to split pathstr into separate parts how can I do this? See example below.
PS: I'm using octave 3.8.1
dpath='tmp/h1/cli/pls/03sox_a_Fs_1000/'
[pathstr,name,ext] = fileparts(dpath)
>>>pathstr = tmp/h1/cli/pls/03sox_a_Fs_1000
If all I want is 03sox_a_Fs_1000 or pls
How can I do this?
Please note the filenames will change and could be of different lengths.
You can use strsplit (here using Matlab) to split your string (believe it or not!) using the delimiter /:
pathstr = 'tmp/h1/cli/pls/03sox_a_Fs_1000'
[Name,~] = strsplit(pathstr,'/')
Now Name looks like this:
Name =
'tmp' 'h1' 'cli' 'pls' '03sox_a_Fs_1000'
So you can select the last element using the end keyword and curly braces since the output of strsplit is a cell array:
Name = Name{end}
or end-1 to retrieve pls.
This applies to names of any length or format, as long as they are separated by /.

How can I perform a reverse string search in Excel without using VBA?

I have an Excel spreadsheet containing a list of strings. Each string is made up of several words, but the number of words in each string is different.
Using built in Excel functions (no VBA), is there a way to isolate the last word in each string?
Examples:
Are you classified as human? -> human?
Negative, I am a meat popsicle -> popsicle
Aziz! Light! -> Light!
This one is tested and does work (based on Brad's original post):
=RIGHT(A1,LEN(A1)-FIND("|",SUBSTITUTE(A1," ","|",
LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))
If your original strings could contain a pipe "|" character, then replace both in the above with some other character that won't appear in your source. (I suspect Brad's original was broken because an unprintable character was removed in the translation).
Bonus: How it works (from right to left):
LEN(A1)-LEN(SUBSTITUTE(A1," ","")) – Count of spaces in the original string
SUBSTITUTE(A1," ","|", ... ) – Replaces just the final space with a |
FIND("|", ... ) – Finds the absolute position of that replaced | (that was the final space)
Right(A1,LEN(A1) - ... )) – Returns all characters after that |
EDIT: to account for the case where the source text contains no spaces, add the following to the beginning of the formula:
=IF(ISERROR(FIND(" ",A1)),A1, ... )
making the entire formula now:
=IF(ISERROR(FIND(" ",A1)),A1, RIGHT(A1,LEN(A1) - FIND("|",
SUBSTITUTE(A1," ","|",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))))
Or you can use the =IF(COUNTIF(A1,"* *") syntax of the other version.
When the original string might contain a space at the last position add a trim function while counting all the spaces: Making the function the following:
=IF(ISERROR(FIND(" ",B2)),B2, RIGHT(B2,LEN(B2) - FIND("|",
SUBSTITUTE(B2," ","|",LEN(TRIM(B2))-LEN(SUBSTITUTE(B2," ",""))))))
This is the technique I've used with great success:
=TRIM(RIGHT(SUBSTITUTE(A1, " ", REPT(" ", 100)), 100))
To get the first word in a string, just change from RIGHT to LEFT
=TRIM(LEFT(SUBSTITUTE(A1, " ", REPT(" ", 100)), 100))
Also, replace A1 by the cell holding the text.
A more robust version of Jerry's answer:
=TRIM(RIGHT(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))), LEN(TRIM(A1))))
That works regardless of the length of the string, leading or trailing spaces, or whatever else and it's still pretty short and simple.
I found this on google, tested in Excel 2003 & it works for me:
=IF(COUNTIF(A1,"* *"),RIGHT(A1,LEN(A1)-LOOKUP(LEN(A1),FIND(" ",A1,ROW(INDEX($A:$A,1,1):INDEX($A:$A,LEN(A1),1))))),A1)
[edit] I don't have enough rep to comment, so this seems the best place...BradC's answer also doesn't work with trailing spaces or empty cells...
[2nd edit] actually, it doesn't work for single words either...
=RIGHT(TRIM(A1),LEN(TRIM(A1))-FIND(CHAR(7),SUBSTITUTE(" "&TRIM(A1)," ",CHAR(7),
LEN(TRIM(A1))-LEN(SUBSTITUTE(" "&TRIM(A1)," ",""))+1))+1)
This is very robust--it works for sentences with no spaces, leading/trailing spaces, multiple spaces, multiple leading/trailing spaces... and I used char(7) for the delimiter rather than the vertical bar "|" just in case that is a desired text item.
This is very clean and compact, and works well.
{=RIGHT(A1,LEN(A1)-MAX(IF(MID(A1,ROW(1:999),1)=" ",ROW(1:999),0)))}
It does not error trap for no spaces or one word, but that's easy to add.
Edit:
This handles trailing spaces, single word, and empty cell scenarios. I have not found a way to break it.
{=RIGHT(TRIM(A1),LEN(TRIM(A1))-MAX(IF(MID(TRIM(A1),ROW($1:$999),1)=" ",ROW($1:$999),0)))}
=RIGHT(A1,LEN(A1)-FIND("`*`",SUBSTITUTE(A1," ","`*`",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))
New answer 9/28/2022
Considering the new excel function: TEXTAFTER (check availability) you can achieve it with a simple formula:
=TEXTAFTER(A1," ", -1)
To add to Jerry and Joe's answers, if you're wanting to find the text BEFORE the last word you can use:
=TRIM(LEFT(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))), LEN(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))))-LEN(TRIM(A1))))
With 'My little cat' in A1 would result in 'My little' (where Joe and Jerry's would give 'cat'
In the same way that Jerry and Joe isolate the last word, this then just gets everything to the left of that (then trims it back)
Copy into a column, select that column and HOME > Editing > Find & Select, Replace:
Replace All.
There is a space after the asterisk.
Imagine the string could be reversed. Then it is really easy. Instead of working on the string:
"My little cat" (1)
you work with
"tac elttil yM" (2)
With =LEFT(A1;FIND(" ";A1)-1) in A2 you get "My" with (1) and "tac" with (2), which is reversed "cat", the last word in (1).
There are a few VBAs around to reverse a string. I prefer the public VBA function ReverseString.
Install the above as described. Then with your string in A1, e.g., "My little cat" and this function in A2:
=ReverseString(LEFT(ReverseString(A1);IF(ISERROR(FIND(" ";A1));
LEN(A1);(FIND(" ";ReverseString(A1))-1))))
you'll see "cat" in A2.
The method above assumes that words are separated by blanks. The IF clause is for cells containing single words = no blanks in cell. Note: TRIM and CLEAN the original string are useful as well. In principle it reverses the whole string from A1 and simply finds the first blank in the reversed string which is next to the last (reversed) word (i.e., "tac "). LEFT picks this word and another string reversal reconstitutes the original order of the word (" cat"). The -1 at the end of the FIND statement removes the blank.
The idea is that it is easy to extract the first(!) word in a string with LEFT and FINDing the first blank. However, for the last(!) word the RIGHT function is the wrong choice when you try to do that because unfortunately FIND does not have a flag for the direction you want to analyse your string.
Therefore the whole string is simply reversed. LEFT and FIND work as normal but the extracted string is reversed. But his is no big deal once you know how to reverse a string. The first ReverseString statement in the formula does this job.
=LEFT(A1,FIND(IF(
ISERROR(
FIND("_",A1)
),A1,RIGHT(A1,
LEN(A1)-FIND("~",
SUBSTITUTE(A1,"_","~",
LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))
)
)
)
),A1,1)-2)
I translated to PT-BR, as I needed this as well.
(Please note that I've changed the space to \ because I needed the filename only of path strings.)
=SE(ÉERRO(PROCURAR("\",A1)),A1,DIREITA(A1,NÚM.CARACT(A1)-PROCURAR("|", SUBSTITUIR(A1,"\","|",NÚM.CARACT(A1)-NÚM.CARACT(SUBSTITUIR(A1,"\",""))))))
Another way to achieve this is as below
=IF(ISERROR(TRIM(MID(TRIM(D14),SEARCH("|",SUBSTITUTE(TRIM(D14)," ","|",LEN(TRIM(D14))-LEN(SUBSTITUTE(TRIM(D14)," ","")))),LEN(TRIM(D14))))),TRIM(D14),TRIM(MID(TRIM(D14),SEARCH("|",SUBSTITUTE(TRIM(D14)," ","|",LEN(TRIM(D14))-LEN(SUBSTITUTE(TRIM(D14)," ","")))),LEN(TRIM(D14)))))
You can achieve this also by reversing the string and finding the first space
=MID(C3,2+LEN(C3)-SEARCH(" ",CONCAT(MID(C3,SEQUENCE(LEN(C3),,LEN(C3),-1),1))),LEN(A1))
Reverse the string
CONCAT(MID(C3,SEQUENCE(LEN(C3),,LEN(C3),-1),1))
Find the first space in the reversed string
SEARCH(" ",...
Take the position of the space found in the reversed string off the length of the string and return that portion
=MID(C3,2+LEN(C3)-SEARCH...
I also had a task like this and when I was done, using the above method, a new method occured to me: Why don't you do this:
Reverse the string ("string one" becomes "eno gnirts").
Use the good old Find (which is hardcoded for left-to-right).
Reverse it into readable string again.
How does this sound?

Resources