Computing.Net > Forums > Unix > concatenate files with identifier

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to get for your free account now!

concatenate files with identifier

Reply to Message Icon

Name: Yoon
Date: November 16, 2008 at 14:34:10 Pacific
OS: window
CPU/Ram: 2gb
Manufacturer/Model: dell
Comment:

Hi all

I concatenate files using cat. Each file contains only one csv data. for example,

file1
100

file2
200

cat file1 file2 > newfile

newfile
100
200

However, is there any way to insert identifier when using cat command?

for example,

newfile should have

file1 100
file2 200


Thanks in advance.


Report Offensive Message For Removal

Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 16, 2008 at 17:00:37 Pacific
Reply:

No, but this script solves the problem:

# no error checking
rm -f newfile
for i in file1 file2
do
printf "%s " $i >> newfile
cat "$i" >> newfile
done



Report Offensive Follow Up For Removal

Response Number 2
Name: Yoon
Date: November 16, 2008 at 17:24:02 Pacific
Reply:

Thanks a lot
However, I got

file1100
file2200

Is there any way to create CSV (comma delimited) or insert space?


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: November 16, 2008 at 18:10:36 Pacific
Reply:

I don't know what shell you are using (I'm using ksh), but this printf statement:

printf "%s " $i >> newfile

has a space after the %s format qualifer, and it works fine.

This example uses a pipe symbol instead of the space:

printf "%s|" $i >> newfile


Report Offensive Follow Up For Removal

Response Number 4
Name: Yoon
Date: November 17, 2008 at 11:45:35 Pacific
Reply:

Thanks a lot!!
You saved my life!


Report Offensive Follow Up For Removal
Reply to Message Icon

Related Posts

See More







Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: concatenate files with identifier

Concatenate Files with similar name www.computing.net/answers/unix/concatenate-files-with-similar-name/5940.html

rm files with same prefix, diff suf www.computing.net/answers/unix/rm-files-with-same-prefix-diff-suf/5644.html

Bash: rename files with 2 spaces www.computing.net/answers/unix/bash-rename-files-with-2-spaces/7140.html