Adding the content of file in another one...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding the content of file in another one...
# 8  
Old 07-07-2011
Assuming that words "awk" and "Delete_zero_flux" appear together only on the line that you want to change:
Code:
awk 'NR==FNR{x=$0;next}/awk/&&/Delete_zero_flux/{$4=$4x}1' file2 file1

This User Gave Thanks to bartus11 For This Post:
# 9  
Old 07-07-2011
It worked but I noticed something interesting...
I remembered that what I actually need to be put in the file is 220 (that is 221-1) so I did the following:

Code:
$ awk 'NR==FNR{x=$0;next}/awk/&&/Delete_zero_flux/{$4=(($4)-1)x}1' test test2
# Then delete unwanted lines from ALL the files (data + model)
awk '{print "head -222 "$1" > reduce_lines_"$1}' all > Delete_zero_flux
chmod +x Delete_zero_flux
./Delete_zero_flux
ls reduce* > all_reduce

$ awk 'NR==FNR{x=$0;next}/awk/&&/Delete_zero_flux/{$4=(($4)+1)x}1' test test2
# Then delete unwanted lines from ALL the files (data + model)
awk '{print "head -220 "$1" > reduce_lines_"$1}' all > Delete_zero_flux
chmod +x Delete_zero_flux
./Delete_zero_flux
ls reduce* > all_reduce

It is interesting that I actualy have to "add" when in reality I am getting the subtraction output.. any idea why?
# 10  
Old 07-07-2011
You are subtracting from the wrong variable. Try this:
Code:
awk 'NR==FNR{x=$0-1;next}/awk/&&/Delete_zero_flux/{$4=$4x}1' test test2

This User Gave Thanks to bartus11 For This Post:
# 11  
Old 07-07-2011
Now I run into one more problem...
For some files with the numbers (like the one that had 221), the file is blank [It is not a mistake no output means that I don't need to use head -xxx but use the whole file (which has 238 lines)
Is there a way to get the correct expression
Code:
(head -238)

for the cases with blank input files?
# 12  
Old 07-07-2011
Try:
Code:
awk 'NR==FNR{x=$0-1;print x;next}/awk/&&/Delete_zero_flux/{if(x==-1){$3="\"cat";$4=""}else{$4=$4x}}1' test test2

# 13  
Old 07-07-2011
I tried it and this is what I get:

Code:
-1
-1
-1
-1
-1

# 14  
Old 07-07-2011
Woops, that print shouldn't be there Smilie Try now:
Code:
awk 'NR==FNR{x=$0-1;next}/awk/&&/Delete_zero_flux/{if(x==-1){$3="\"cat";$4=""}else{$4=$4x}}1' test test2

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

4. Shell Programming and Scripting

Adding Content to Variable (bash)

is this possible? its kind of like incrementing the value of a number in a variable. but in this case, instead of the value of the variable being a number, it's just contents/strings/characters/alpha-numeric etc. NOT a number. For instance: VAR=Tommy for all in $(blah blah) do ... (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

Adding content of two file in a single file column wise

Hi, I am trying to get the file in particular pattern using shell script. I have to add one column to some other file. For example consider two file as below. File1: name1 name2 name3 File2: Add1 age1 Add2 age2 Add3 age3 I want this two file in a single file format something like... (3 Replies)
Discussion started by: diehard
3 Replies

6. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

7. Shell Programming and Scripting

adding the content of a file to another file

hi guys, I posted a similar question about reading a file and adding its content to another file and i used sed '/HELLO/r fileB' fileA however this command adds the content of fileB under the word "HELLO" what if i need to add the word above "HELLO". what could i use? Thanks, (6 Replies)
Discussion started by: ROOZ
6 Replies

8. Shell Programming and Scripting

adding the content of a file to another file

Hi everyone, I am trying to search for a pattern(in this case copyright) in file A and then add the content of file B under the pattern(copyright) found in file A i did the following set var=`cat ~/b` sed "/copyright/ a\${var}" ~/a this does it job partially because it does not keep the... (4 Replies)
Discussion started by: ROOZ
4 Replies

9. Shell Programming and Scripting

Adding filename into file content

Dear Experts, Please help to teach me how to add the filename into the file content. Actually the file name are EVENTS-20050912. ***************New output that I want*************** EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013 EVENTS-20050912 12:10:28 ALARM: BTSSPAN-297-2... (1 Reply)
Discussion started by: missutoomuch
1 Replies
Login or Register to Ask a Question