Get last field using awk substr

Use the fact that awk splits the lines in fields based on a field separator, that you can define. Hence, defining the field separator to / you can say: awk -F “https://stackoverflow.com/” ‘{print $NF}’ input as NF refers to the number of fields of the current record, printing $NF means printing the last one. So … Read more

php substr() function with utf-8 leaves � marks at the end

The comments above are correct so long as you have mbstring enabled on your server. $var = “Бензин Офиси А.С. также производит все типы жира и смазок и их побочных продуктов в его смесительных установках нефти машинного масла в Деринце, Измите, Алиага и Измире. У Компании есть 3 885 станций технического обслуживания, включая сжиженный газ … Read more

Extract the first 2 Characters in a string

You can just use the substr function directly to take the first two characters of each string: x <- c(“75 to 79”, “80 to 84”, “85 to 89”) substr(x, start = 1, stop = 2) # [1] “75” “80” “85” You could also write a simple function to do a “reverse” substring, giving the ‘start’ … Read more

Extract a substring according to a pattern

Here are a few ways: 1) sub sub(“.*:”, “”, string) ## [1] “E001” “E002” “E003” 2) strsplit sapply(strsplit(string, “:”), “[“, 2) ## [1] “E001” “E002” “E003” 3) read.table read.table(text = string, sep = “:”, as.is = TRUE)$V2 ## [1] “E001” “E002” “E003” 4) substring This assumes second portion always starts at 4th character (which is … Read more