Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Parts of String With Sed

Tags:

linux

bash

unix

sed

I have lines of data that looks like this:

sp_A0A342_ATPB_COFAR_6_+_contigs_full.fasta
sp_A0A342_ATPB_COFAR_9_-_contigs_full.fasta
sp_A0A373_RK16_COFAR_10_-_contigs_full.fasta
sp_A0A373_RK16_COFAR_8_+_contigs_full.fasta
sp_A0A4W3_SPEA_GEOSL_15_-_contigs_full.fasta

How can I use sed to delete parts of string after 4th column (_ separated) for each line. Finally yielding:

sp_A0A342_ATPB_COFAR
sp_A0A342_ATPB_COFAR
sp_A0A373_RK16_COFAR
sp_A0A373_RK16_COFAR
sp_A0A4W3_SPEA_GEOSL
like image 795
neversaint Avatar asked Jun 24 '10 02:06

neversaint


People also ask

How do I remove a character from a string in Linux?

You can also use the sed command to remove the characters from the strings. In this method, the string is piped with the sed command and the regular expression is used to remove the last character where the (.) will match the single character and the $ matches any character present at the end of the string.

How do you use sed command to replace a string in a file?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.

How do I remove a character from a Unix file?

Using the truncate Command. The command truncate contracts or expands a file to a given size. The truncate command with option -s -1 reduces the size of the file by one by removing the last character s from the end of the file.


1 Answers

cut is a better fit.

cut -d_ -f 1-4 old_file

This simply means use _ as delimiter, and keep fields 1-4.

If you insist on sed:

sed 's/\(_[^_]*\)\{4\}$//'

This left hand side matches exactly four repetitions of a group, consisting of an underscore followed by 0 or more non-underscores. After that, we must be at the end of the line. This is all replaced by nothing.

like image 68
Matthew Flaschen Avatar answered Sep 21 '22 14:09

Matthew Flaschen