concatenating similar files in a directory

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting concatenating similar files in a directory
# 1  
Old 04-28-2011
concatenating similar files in a directory

I am new in unix.
I have below requirement:
I have two files at the same directory location
File1.txt and File2.txt (just an example, real scenario we might have File2 and File3 OR File6 and File7....)

File1.txt has :

File2.txt has:

I want to keep File2.txt as below and delete the File1.txt.

File2.txt will have:

Can anyone please provide some help here.

Last edited by zaxxon; 04-28-2011 at 03:00 PM.. Reason: code tags
# 2  
Old 04-28-2011
You may do that in the following steps:
1. head -1 File2.txt > result.tmp
2. grep 'record1' File1.txt >> result.tmp
3. awk 'NR>1' File2.txt > result.tmp
4. mv result.tmp File2.txt

Kinda cumbersome, but this is what comes to my head at the first sight of your question.
I know this can be achieved more elegantly with SED, but SED is still strange to me.
# 3  
Old 04-28-2011
With sed:
$ sed '/header/ a\'$(grep record f1)'' f2 > newfile

That a\ will append the text at the address /header/. You could then mv newfile to f2 and sowith overwrite it. Then rm f1. If your sed supports the -i flag, you can directly edit f2 so you'd just remove f1 afterwards.
# 4  
Old 04-28-2011
One caveat, does the trailer record have a count of the detail records.
# 5  
Old 04-28-2011
The trailer does have a count of the records.

---------- Post updated at 01:00 PM ---------- Previous update was at 12:50 PM ----------

Thanks for all the replies....
first problem is to identify that which one is file created earlier and which one later. It could be file1 and file2 or file6 and on. I am writting this in a shell script. FILE_NAME=`echo File*` will give me
FILE_NAME=File1 File2.
I want to get it in seperate variable as below.

so that i can use the logic suggested by all of you.

Last edited by Deepak62828r; 04-28-2011 at 04:02 PM.. Reason: If at a time there are two files File1 and File2. File2 is the latest one
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to move the files older than x days with similar directory structure?

Hello, I need to move all the files inside /XYZ (has multi-depth sub directories) that are older than 14 days to/ABC directory but with retaining the SAME directory structure. for example: /XYZ/1/2/3/A/b.txt should be moved as /ABC/1/2/3/A/b.txt I know about find /XYZ -type f -mtime +14... (3 Replies)
Discussion started by: prvnrk
3 Replies

2. Shell Programming and Scripting

Finding files in directory with similar names

So, I have a directory tree that has many files named thusly: X_REVY.PDF I need to find any files that have the same X portion (which can be nearly anything) as any another file (in any directory) but have different Y portions (which can be any number from 1-99). I then need it to return... (3 Replies)
Discussion started by: Kamezero
3 Replies

3. Shell Programming and Scripting

To find ls of similar pattern files in a directory by passing the variable.

Hi, I have file in my $datadir as below :- SAT_1.txt SAT_2.txt BAT_UD.lst BAT_DD1.lst DUTT_1.txt DUTT_la.txt Expected result :- should get all the above file in $<Filename>_file.lst Below is my code :- for i in SAT BAT DUTT do touch a.lst cd $datadir (1 Reply)
Discussion started by: satishmallidi
1 Replies

4. Shell Programming and Scripting

Concatenating contents of a file with members in a directory

Hi, I have a unix file with the below structure - CustId1 CustName1 CustPhn1 /u/home/xmldata/A000001 CustId2 CustName2 CustPhn2 /u/home/xmldata/A000002 CustId3 CustName3 CustPhn3 /u/home/xmldata/A000003 Then I have another unix directory /u/home/xmldata This directory has... (3 Replies)
Discussion started by: Simanto
3 Replies

5. Shell Programming and Scripting

Find the similar directory

Hi I have one directory whose name i don't remember exactly only starting letter i know which is Resp. Can you please let me know the command to find the similar directory in the root. Rajesh (3 Replies)
Discussion started by: guddu_12
3 Replies

6. UNIX for Dummies Questions & Answers

Delete X amount of similar files in a directory

Need a Unix command to save the last 20 versions of a file in a specific directory and delete everything else. Date is irrelevant. Anyone aware of such an animal? In my test, I came up with: ls -t1 /tmp/testfile* | tail -n +20 | xargs rm I don’t quite trust the author though! (1 Reply)
Discussion started by: rwsherman
1 Replies

7. Shell Programming and Scripting

concatenating the filenames in a directory

hi all, I have a requirement where in i have to read all the filenames based on a pattern from a directory and concatenate all these file names and write it to another file. i am using the following code to do this var1='' for filename in $_DIR/${FILE_NAME}* do if if then... (7 Replies)
Discussion started by: nvuradi
7 Replies

8. Shell Programming and Scripting

Concatenating two files

HI I need to concatenate two files which are having headers. the result file should contain only the header from first file only and the header in second file have to be skipped. file1: name age sriram 23 file2 name age prabu 25 result file should be name age sriram 23 prabu ... (6 Replies)
Discussion started by: Sriramprabu
6 Replies

9. UNIX for Dummies Questions & Answers

Concatenating records from 2 files

I'm trying to concatenate records from 2 files and output it to a third file. The problem I'm running into is that it seems like the "While" command is limited to processing one file at a time. It seems like you could read a record from file1 into a variable. Then do the same for the for file2.... (4 Replies)
Discussion started by: Powcmptr
4 Replies

10. UNIX for Dummies Questions & Answers

concatenating x files into a one...

... i have 4 files to concatenate but in a certain order and i wanted to do it in a shorter one line command , if possible ! 4 files : file , file0 , file1 and file2 file1 into file2 file0 into the result file into the result thanks in advance Christian (1 Reply)
Discussion started by: Nicol
1 Replies
Login or Register to Ask a Question

Featured Tech Videos