conditionally combine text from two files into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conditionally combine text from two files into one
# 1  
Old 04-10-2011
conditionally combine text from two files into one

Hi! I'm trying to take multiple text files (6), which have text on some lines but not others, and combine them. I'd also like to make the values in one column of some of the files (files 4-6) negative. I'm trying to write a short script (see below) as I have to do this with a large number of files. Any advice much appreciated! Example:

Input text file 1:
Code:
0          1.3           1


34         4             1

.....

Input text file 2:
Code:
10          0.3           1


24         3             1

.....

Output text file:
Code:
0          1.3           1
34         4             1
.....
10         0.3          -1
24         3             -1
.....


What I have so far follows. problems: it does not get rid of the rows of empty text (it puts 0.000 in them), and it doesn't work consistently (sometimes it only takes the first line of an input file and ignores the others).
Code:
#!/bin/bash
awk  '{printf("%f %f %f\n", $1, $2, $3)}' *positive_files*.txt > file_1.txt
awk  '{printf("%f %f -%f\n", $1, $2, $3)}' *negative_files*txt >> file_1.txt


Last edited by Franklin52; 04-10-2011 at 03:30 PM.. Reason: Please use code tags
# 2  
Old 04-10-2011
Code:
awk  'NF{printf("%f %f %f\n", $1, $2, $3)}' *positive_files*.txt > file_1.txt
awk  'NF{printf("%f %f -%f\n", $1, $2, $3)}' *negative_files*txt >> file_1.txt

# 3  
Old 04-10-2011
Thank you very much! Thanks also for fixing the formatting of my posted code. f Smilie

Unfortunately this doesn't work though, it still spits out 0s for all the rows that I'm not interested in. I just added a sed command to replace the 0s with spaces to fix this up.

One other thing – it's still doing this weird thing where sometimes it only takes the first line of a text file, any advice? e.g., output might be:

Code:
0          1.3           1
10         0.3          -1
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
24         3             -1
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
.....


Last edited by felix.echidna; 04-10-2011 at 04:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine Two Text Files (PERMUTE)

Hello everybody, I would like to know how can I obtain this: There are two text files.....ffirst.txt and fsecond.txt ffirst.txt contains 5 lines (example): A B C D E fsecond.txt contains 10 lines (example): 1 2 3 4 5 6 (4 Replies)
Discussion started by: gandrinno1
4 Replies

2. Shell Programming and Scripting

Combine two text files

Hi I use Ubuntu 14.04 LTS and bash shell. I need help to write a script to combine two text files. The first file is FIRST.txt <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text... (15 Replies)
Discussion started by: my_Perl
15 Replies

3. Shell Programming and Scripting

Combine the lines from separate text files

Hi All, I have three separate text files which has only one line and i want to combine these lines in one text file which will have three lines. cat file1.txt abc cat file2.txt 1265 6589 1367 cat file3.txt 0.98 0.36 0.5 So, I want to see these three lines in the... (9 Replies)
Discussion started by: senayasma
9 Replies

4. Shell Programming and Scripting

How to replace a text in a file conditionally?

I have got about 100 ascii files and I want replace some variable with a new one on an HP-UX system. But I want to put a line of comments before the change. I want to make file1 to file2. I am explaining below. file1: line1 line2 export QNAME=ABC line4 line5 file2: line1 line2 #... (3 Replies)
Discussion started by: asutoshch
3 Replies

5. UNIX for Dummies Questions & Answers

how to combine text files

how to combine text files in new file and be separated by commas : example: f1.txt 1 2 3 f2.txt x y z f3.txt m n o i need output to be fnew.txt 1,x,m (9 Replies)
Discussion started by: takyeldin
9 Replies

6. Shell Programming and Scripting

Add text at the end of line conditionally

Hi All, I have a file as below: cat myfile abcdef NA rwer tyujkl na I wish to add the text ".txt" at the end of all lines except the lines starting with NA or na. I know i can add text at the end of line using following command but I am not sure how to valiate the condition. (14 Replies)
Discussion started by: angshuman
14 Replies

7. Shell Programming and Scripting

Combine Multiple text or csv files column-wise

Hi All I am trying to combine columns from multiple text files into a single file using paste command but the record length being unequal in the different files the data is running over to the closest empty cell on the left. Please see below. What can i do to resolve this ? File 1 File... (15 Replies)
Discussion started by: venky_ibm
15 Replies

8. Shell Programming and Scripting

Conditionally prepending text

I am currently writing a script to compare a file list created over an FTP connection to a local directory. I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line... (6 Replies)
Discussion started by: Dal
6 Replies

9. Shell Programming and Scripting

how to combine 2 lines in same files based on any text

hi, I want to combine two lines in same file. If the line ends with '&' it should belongs to previous line only Here i am writing example. Ex1: line 1 : return abcdefgh& line 2 : ijklmnopqr& line 3 : stuvw& line 4 : xyz output should be line 1: return abcdefghijklmnopqrstuvwxyz ... (11 Replies)
Discussion started by: spc432
11 Replies

10. UNIX for Dummies Questions & Answers

combine text files into one file

I need to write a shell script which combines/joins 3 text files into one file. Do i put the txt files in the same folder as my script? Here is what i have: #!/bin/bash file1=$1 file2=$2 file3=$3 out="output.txt" count=0 if then echo "$(basename $0) file1 file2 file3" ... (3 Replies)
Discussion started by: zzthejimzz
3 Replies
Login or Register to Ask a Question