This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Question to order chr positions

Hello, I am trying to save multiple files containing SNPs at different chr positions. I run this command on my folder

ls -1v 192_imputed_*.bcf

The output is below, but as you can see 192_imputed_1_1_4516141.bcf is way below the list and it needs to be first, since it contains chr1 at position 1 to 4516141. I think this command is not considering the number of digits.

Any recommendation on how to run this?

Output when running ls -1v 192_imputed_*.bcf:

192_imputed_1_101439501_107834288.bcf
192_imputed_1_106228628_112319459.bcf
192_imputed_1_111003065_116582595.bcf
192_imputed_1_11516244_16620614.bcf
192_imputed_1_115168620_124939835.bcf
192_imputed_1_118703464_152885397.bcf
192_imputed_1_149668258_156865558.bcf
192_imputed_1_15242560_20516362.bcf
192_imputed_1_154762667_160876320.bcf
192_imputed_1_159865632_164865761.bcf
192_imputed_1_163475638_168865730.bcf
192_imputed_1_167675358_173335149.bcf
192_imputed_1_171252117_178662125.bcf
192_imputed_1_176466805_182572121.bcf
192_imputed_1_181009041_187359495.bcf
192_imputed_1_184978611_193504424.bcf
192_imputed_1_189935513_199830441.bcf
192_imputed_1_19516316_24691107.bcf
192_imputed_1_196206348_203080529.bcf
192_imputed_1_1_4516141.bcf
192_imputed_1_201568970_206757061.bcf
192_imputed_1_205518004_210907885.bcf
192_imputed_1_209548028_215053581.bcf
192_imputed_1_213692560_218921916.bcf
192_imputed_1_217434131_223459900.bcf
192_imputed_1_221685953_229121041.bcf
192_imputed_1_226424923_232971735.bcf
192_imputed_1_22860718_29548741.bcf
192_imputed_1_231971761_236971770.bcf
192_imputed_1_235971721_240971799.bcf
192_imputed_1_239948385_244971923.bcf
192_imputed_1_243971915_248945660.bcf
192_imputed_1_26659436_33653616.bcf
192_imputed_1_30868226_37226671.bcf
192_imputed_1_3516132_8962279.bcf
192_imputed_1_35999640_41679122.bcf
192_imputed_1_39975532_47597191.bcf
192_imputed_1_44472694_53987386.bcf
192_imputed_1_51321388_58057424.bcf
192_imputed_1_56791524_61813006.bcf
192_imputed_1_60813000_66446714.bcf
192_imputed_1_64575907_70651641.bcf
192_imputed_1_68293104_76752362.bcf
192_imputed_1_7451971_12516231.bcf
192_imputed_1_74665008_81401929.bcf
192_imputed_1_79634774_85505451.bcf
192_imputed_1_83803364_89907158.bcf
192_imputed_1_87939501_94548743.bcf
192_imputed_1_91602454_98958509.bcf
192_imputed_1_96996841_103370738.bcf
gwas chromosomes

1 answer

Run the output through a sort if the file order matters to you:

/bin/ls -1v *.bcf | sort -t_ -k1,1n -k3,3n -k4,4n -k 5,5n

There might be a better way to write that command though.

EDIT: From the -v, I'm assuming you're trying to leverage GNU ls -v natural sort. If that's the case, ensure you're using GNU ls indeed GNU ls by using man ls. On my mac

$ man ls | grep -- '-v'
     -v      Force unedited printing of non-graphic characters; this is the
$ which gls
/opt/homebrew/bin/gls #This is my homebrew-installed GNU ls
$ man gls | grep -- '-v'
              version (-v), extension (-X), width
       -v     natural sort of (version) numbers within text
       --version

See how my ls has a different -v option than my GNU ls? You might be in a similar situation as well

$ ls -1v | head
192_imputed_1_101439501_107834288.bcf
192_imputed_1_106228628_112319459.bcf
192_imputed_1_111003065_116582595.bcf
192_imputed_1_11516244_16620614.bcf
192_imputed_1_115168620_124939835.bcf
192_imputed_1_118703464_152885397.bcf
192_imputed_1_149668258_156865558.bcf
192_imputed_1_15242560_20516362.bcf
192_imputed_1_154762667_160876320.bcf
192_imputed_1_159865632_164865761.bcf

$ gls -1v | head
192_imputed_1_1_4516141.bcf
192_imputed_1_3516132_8962279.bcf
192_imputed_1_7451971_12516231.bcf
192_imputed_1_11516244_16620614.bcf
192_imputed_1_15242560_20516362.bcf
192_imputed_1_19516316_24691107.bcf
192_imputed_1_22860718_29548741.bcf
192_imputed_1_26659436_33653616.bcf
192_imputed_1_30868226_37226671.bcf
192_imputed_1_35999640_41679122.bcf

$ ls -1v *.bcf | sort -t_ -k1,1n -k3,3n -k4,4n -k 5,5n | head
192_imputed_1_1_4516141.bcf
192_imputed_1_3516132_8962279.bcf
192_imputed_1_7451971_12516231.bcf
192_imputed_1_11516244_16620614.bcf
192_imputed_1_15242560_20516362.bcf
192_imputed_1_19516316_24691107.bcf
192_imputed_1_22860718_29548741.bcf
192_imputed_1_26659436_33653616.bcf
192_imputed_1_30868226_37226671.bcf
192_imputed_1_35999640_41679122.bcf

Log in to answer this question.