1

I have text file having name like this:

map_leaf_M_BAN.AC.txt   
map_leaf_M_BAN.A.txt    
map_leaf_M_BAN.C.txt    
map_leaf_M_BAR.AC.txt

I need these name in text file like this

map_leaf_M_BAN.AC.txt,map_leaf_M_BAN.A.txt,map_leaf_M_BAN.C.txt,map_leaf_M_BAR.AC.txt

Please suggest How I can do this?

Kind regards

1

2 Answers 2

3

If you have the names in a text file (names.txt) already, you just want to replace all newlines with commas:

$ perl -pe 's/\s*\n/,/' names.txt 
map_leaf_M_BAN.AC.txt,map_leaf_M_BAN.A.txt,map_leaf_M_BAN.C.txt,map_leaf_M_BAR.AC.txt,$

But that still needs a final newline and has an extra comma in the end. So remove those with:

$ perl -pe 's/\s*\n/,/' names.txt | sed 's/,$/\n/'
map_leaf_M_BAN.AC.txt,map_leaf_M_BAN.A.txt,map_leaf_M_BAN.C.txt,map_leaf_M_BAR.AC.txt

And, to save this in a new file, just redirect the output:

perl -pe 's/\s*\n/,/' names.txt | sed 's/,$/\n/' > newFile.txt
0

Try:

ls *txt | paste -s -d, > newFile.txt
3
  • I'm not sure why this has been downvoted; it seems to me to be a more-or-less valid attempt to answer a slightly different interpretation of the OP's "I have text file having name like this" Mar 26, 2019 at 23:26
  • Dunno. There's nothing in the question about "sed only answer", and you don't even need to have the names inside a txt file.
    – JucaPirama
    Mar 27, 2019 at 13:10
  • Arguably ls * | ... is bad practice because it will be subject to word splitting; for the filenames in the OP that's not an issue but a more general solution would probably be something like printf '%s\0' *txt | paste -zs -d, (which makes everything in the pipeline null-delimited) Mar 27, 2019 at 13:15

You must log in to answer this question.

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