Substitute one line of multiple files according to another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitute one line of multiple files according to another file
# 8  
Old 05-26-2015
Quote:
Originally Posted by RudiC
How about
Code:
awk ' 
NR==FNR         {if (/avg_ins/) {ZW=NR
                                 $0="avg_ins="
                                }
                 TMP[NR]=$0
                 MAX=NR
                 next  
                }
                {SUP[ZW]=$2
                 for (i=1; i<=MAX; i++) print TMP[i] SUP[i]  > $1".config" 
                }
' template table
... ... ...

Depending on the version of awk being used and the number of config files being created, it might be safer to change the line:
Code:
                 for (i=1; i<=MAX; i++) print TMP[i] SUP[i]  > $1".config"

to:
Code:
                 for (i=1; i<=MAX; i++) print TMP[i] SUP[i]  > ($1".config")
                 close($1".config")

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 05-27-2015
Hi, Don!
I am using gawk 4.0.1. Could you please elaborate why it might be saferwith your modification?
Thanks!
# 10  
Old 05-27-2015
There's two modifications:
- the string concatenation might not work correctly for redirection file names on all system unless parenthesized (worked for me...)
- ALL awks will run out of file descriptors, some sooner (about 10), others later. So closing them if no more needed puts you on the safe side in either case at the (small) cost of a file operation.

Last edited by RudiC; 06-08-2015 at 10:01 AM.. Reason: typo
This User Gave Thanks to RudiC For This Post:
# 11  
Old 05-27-2015
Thanks a lot!
# 12  
Old 05-27-2015
Expanding a little bit on what RudiC said: The standards do not specify the precedence of string concatenation and input or output redirection. So the statement:
Code:
print TMP[i] SUP[i] > $1".config"

can be evaluated as if it were written as:
Code:
print TMP[i] SUP[i] > ($1".config")

(as it is with gawk and many other versions of awk), or as:
Code:
(print TMP[i] SUP[i] > $1)".config"

(as it is with awk on BSD systems, OS X systems, and many others). Explicitly adding the parentheses makes it clear which you want and makes that part of your code work with any version of awk in case you ever try to move your script to a different operating system.

And since I learned how to use awk on a system that only allowed ten open file descriptors (including the current input file and standard output), I always close file descriptors I no longer need unless I know (as part of the script specifications) that fewer than eight additional input and output files will be used during the lifetime of the script.
This User Gave Thanks to Don Cragun For This Post:
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 substitute a word in multiple file?

Team, I want to change below parameter in all the files in a directory, Check for HOSTNAME=`hostname` Change to HOSTNAME=localhost And I tried below but, its not working ☹ find /tmp -type f -exec sed 's/"HOSTNAME\=\`hostname\`"/"HOSTNAME\=localhost/g'" Help me if I am missing... (6 Replies)
Discussion started by: natraj005
6 Replies

2. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

3. Shell Programming and Scripting

To substitute multiple variable by their content in a file

Dear All, I would like to instantiate a "pattern file" substituting "variable name" by their content. Saying, we have: 1/ a "pattern file" containing different "variable name", the first character of the "variable name" is "@": $ cat TPTModl.txt DEFINE... (12 Replies)
Discussion started by: dae
12 Replies

4. Shell Programming and Scripting

awk - Multiple files - 1 file with multi-line data

Greetings experts, Have 2 input files, of which 1 file has 1 record per line; in 2nd file, multiple lines constitute 1 record; Hence declared the RS=";" Now in the first file which ends with ";" at each line of the line; But \nis also being considered as part of the data due to which I am... (1 Reply)
Discussion started by: chill3chee
1 Replies

5. Shell Programming and Scripting

Append Multiple files with file name in the beginning of line

Hi, I have multiple files having many lines like as bvelow: file Name a.txt abc def def xyz 123 5678 file Name b.txt abc def def xyz 123 5678 I would like to append files in the below format to a new file: file Name c.txt (7 Replies)
Discussion started by: rramkrishnas
7 Replies

6. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

7. Shell Programming and Scripting

Split a file into multiple files based on line numbers and first column value

Hi All I have one query,say i have a requirement like the below code should be move to diffent files whose maximum lines can be of 10 lines.Say in the below example,it consist of 14 lines. This should be moved logically using the data in the fisrt coloumn to file1 and file 2.The data of first... (2 Replies)
Discussion started by: sarav.shan
2 Replies

8. Shell Programming and Scripting

substitute a line in a file if line number is available

Hi guys, is there are way to substitute the content of certain line in the file by another entry if line number is available? For example, I have a variable A="HCMLPBBG" and a file MYFILE. I need to substitute entry on line 18168 of MYFILE with the value of the variable "A". Is there a way to... (1 Reply)
Discussion started by: aoussenko
1 Replies

9. Shell Programming and Scripting

substitute a line in file

i have an file ,i want to substitute line 003 M 33 22 22 00 WITH NEW 003 M 24 26 28 00 how can i do it (2 Replies)
Discussion started by: RahulJoshi
2 Replies

10. UNIX for Dummies Questions & Answers

Ksh Storing Multiple Files and reading each line in each file.

How would I go about storing multiple file paths in a directory that begin like: 20080402* and run a loop that reads each line of each file thats in a given directory. So far this is what I have: #!/bin/ksh echo "ENTER Reprint Date (YYYYMMDD): " read ReprintDate echo ""... (1 Reply)
Discussion started by: developncode
1 Replies
Login or Register to Ask a Question