0

The text file I have now looks like this,

VOG0005              -          k141_44786_1         -            4.9e-11   41.0   0.0     2e-08   32.4   0.0   2.0   2   0   0   2   2   2   2 -   
VOG0005              -          k141_46357_20        -            9.2e-44  148.8   1.7   2.4e-32  111.2   0.2   3.0   2   1   1   3   3   3   3 -   
VOG0010              -          k141_1081_2          -            1.2e-06   27.4   0.5   0.00011   21.0   0.0   2.3   2   0   0   2   2   2   2 -
VOG0010              -          k141_17432_67        -            1.2e-07   30.7   0.0   1.7e-07   30.2   0.0   1.3   1   1   0   1   1   1   1 -
VOG0010              -          k141_2610_7          -            2.1e-06   26.6   0.0   2.9e-06   26.2   0.0   1.1   1   0   0   1   1   1   1 

Does the sort command count the hyphen and the spaces in between as separate columns? If yes, then how can I use the cut command to remove the hyphen in between?

VOG0005 - k141_44786_1 - 4.9e-11

to get something like this

VOG0005 k141_44786_1 4.9e-11

i.e., with a single space would work for me. I have tried one command: cut -f2 File.txt, but it doesn't seem to work in my case.

Can anyone help please?

1

3 Answers 3

0

You can use sed command to remove the hyphen with spac.

sed 's/- //g'

Please refer the below example

# cat example.txt
VOG0005 - k141_44786_1 - 4.9e-11 41.0 0.0 2e-08 32.4 0.0 2.0 2 0 0 2 2 2 2 -
VOG0005 - k141_46357_20 - 9.2e-44 148.8 1.7 2.4e-32 111.2 0.2 3.0 2 1 1 3 3 3 3 -
VOG0010 - k141_1081_2 - 1.2e-06 27.4 0.5 0.00011 21.0 0.0 2.3 2 0 0 2 2 2 2 -
VOG0010 - k141_17432_67 - 1.2e-07 30.7 0.0 1.7e-07 30.2 0.0 1.3 1 1 0 1 1 1 1 -
VOG0010 - k141_2610_7 - 2.1e-06 26.6 0.0 2.9e-06 26.2 0.0 1.1 1 0 0 1 1 1 1 -

# sed 's/- //g' example.txt
VOG0005 k141_44786_1 4.9e-11 41.0 0.0 2e-08 32.4 0.0 2.0 2 0 0 2 2 2 2 -
VOG0005 k141_46357_20 9.2e-44 148.8 1.7 2.4e-32 111.2 0.2 3.0 2 1 1 3 3 3 3 -
VOG0010 k141_1081_2 1.2e-06 27.4 0.5 0.00011 21.0 0.0 2.3 2 0 0 2 2 2 2 -
VOG0010 k141_17432_67 1.2e-07 30.7 0.0 1.7e-07 30.2 0.0 1.3 1 1 0 1 1 1 1 -
VOG0010 k141_2610_7 2.1e-06 26.6 0.0 2.9e-06 26.2 0.0 1.1 1 0 0 1 1 1 1 -
3
  • Thank you so much for the help, I will understand sed commands now, and return for further help if needed. :-)
    – Vishu1511
    Commented Feb 20, 2020 at 6:54
  • I was able to remove the "-" sign but the spaces still remains there. Actually I further want to "sort" and remove the similar rows (According to k141_44786_1 column, which have some similar acc. no. which needs to be removed), in the table for which I am using "sort -k1,1 -k12,12nr -k11,11n example.txt | sort -u -k1,1 --merge" command. But It is not giving the desired results which I need. Can you please help me on that as ell.?
    – Vishu1511
    Commented Feb 20, 2020 at 8:37
  • @Vishu1511 please edit your post and add all the details
    – annahri
    Commented Feb 20, 2020 at 9:00
0

Similar to sed, you can also do it using perl:

$ cat File.txt | tr -s " " | perl -pe 's/[[:space:]]-//g' | sponge File.txt
VOG0005 k141_44786_1 4.9e-11 41.0 0.0 2e-08 32.4 0.0 2.0 2 0 0 2 2 2 2
VOG0005 k141_46357_20 9.2e-44 148.8 1.7 2.4e-32 111.2 0.2 3.0 2 1 1 3 3 3 3
VOG0010 k141_1081_2 1.2e-06 27.4 0.5 0.00011 21.0 0.0 2.3 2 0 0 2 2 2 2
VOG0010 k141_17432_67 1.2e-07 30.7 0.0 1.7e-07 30.2 0.0 1.3 1 1 0 1 1 1 1
VOG0010 k141_2610_7 2.1e-06 26.6 0.0 2.9e-06 26.2 0.0 1.1 1 0 0 1 1 1 1

or

<File.txt tr -s " " | perl -pe 's/[[:space:]]-//g' | sponge File.txt

# on fish shell
tr -s " " <File.txt | perl -pe 's/[[:space:]]-//g' | sponge File.txt

Explanation

tr -s " "                    
# Removes extra spaces
perl -pe 's/[[:space:]]-//g' 
# Replaces any ' -' with nothing
sponge File.txt              
# Reads standard input and writes it out to the specified file.

Apparently you can achieve the same result using only perl (thanks @Stéphane Chazelas) :

perl -pi -e 's/ +/ /g; s/[[:space:]]-//g' File.txt

similarly sed:

sed -i -E 's/ +/ /g; s/[[:space:]]-//g' File.txt

see @Stéphane Chazelas comment for explanation

4
  • 1
    There's nothing bash/zsh-specific in the last command. That's Bourne/POSIX standard shell syntax. It would also work in csh. The only reason it wouldn't work in rc or derivatives is because of the double quotes instead of single quotes. The only shell it wouldn't work in would be fish, where redirections can't precede the command name. It would have to be tr <file.txt -s " " or tr -s " " <file.txt... Commented Feb 20, 2020 at 8:13
  • 1
    tr -s ' ' can be achieved in perl with s/ +/ /g; so you could do the whole thing with perl -pi -e 's/ +/ /g; s/[[:space:]]-//g' File.txt Commented Feb 20, 2020 at 8:16
  • Thanks for the knowledge! @StéphaneChazelas
    – annahri
    Commented Feb 20, 2020 at 8:46
  • Thank you so much for the help Annahri & Stéphane Chazelas , this solved all my problems even those which I didn't asked for, here.
    – Vishu1511
    Commented Feb 20, 2020 at 9:15
0

First of all, yes, sort will regard the first - as being column 2, the k141_xxxx as column 3, the second - as column 4, etc.  But that’s an incomplete oversimplification; the truth is subtle and complex.

Your question is not entirely clear.  To clobber the second and fourth columns, whatever they are, do

awk '{ $2=""; $4=""; print }' example.txt

To clobber any fields that are a hyphen, do

awk '{for (i=1; i<=NF; i++) if ($i == "-") $i = ""; print}' example.txt

awk is overkill (but then, so is perl).

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .