An alternative: cut -d <string>? An alternative: cut -d <string>? shell shell

An alternative: cut -d <string>?


awk does allow a string as delimiter:

$ awk -F"_upstream_" '{print $1}' fileaedes_aegyptianopheles_albimanusanopheles_arabiensisanopheles_stephensiculex_quinquefasciatusdrosophila_melanogaster

Note for the given input you can also use cut with _ as delimiter and print first two records:

$ cut -d'_' -f-2 fileaedes_aegyptianopheles_albimanusanopheles_arabiensisanopheles_stephensiculex_quinquefasciatusdrosophila_melanogaster

sed and grep can also make it. For example, this grep uses a look-ahead to print everything from the beginning of the line until you find _upstream:

$ grep -Po '^\w*(?=_upstream)' fileaedes_aegyptianopheles_albimanusanopheles_arabiensisanopheles_stephensiculex_quinquefasciatusdrosophila_melanogaster


If you only want the first field, you could do this in pure bash:

ls | while read line; do echo "${line%%_upstream_*}"; done


You could also use sed:

sed -i.bak 's/_upstream.*//' file

Result:

aedes_aegyptianopheles_albimanusanopheles_arabiensisanopheles_stephensiculex_quinquefasciatusdrosophila_melanogaster

Note: This will also create a backup of the original file as file.bak.