1

I've 672 files in local directory, filenames are like: boutique-*_20160116hh0000.csv, general-tgbt_1_20160116hh0000.csv, where hh - hours' number and (for instance, boutique-a13_15_20160116060000.csv, boutique-a16_18_20160116070000.csv, boutique-a05_7_20160116190000.csv and so on).

I run the following command in bash:

(for i in `ls -1 | sort`; do cat $i | while read line; do echo "${i%%_2016*}," $line; done ; done;) > c.csv

And it leads to infinite loop, file c.csv grows in size.

Whats the reason?

I believe that my c.csv file is also falling into result of

`ls -1 | sort`

But is this behaviour correct?

After getting the first comment I try another test - keep only 4 files instead of 672 (boutique-a05_7_20160116220000.csv, boutique-a16_18_20160116020000.csv, general-tgbt_1_20160116170000.csv, wago-pfc-750-815_99_20160116020000.csv) and my command works fine.

Thanks

5
  • 2
    ( commands here not run until at least ) > output_here_is_opened
    – mikeserv
    Jan 20 '16 at 13:00
  • You seem to have created two accounts. Please see here for help on merging them.
    – terdon
    Jan 20 '16 at 14:27
  • Also, what exactly are you trying to do? What output are you expecting? You have chosen a very complicated way to do something that, if I understand correctly, is very simple. Please edit your question and explain what your final objective is, not just the command you used to achieve it.
    – terdon
    Jan 20 '16 at 14:30
  • Thanks. Indeed I want to put prefix from filename to each line of the file, merge that content in one big file (in sorted order). Although I'm curious about more simple ways always, the first reason of my question - what is the proper (maybe defined in any standard) way for bash to execute these command? Should my c.csv be also in the list from ls?
    – Yabel
    Jan 21 '16 at 11:08
  • mikeserv, Could you please clarify is behaviour you mentioned specified in any standard (or official documentation)? I'm confused that when I kept only 4 files instead of 672 (boutique-a05_7_20160116220000.csv, boutique-a16_18_20160116020000.csv, general-tgbt_1_20160116170000.csv, wago-pfc-750-815_99_20160116020000.csv) my command works fine.
    – Yabel
    Jan 23 '16 at 9:35
0

It's better to put the result elsewhere. The ls output is already sorted, but you don't need that anyway. Shorten your script to:

for i in *.csv ; do
    while read line ; do
        echo "${i%%_2016*}," $line
    done <$i
done >/tmp/c.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.