Reputation: 42050
Suppose I am writing a shell script foo.bash
to concatenate the contents of test/*.txt
with a comma like that:
> cat test/x.txt
a b c
> cat test/y.txt
1 2 3
> foo.bash test
a b c,
1 2 3
How would you write such a script ?
Upvotes: 0
Views: 80
Reputation: 3441
You could use regex to do achive it.The command below grabs the content of every file and appends it after the name of the file ($ARGV
).
$ grep -ER '*'
a.txt:a
b.txt:b
c.txt:c
$ perl -pe 's/^(.*)\n$/$ARGV:\1,/;' * > file.txt
$ cat file.txt
a.txt:a,b.txt:b,c.txt:c,
Upvotes: 0
Reputation: 133538
Could you please try following(in case you want to concatenate lines of files, line by line with comma).
paste -d, *.txt
EDIT2: To concatenate all .txt files contents with ,
try following once(needed GNU awk
).
awk 'ENDFILE{print ","} 1' *.txt | sed '$d'
Upvotes: 1
Reputation: 5762
What about
for file in /tmp/test/*.txt; do
echo -n "$(cat "$file"),"
done | sed 's/.$//'
or maybe
for file in /tmp/test/*.txt; do
sed 's/$/,/' "$file"
done | sed 's/.$//'
Upvotes: 1