Need help editing text in the shell
1
0
Entering edit mode
10 months ago

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 • 697 views
ADD COMMENT
0
Entering edit mode

are you converting BED to intervals ? see also: Cheat Sheet For One-Based Vs Zero-Based Coordinate Systems

ADD REPLY
3
Entering edit mode
10 months ago
Medhat 9.7k

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.

ADD COMMENT
0
Entering edit mode
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!

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1602 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6