I have to merge the data in all the files in a folder such that the data of the earliest file come first then the second file's data and so on. Please help.
You can use ls with suitable options to generate a list of the directories contents in the proper order. That list can be piped into a while-loop for reading. Use test/[ to verify that the item is indeed a file before cat'ing. Redirect the output of the while-loop to a file of your choice.
That's an unsafe and unreliable way to work with a list of filenames.
That for-loop is badly broken. Instead of the four files, only two are printed, one of them three times. Pathname expansion (globbing) after the command substitution replaces ? with ? a and [a] with a, leading to the three instances of a. The -n is treated is silently consumed by echo as an option.
Beware pathname expansion after command substitution. Beware feeding echo arbitrary arguments. printf is safer and portable (and, like echo, usually a shell builtin).
Regards,
Alister
---------- Post updated at 06:14 PM ---------- Previous update was at 06:10 PM ----------
Quote:
Originally Posted by Lem
Usually it's a very bad thing to use ls instead of find for task like this, but... being really careful, try to see:
If done correctly, there's absolutely nothing wrong or unsafe about piping ls into a while-read loop. The only text it cannot handle is a newline in a pathname because ls and read use it as their delimiter.
Your example, however, isn't well-constructed. You must use IFS= to prevent trimming of leading/trailing whitespace and -r to treat backslashes literally. To read arbitrary lines of text verbatim: IFS= read -r line
Regards,
Alister
Last edited by alister; 06-13-2012 at 07:36 PM..
These 2 Users Gave Thanks to alister For This Post:
Thanks all for reply..I need to append the timestamp of the file at the end of each record as well...I am new to UNIX,so please provide the code.Thanks for the help..
I knew. When I wrote "be very careful", I meant: I know that a lot of things can go wrong... but at the moment I couldn't say precisely what they are.
I'm just a shell newbie, and bash is only my latest hobby. So please take it into account when I write some nonsense. I'm sorry in advance.
Quote:
You must use IFS= to prevent trimming of leading/trailing whitespace and -r to treat backslashes literally. To read arbitrary lines of text verbatim: IFS= read -r line
Ok, it's easy to remember. "IFS= ", and read will catch the whole line 100% of times. Thanks.
Hi Team,
I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder.
Eg:
parent folder --> /Interface/data/test/IN
Sub folder -->/Interface/data/test/IN/Invoice20180607233338
Subfolder will be always with timestamp... (6 Replies)
I need help to write shell script to copy files from one server to another server.
Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Dear Friends,
I am looking for a shell script to merge input files into one file .. here is my idea:
1st paramter would be outfile file (all input files content)
read all input files and merge them to input param 1
ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Hi,
I had a folder ABC . Was trying to move it . But the command was randomly stopped by mistake. Now , I have folder ABC in 2 location with different half data at each path . How do I merge both the folders and sub folders into a single one ?
Any help is appreciated. (2 Replies)
Friends,
I need help with the following in UNIX.
Merge all csv files in one folder considering only 1 header row and ignoring header of all other files.
FYI - All files are in same format and contains same headers.
Thank you (4 Replies)
Hi all,
I have copied all my files to one folder.and i want to check how many files (count) in the folder recently moved or total files in my folder?
please send me the query asap. (3 Replies)
Hi all:
I have a problem with a C++ service runing on solaris 10. This service only set a signal on oracle table. When the service detect a cut off on the line (tcp/ip), trigger a cobol program for set the signal OFF. When the line is on again, the service trigger the same cobol program for set... (0 Replies)
Hello Friends,
I want to merge two files in ascending order on the first field. And if the first field matches sort on 3rd field i.e, TXADDR should come ahead of RXADDR .
file1
9 : TXADDR : 00000000
65 : TXDATA 0000000000000011
83 : TXDATA 0000000000000012
453 :... (10 Replies)
Hello all,
I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written.
I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)