Home » Infrastructure » Linux » Iteration through all files and concatenating file name with contents. (GNU/Linux x86_64 )
Iteration through all files and concatenating file name with contents. [message #597740] Tue, 08 October 2013 01:15 Go to next message
Manoj.Gupta.91
Messages: 239
Registered: March 2008
Location: Delhi
Senior Member
Hi All,

I'm new to Linux. I'm facing an issue. I need to iterate through all the files in a folder and concatenate File Name with the contents of file and store output in one file.

Eg.

File1 (Contents)
7       1       0       1
6       1       0       1
5       1       0       1
4       1       0       1
3       1       0       1
2       1       0       1
1       1       0       1

File2 (Contents)
664     1       0       1
664     1       0       1
664     1       0       1
664     1       0       1
664     1       0       1
664     1       0       1
664     1       0       1
664     1       0       1
664     1       0       1


File3(Expected Output after processing)
File1	7       1       0       1
File1	6       1       0       1
File1	5       1       0       1
File1	4       1       0       1
File1	3       1       0       1
File1	2       1       0       1
File1	1       1       0       1
File2	664     1       0       1
File2	664     1       0       1
File2	664     1       0       1
File2	664     1       0       1
File2	664     1       0       1
File2	664     1       0       1
File2	664     1       0       1
File2	664     1       0       1
File2	664     1       0       1


I did tried to work on this but facing some issue.
sed 's/^/\t/' File1.txt | sed 's/^/File1.txt/' >> File3.txt
sed 's/^/\t/' File2.txt | sed 's/^/File2.txt/' >> File3.txt

This sed script works for individual file. But I've about 500 files. One more issue I'm facing is with the file naming convention. Some example names are as below.

11File.txt
- File2.txt
(-) File3.txt
File_4.txt
File-5.txt
(+) File6.txt
+ File2.txt
-TMS_S.txt


I also tried to create a loop for iteration through all the files in a folder but it is giving me some error.
#!/bin/sh -f
for myfile in *.txt
do
    echo $myfile
done


'yScript.sh: line 3: syntax error near unexpected token `do
'yScript.sh: line 3: `do


Please help me to solve this issue.

Thanks & Regards
Manoj

[Updated on: Tue, 08 October 2013 01:22]

Report message to a moderator

Re: Iteration through all files and concatenating file name with contents. [message #597742 is a reply to message #597740] Tue, 08 October 2013 01:46 Go to previous message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
This question bears no real connection with Oracle database.
Please find a more appropriate forum.
The topic is locked. /forum/fa/448/0/
If I'm wrong, please, PM me (or report this message to a moderator, explain why you think it should be unlocked and it might be done).

Regards
Michel
Previous Topic: /tmp directory problem
Next Topic: differentiate root level and dba tasks
Goto Forum:
  


Current Time: Thu Mar 28 04:31:11 CDT 2024