Sponsored Content
Full Discussion: Concatenate 560 files in one
Top Forums UNIX for Dummies Questions & Answers Concatenate 560 files in one Post 302761745 by bakunin on Saturday 26th of January 2013 05:49:16 PM
Old 01-26-2013
You didn't tell us if there is any naming convention on the file names, so it is difficult to suggest a loop where you don't have to name the possible file names but instead generate them. In any case the loop you use should NOT be a for-loop, because it can break if a file glob expands to something you didn't foresee.

Instead, use a while-loop, like this:

Code:
ls <some-file-glob> | while read filename ; do
     # do something with "$filename"
done

To add a file to another without the first two lines you can use "sed":

Code:
sed -n '3,$ p' inputfile >> collectfile

So, putting it together, your script could look like this - you will have to supply the correct file glob instead of "*input", which i used for demonstration purposes:

Code:
#! /bin/ksh

typeset outfile="/path/to/output"
typeset infile=""

rm -rf "$outfile" 2>/dev/null >/dev/null

ls *input | while read infile ; do
     if [ -e "$outfile" ] ; then
          sed -n '3,$ p' "$infile" >> "$outfile"
     else
          cp "$infile" "$outfile"
     fi
done

This will copy the first input-file (preserving the 2-line header) and cut off the header of any following file. "if [ -e <filename> ]" means: if "<filename>" exists and is a regular file. Because any eventually existing output file is deleted prior to the loop this is not the case with the first file.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to concatenate all files.

Hi, I'm totally new to Unix. I'm an MVS mainframer but ran into a situation where a Unix server I have available will help me. I want to be able to remotely connect to another server using FTP, login and MGET all files from it's root or home directory, logout, then login as a different user and do... (1 Reply)
Discussion started by: s80bob
1 Replies

2. Shell Programming and Scripting

concatenate two files with different No of rows

need a shell which perform following function file 1 ( every time new data comes) 1212 2323 3434 4545 5656 . . . . file 2 (fixed line) update bc_tbl set aix=data , bix=back where cix=U and serial=; now when i execute shell it will concatinate file 1, file 2 & make file 3 as... (3 Replies)
Discussion started by: The_Archer
3 Replies

3. Shell Programming and Scripting

Concatenate files

I have directory structure sales_only under which i have multiple directories for each dealer example: ../../../Sales_Only/xxx_Dealer ../../../Sales_Only/yyy_Dealer ../../../Sales_Only/zzz_Dealer Every day i have one file produce under each directory when the process runs. The requirement... (3 Replies)
Discussion started by: mohanmuthu
3 Replies

4. Shell Programming and Scripting

Concatenate files

Hi, I want to create a batch(bash) file to combine 23 files together. These files have the same extension. I want the final file is save to a given folder. Once it is done it will delete the 23 files. Thanks for help. Need script. (6 Replies)
Discussion started by: zhshqzyc
6 Replies

5. Shell Programming and Scripting

Concatenate files

I have a file named "file1" which has the following data 10000 20000 30000 And I have a file named "file2" which has the following data ABC DEF XYZ My output should be 10000ABC 20000DEF (3 Replies)
Discussion started by: bobby1015
3 Replies

6. Fedora

Concatenate Numerous Files

Hey! I wanted to find a text version of the Bible for purposes of grepping. The only files I could find, (in the translation I wanted), were Old Testament.txt and New Testament.txt. I thought, "fine, I'll just concatenate those two, no problemo." But when I unpacked them, turns out they had each... (22 Replies)
Discussion started by: sudon't
22 Replies

7. UNIX for Dummies Questions & Answers

Concatenate Several Files to One

Hi All, Need your help. I will need to concatenate around 100 files but each end of the file I will need to insert my name DIRT1228 on each of the file and before the next file is added and arrived with just one file for all the 100files. Appreciate your time. Dirt (6 Replies)
Discussion started by: dirt1228
6 Replies

8. UNIX for Dummies Questions & Answers

Concatenate files

Hi I am trying to learn linux step by step an i am wondering can i use cat command for concatenate files but i want to place context of file1 to a specific position in file2 place of file 2 and not at the end as it dose on default? Thank you. (3 Replies)
Discussion started by: iliya24
3 Replies

9. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies
SHCOMP(1)						       Korn shell utilities							 SHCOMP(1)

NAME
shcomp - compile a shell script SYNOPSIS
shcomp [ options ] [infile [outfile]] DESCRIPTION
Unless -D is specified, shcomp takes a shell script, infile, and creates a binary format file, outfile, that ksh can read and execute with the same effect as the original script. Since aliases are processed as the script is read, alias definitions whose value requires variable expansion will not work correctly. If -D is specified, all double quoted strings that are preceded by $ are output. These are the messages that need to be translated to locale specific versions for internationalization. If outfile is omitted, then the results will be written to standard output. If infile is also omitted, the shell script will be read from standard input. OPTIONS
[D dictionary] Generate a list of strings that need to be placed in a message catalog for internationalization. [n noexec] Displays warn- ing messages for obsolete or non-conforming constructs. [v verbose] Displays input from infile onto standard error as it reads it. EXIT STATUS
0 Successful completion. >0 An error occurred. SEE ALSO
ksh(1) IMPLEMENTATION
version shcomp (AT&T Research) 2003-03-02 author David Korn <dgk@research.att.com> copyright Copyright (c) 1982-2010 AT&T Intellectual Property license http://www.opensource.org/licenses/cpl1.0.txt 2003-03-02 SHCOMP(1)
All times are GMT -4. The time now is 10:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy