Use text of one file to save in another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use text of one file to save in another
# 1  
Old 03-24-2015
Use text of one file to save in another

I am trying to use the text of one file as a text file name with and the text of another as the contents of that text file. Is this possible? Thank you Smilie.

Code:
 
For example, in the two files attached, target.txt has: 
1.txt 
2.txt 
and out_parse.txt has:
13	20763642	20763642	C	G
13	20763438	20763438	C	G

What I can not seem to do is use the first line of target.txt as the text file name and combine it with the first line of out_parse.txt and so on for the second.

Desired output:
1.txt
13	20763642	20763642	C	G

2.txt
13	20763438	20763438	C	G


Thank you Smilie.
# 2  
Old 03-24-2015
Try:
Code:
awk '{close(fname)} (getline fname<f)>0 {print>fname}' f=target.txt out_parse.txt

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-24-2015
Code:
$
$ paste -d: target.txt out_parse.txt | sed -e 's/:/\n/' -e 's/$/\n/'
1.txt
13      20763642        20763642        C       G

2.txt
13      20763438        20763438        C       G
$
$

This User Gave Thanks to durden_tyler For This Post:
# 4  
Old 03-24-2015
Thank you both Smilie.
# 5  
Old 03-25-2015
Note that the results of durden_tyler's solution and mine differ a lot, because of ambiguity in your requirements.

I read it so that the output would produce 2 different files with the file names specified in the target file, each with one line from the out_parse file

durden_tyler's output produces one file with both the input and the file names...

To avoid this kind of confusion, you need to put more effort still in specifying your requirements..


----
Quote:
Originally Posted by durden_tyler
Code:
$
$ paste -d: target.txt out_parse.txt | sed -e 's/:/\n/' -e 's/$/\n/'
[..]

Note: only GNU sed supports \n in the replacement part of the s function.
# 6  
Old 03-25-2015
Thank you for the clarification and I apologize for the ambiguity... I will put for more effort in being more specific. Thank you Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Save line from text in variable

Hi, I wrote a csh script where I want to save in a loop each time a different line from a text file (att_file) in the $name variable. But it seems not to work. att_file looks like: 123123123 345345345 345345345 set name = `head -n $count $att_file | tail -n 1 | awk '{print $1}'` Do... (3 Replies)
Discussion started by: MLImag
3 Replies

2. Shell Programming and Scripting

How to save the 'nmon' output to a text file with a script?

Hi all, I want to do an Unix Script to save the 'nmon' output on a text file and I don't know how to do it. I need a Script for each monitoring and also one to multiple monitorings. Thanks (6 Replies)
Discussion started by: Javi1990
6 Replies

3. Programming

Save output in text C++

Hi , i want to save the output of my c ++ code to a text file which is in a particular path : this is part of my code and I dunno where I am doing it wrong do { for( int i = 0; i < l; ++i ) { std::cout << 1 + k * i + index << ' '; } ... (2 Replies)
Discussion started by: siya@
2 Replies

4. Shell Programming and Scripting

Read a file name from a text file and save it in a variable

i have a text file consists of different file names like: line 1: lib/libIMb.so message broker file line 2: lil/imbdfg.lil message broker file i need to extract libIMb.so and imbdfg.lil files from those lines and save them in a variable. so that i can search for... (9 Replies)
Discussion started by: santosh2626
9 Replies

5. Shell Programming and Scripting

Save result to a text file

Currently I have a perl code to combine two different files. #! /usr/bin/perl -w use strict; open FP1,"A.txt"; open FP2,"B.txt"; my ($l1,$l2); while(1) { $l1=<FP1>; chomp $l1; $l2=<FP2>; chomp $l2; last unless(defined $l1 or defined $l2); print "$l1 $l2\n"; } close FP2;... (4 Replies)
Discussion started by: Tzeronone
4 Replies

6. Programming

Extract text from file and save as individual file

Dear All I am using the following shell script to extract the columns from the file. for filename in *.rpt do awk -F"\t" ' BEGIN {OFS="|"} FNR > 0 {print $1,$2,$3,$5,FILENAME} ' $filename >> output.txt done However, the script works fine. Instead of saving the single... (4 Replies)
Discussion started by: bala06
4 Replies

7. 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

8. 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

9. Shell Programming and Scripting

Data fetched from text file and save in a csv file

Hi i have wriiten a script which fetches the data from text file, and saves in the output in a text file itself, but i want that the output should save in different columns. I have the output like: For Channel:response_time__24.txt 1547 data points 0.339 0.299 0.448 0.581 7.380 ... (1 Reply)
Discussion started by: rohitkalia
1 Replies

10. Shell Programming and Scripting

can I save list of files in memory and not in text file?

Hello all im using allot with the method of getting file list from misc place in unix and copy them into text file and then doing misc action on this list of files using foreach f (`cat file_list.txt`) do something with $f end can I replace this file_list.txt with some place in memory? ... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question