0

I'm having a folder with CSVs like:

A.csv

pet1;dog
pet2;monkey

B.csv

pet1;cat
pet2;wolf

I can now reformat those in bash (Linux):

for filename in *.csv; do
  CSVBody=$(cat $filename | cut -d ";" -f2 | paste -sd ";" -)
  echo -e "$CSVBody" >> ./converted/merged.csv
done

To get

merged.csv

dog;monkey
cat;wolf

Unfortunately, I'm a long time away of windows. How would I cut, paste and pipe to a file in bash?

Anatol
  • 1,923
  • 6
  • 26
  • 55
  • Possible duplicate of [Combining multiple text files into one](https://stackoverflow.com/questions/14521799/combining-multiple-text-files-into-one) –  Sep 03 '19 at 12:25

1 Answers1

0

An alternative to reading files in parallel is storing one set in a pseudo hash table

:: Q:\Test\2019\09\03\SO_57771554.cmd
@Echo off & Setlocal EnableDelayedExpansion

for /f "tokens=1* delims=;" %%A in (A.csv) do Set "File1[%%A]=%%B"

for /f "tokens=1* delims=;" %%A in (B.csv) do Echo !File1[%%A]!;%%B

> Q:\Test\2019\09\03\SO_57771554.cmd
dog;cat
monkey;wolf

EDIT due to Aacini's valuable hint, I did it wrong.

:: Q:\Test\2019\09\03\SO_57771554.cmd
@Echo off & Setlocal EnableDelayedExpansion
for %%F in (*.csv) do (
    Set "Line="
    for /f "tokens=1* delims=;" %%A in (%%F) do Set "Line=!Line!;%%B"
    Echo(!Line:~1!
)

> SO_57771554_2.cmd
dog;monkey
cat;wolf
  • 3
    Mmm... I don't know if my English language understanding is not enough, but I can see in the question that the OP [wants] "to get merged.csv" with `dog;monkey` and `cat;wolf` rows. Where in the question specify that the desired result is the transposed one? – Aacini Sep 03 '19 at 13:05
  • @Aacini That's true - his sample ***transposes*** rows/columns per file. –  Sep 03 '19 at 14:04