How to add data from 2 input files and save it in 1 output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add data from 2 input files and save it in 1 output file
# 1  
Old 12-15-2010
How to add data from 2 input files and save it in 1 output file

Hi,

i have 2 input files which are file1.txt and file2.txt. I need to extract data from file1.txt and file2.txt and save it in file3.txt like example below:-

File1.txt
ID scrap1
Name scrap1
start 1
end 10

ID scrap2
Name scrap2
start 11
end 15

ID scrap3
Name scrap3
start 20
end 70

File2.txt
N size1.pf size1.fna
N size2.pf size2.fna
N size3.pf size3.fna

the input are from 2 different files and the output should be in file3.txt and should have as follows:-

File3.txt
scrap1 scrap1 N size1.pf size1.fna
scrap2 scrap2 N size1.pf size1.fna
scrap3 scrap3 N size1.pf size1.fna

the first 2 columns has the same data but for different field which are ID and Name. How to do this in perl or sed? i have more than 10k data to deal with in this way. appreciate if u could help on this. thanks
# 2  
Old 12-15-2010
Code:
awk 'NR!=FNR{print L[FNR],$0; next} $1=="ID"{I=$2}
    $1=="Name"{L[++p]=I FS $2}' file1.txt file2.txt > file3.txt

# 3  
Old 12-16-2010
hi,

thanks for your reply... It works...but it just show the first line only.. Smilie
# 4  
Old 12-16-2010
Funny, it's working fine here:
Code:
$ awk 'NR!=FNR{print L[FNR],$0; next} $1=="ID"{I=$2}
    $1=="Name"{L[++p]=I FS $2}' file1.txt file2.txt > file3.txt
$ cat file3.txt
scrap1 scrap1 N size1.pf size1.fna
scrap2 scrap2 N size2.pf size2.fna
scrap3 scrap3 N size3.pf size3.fna

Which first line is it printing? Are you on solaris, try nawk instead of awk
# 5  
Old 12-16-2010
no, i'm just using ubuntu..i'm new in this scripting stuff. i did try your script again and again just now but i got the same result. it just give me 1 row instead of 3 rows like u did..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add coordinates to line of output extracted from input file

I am trying to compare/confirm the output of an script using the perl below, which does execute. However I can not seem to print $1 and $2 in each line of the input separated by a tab in each line of the output. The input file is quite large so I have only included a portion, but the format is the... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Open Text file input data and save it.

Hi Guys, I have blank file A.txt I will run the script xyz.sh First i want to open a.txt file... Now i will enter some data like XYZ ABC PQR .. Save it and keep continue my script.... END of my script. Thanks (1 Reply)
Discussion started by: asavaliya
1 Replies

4. Shell Programming and Scripting

adding data in input file if 2nd script output SUCCESS

Hi All, how can i edit my original data and add more data if my 2nd script outputs SUCESS? ex. input file: 1.txt nik,is,the 1script.sh if 2ndscript.sh output SUCCESS then i'm going to edit my input file and add data best,pogi.. sample outputdata. nik,is,the,best,pogi 2ndscript.sh... (3 Replies)
Discussion started by: nikki1200
3 Replies

5. Shell Programming and Scripting

split input data file and put into same output file

Hi All, I have two input file and need to generate a CSV file. The existing report just "GREP" the records with the Header and Tailer records with the count of records. Now i need to split the data into 25 records each in the same CSV file. id_file (Input file ) 227050994 232510151... (4 Replies)
Discussion started by: rasmith
4 Replies

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

8. Shell Programming and Scripting

select data from oracle table and save the output as csv file

Hi I need to execute a select statement in a solaris environment with oracle database. The select statement returns number of rows of data. I need the data to be inserted into a CSV file with proper format. For that we normally use "You have to select all your columns as one big string,... (2 Replies)
Discussion started by: rdhanek
2 Replies

9. Shell Programming and Scripting

building output file from multiple input files

Hi there, I am trying to figure out a way to combine multiple sources with different data on a single file, and I am trying to find the best way to do it. I have multiple files, let's say A, B, C and D. A has a field in common with B, B has a field in common with C, and C has a field in... (2 Replies)
Discussion started by: ppucci
2 Replies

10. Shell Programming and Scripting

Replacing data of output file with input

Hi, I have a ksh which peocess and get me data from 3 days... ie if i process it on jan 28.. it gets data for 25, 26 and 27.... the process run every day and get previous 3 days data...all this data is appened to a file lets call time.out Now time.out cannot have deplicate data so what i want... (10 Replies)
Discussion started by: bhagya2340
10 Replies
Login or Register to Ask a Question