hash
hash

Reputation: 103

printing results in one line separated by commas in bash

How can I print all text file location separated by commas in one line? Can I do this in for loop?

Here is an example of files.

/data/home/files/txt_files_1/file1.txt
/data/home/files/txt_files_1/file2.txt
/data/home/files/txt_files_1/file3.txt

/data/home/files/txt_files_2/file1.txt
/data/home/files/txt_files_2/file2.txt
/data/home/files/txt_files_2/file3.txt

output would look like

/data/home/files/txt_files_1/file1.txt,/data/home/files/txt_files_1/file2.txt,/data/home/files/txt_files_1/file3.txt \

/data/home/files/txt_files_2/file1.txt,/data/home/files/txt_files_2/file2.txt,/data/home/files/txt_files_2/file3.txt

Thanks

Here is the correct code

#!/bin/bash
delim=""
  for i in /data/home/files/txt_files_1/file*
  do
      printf "%s%s" "$delim" "$i"
      delim=","
  done
   printf "\\"
   printf "\n"


  for i in /data/home/files/txt_files_2/file*
  do
      printf "%s%s" "$delim" "$i"
      delim=","
  done

Upvotes: 1

Views: 224

Answers (4)

konsolebox
konsolebox

Reputation: 75478

For single file input:

awk -v OFS=, -v RS= 'NF { $1 = $1; print }' file

Output:

/data/home/files/txt_files_1/file1.txt,/data/home/files/txt_files_1/file2.txt,/data/home/files/txt_files_1/file3.txt
/data/home/files/txt_files_2/file1.txt,/data/home/files/txt_files_2/file2.txt,/data/home/files/txt_files_2/file3.txt

Or

awk -v OFS=, -v RS= -v ORS='\n\n' 'NF { $1 = $1; print }' file

Output:

/data/home/files/txt_files_1/file1.txt,/data/home/files/txt_files_1/file2.txt,/data/home/files/txt_files_1/file3.txt

/data/home/files/txt_files_2/file1.txt,/data/home/files/txt_files_2/file2.txt,/data/home/files/txt_files_2/file3.txt

Upvotes: 2

Tom Fenech
Tom Fenech

Reputation: 74595

Assuming your input is in a file called list, this Perl one-liner does the job:

perl -F'\n' -00 -ane 'push @a, join(",", @F) }{ print(join(" \\\n\n", @a), "\n")' list

explanation

  • -00, in combination with -n, reads the file one block (paragraph) at a time.
  • The -a switch in combination with -F'\n' auto-splits the text on each new line. The result goes into the array @F.
  • An array is built, each element containing the comma separated list of the elements in @F
  • Once the file has been processed, all the elements of the array @a are printed, joined together as you specified. The additional "\n" on the end is optional.

Output:

/data/home/files/txt_files_1/file1.txt,/data/home/files/txt_files_1/file2.txt,/data/home/files/txt_files_1/file3.txt \

/data/home/files/txt_files_2/file1.txt,/data/home/files/txt_files_2/file2.txt,/data/home/files/txt_files_2/file3.txt

Upvotes: 1

Louis Kottmann
Louis Kottmann

Reputation: 16618

<command to generate lines of paths> | tr '\n' ','

example:

echo "/data/home/files/txt_files_1/file1.txt
/data/home/files/txt_files_1/file2.txt
/data/home/files/txt_files_1/file3.txt

/data/home/files/txt_files_2/file1.txt
/data/home/files/txt_files_2/file2.txt" | tr '\n' ','

outputs:

/data/home/files/txt_files_1/file1.txt,/data/home/files/txt_files_1/file2.txt,/data/home/files/txt_files_1/file3.txt,,/data/home/files/txt_files_2/file1.txt,/data/home/files/txt_files_2/file2.txt

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328556

You can use printf "%s," "$file" to print several names into a single line. To get the delimiters right, I use this trick:

delim=""
...loop...
    printf "%s%s" "$delim" "$file"
    delim=","
printf "\n"

Upvotes: 1

Related Questions