Command to merge several file as one
Hi...All, there are nearly 70 files(notes) in a folder which belongs to one particular course and the file's names are not arranged numerically when I do ls command. The file's names are like 1_some_name.txt, 2_some_other_name.txt 3_some_different_name.txt and so on up to 69_some_last_filename.txt. I merged all files as one using below command.
"ls | sort -n | for i in `awk '{print $1}'`; do cat $i >> ../Newfile.txt; done" It worked great and seems above command has not missed any of the file. When I opened Newfile.txt found it is some difficult to find file-wise since either there is no gap or no any main heading between the files. So I just wanted that the file names(1_some_name.txt, 2_some_other_name.txt) only should be heading between every files and at least 3 lines gap should be in between each and every file. So could anybody please tell me how it can be possible. |
Just add an echo command in the loop
Code:
do echo -e "\n\n\n$i"; cat $i >> ../Newfile.txt; done Perhaps Code:
for i in ?_*.txt ; do echo -e "\n\n\n$i"; cat $i >> ../Newfile.txt; done Code:
for i in ??_*.txt ; do echo -e "\n\n\n$i"; cat $i >> ../Newfile.txt; done |
I personally think it's better to start by ensuring that the filenames are all zero padded to the same depth. Then it becomes safer and easier to use simple globbing and a loop, like the one given above. The shell will do all the sorting for you.
Code:
Code:
#!/bin/bash |
Command to merge several file as one
Sorry for the late response. And I am sorry if my poor English is not able to define that what I need. I ran below command but still filenames are not coming as heading in the Newfile.txt before file's content starts.
"ls | sort -n | for i in `awk '{print $1}'`; do echo -e "\n\n\n$i"; cat $i >> ../Newfile.txt; done" It added all file's contents as filewise(from 1 to 69) in Newfile.txt but I need these below each and every output to be heading before each and every file content in the Newfile.txt. It is just echo the 69 filenames in the output for above command and not adding those filenames to the file. see the below output 64_SSH_Introduction.txt 65_IPTables.txt 66_IPv6_IPTables.txt 67_NMap_Introduction.txt 68_Nessus_Introduction.txt 69_Snort_Sniffer_Logger.txt This is what I needed as to be heading i.e 1_first_file.txt should be as heading in the first line of Newfile.txt and the content of 1_first_file.txt should come after this heading, then 2_second_file.txt as heading after first file's content, then content of 2_second_file.txt and so on upto 69_Snort_Sniffer_Logger.txt I removed Newfile.txt and ran the below command inside the same directory. "for i in ?_*.txt ; do echo -e "\n\n\n$i"; cat $i >> ../Newfile.txt; done" found below output and found empty Newfile.txt when I cat it. "; cat $i >> ../Newfile.txt; done ?_*.txt cat: ?_*.txt: No such file or directory" Then I ran below command "for i in ??_*.txt ; do echo -e "\n\n\n$i"; cat $i >> ../Newfile.txt; done" and found there is no difference between "ls | sort -n | for i in `awk '{print $1}'`; do echo -e "\n\n\n$i"; cat $i >> ../Newfile.txt; done" and above command. I mean only all 69 file's contents are coming serially but not heading along with content which is explained in previous paragraph. So could you please show how to modify this command. Quote:
|
Command to merge several file as one
Quote:
mv: `./68_Nessus_Introduction.txt' and `./68_Nessus_Introduction.txt' are the same file mv: `./69_Snort_Sniffer_Logger.txt' and `./69_Snort_Sniffer_Logger.txt' are the same file |
Well, as you know there are 70 files each starting with a number, how about something like:
Code:
#!/bin/bash |
I see the problem with what I originally posted. Forgot to redirect the output of echo.
Code:
for i in ?_*.txt ; do echo -e "\n\n\n$i" >> ../Newfile.txt; cat $i >> ../Newfile.txt; done |
Command to merge several file as one
Super, I tried "??" instead of "?" It gave the output what I exactly wanted. With single question mark("?") I got the below error
cat: ?_*.txt: No such file or directory. I just wanted to clarify one thing. "\n\n\n" is nothing but the 3 lines gap between files. Isn't it? Anyway thank you master for giving some idea to use advanced commands like ??_*.txt instead ls, sort etc. Quote:
|
Command to merge several file as one
Thanks for your suggestion with shell script. I copied the script as it is to test file inside the same directory and executed. Found "0" for the status "echo $?" and Newfile.txt file is created in the same directory but it is empty when I cat it.
Quote:
|
Quote:
Yes, the \n\n\n sequence simply outputs three new lines. |
Quote:
Code:
1_blah.txt |
Command to merge several file as one
Thank you all, Let me have a deep look into the David and Grail scripts and then try.
Quote:
|
All times are GMT -5. The time now is 10:42 PM. |