anikaM
anikaM

Reputation: 429

How to delete multiple first columns from multiple files?

I have multiple files like this:

trans_ENSG00000047849.txt.traw
trans_ENSG00000047848.txt.traw
trans_ENSG00000047847.txt.traw
...

and each has around 300 columns. Columns are separated with tab.I would like to remove the first 7 columns from each of those files.

I know how to do it for each file:

cut  -f 7- trans_ENSG00000047849.txt.traw > trans_ENSG00000047849.txt.trawN

Is there is a way to do it at once for all files?

NOTE: there is a tab at the beginning. Therefore I used here cut -f 7 rather than cut -f 8 to remove the first 7 columns.

Upvotes: 1

Views: 922

Answers (2)

Tyl
Tyl

Reputation: 5262

Backup your files first, and try this (GNU sed):

sed -ri 's/^([^\t]*\t){7}//' trans_*.txt.traw

As -i to sed will change your files in place. (You can remove the i for testing).

Eg:

$ cat file
1       2       3       4       5       6       7       8       9       0
a       b       c       d       e       f       g       h       i       j

        dfad            da
$ sed -ri 's/^([^\t]*\t){7}//' file

$ cat file
8       9       0
h       i       j

        dfad            da

However, the command's for simple, so it won't remove when there're less than 7 columns. (Guess you won't have lines like this, right?)

If you still want to remove when there're less than 7 columns:

sed -r 's/^([^\t]*(\t|$)){,7}//'

Upvotes: 0

Barmar
Barmar

Reputation: 782785

Just use a for loop:

for file in *.txt.traw
do 
    cut  -f 7- "$file" > "$file"N
done

Upvotes: 1

Related Questions