bash, extract string before a colon
问题 If I have a file with rows like this /some/random/file.csv:some string /some/random/file2.csv:some string2 Is there some way to get a file that only has the first part before the colon, e.g. /some/random/file.csv /some/random/file2.csv I would prefer to just use a bash one liner, but perl or python is also ok. 回答1: cut -d: -f1 or awk -F: '{print $1}' or sed 's/:.*//' 回答2: Another pure BASH way: > s='/some/random/file.csv:some string' > echo "${s%%:*}" /some/random/file.csv 回答3: Try this in