This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Need help editing text in the shell

Hello.

I'm learning how to edit text with shell scripting, and I need to change the format of this:

                              > chr1    204073115   204127743
                              > chr6    31164337    31170682
                              > chr17   42313412    42388540

I want to change it to this:

chr1-204073115:204127743    
chr6-31164337:31170682
chr17:42313412-42388540

I have no clue how to do it. I've tried using the command tr, but the results are not what I expected for now. Any ideas?

Thank you

bash shell

1 answer

You can use awk to achieve the desired result as follows:

   awk -F"\t" '{print $1 "-" $2 ":" $3}' yourInputFile.txt 

This command will process the yourInputFile.txt file using awk and perform the specified action. It will take each line of the file and print the contents of the first field ($1), followed by a hyphen (-), then the second field ($2), and finally a colon (:) followed by the third field ($3). The resulting output will be displayed in the console or terminal. Make sure to replace yourInputFile.txt with the actual filename or path to the file you want to process.
To specify the delimiter use the -F option in awk.

tr ">" " " < human_coordinates_diff.txt | awk '{print $2 "-" $3 ":" $4}' human_coordinates_diff.txt > human_coordinates.txt

I used the command tr first to eliminate the >, and then I used the awk command in a similar way as you suggested. It worked!

Thank you so much for your help!

Consider accepting @medhat's answer (green check mark) to provide closure to this thread.

Log in to answer this question.