regex - Bash: list different prefixes of files -


suppose have series of files, listed as:

t001_000.txt t001_001.txt t001_002.txt t005_000.txt t005_001.txt t012_000.txt ... t100_000.txt 

we want merge files same t??? prefix. example, every file prefix t001 want do:

merge t001_*.txt > newt001.txt #i made function 

how bash list of different prefixes?

here's pure bash way of getting prefixes:

for file in *.txt      echo "${file%_*.txt}" done | sort -u 

this give list of file prefixes. there, use cat.

the for loop goes through of files. for file in t*_*.txt limit files you're picking up.

the ${file%_*.txt} small right pattern filter removes _*.txt variable $file. sort -u sorts of these prefixes, , combines duplicates.

the best way use function:

function prefix {     file in *.txt             echo "${file%_.txt}"     done | sort -u }  prefix | while read prefix    ${prefix}_*.txt > cat $prefix.txt done 

note ${...} around name. that's because $prefix_ valid shell script variable. need ${prefix} let shell know i'm talking $prefix , not $prefix_.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -