Need help with file substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with file substitution
# 1  
Old 09-17-2008
Need help with file substitution

hello all
just trying to write a script that will read a file (line by line) and substitute (tht line) in a different file if (tht line) is present in second file

Ex: script has to read file A (line by line) and if file B has tht line it should get substituted with the line in file A

file A:

length=30

threads=40

file B:

threads=20

The script has to read file A (line by line) and has to substitute file B in this case threads=20 with threads=40. Please let me know if guys have any ideas.
# 2  
Old 09-17-2008

Code:
NL='
'
fileB=$( cat fileB )
while read line
do
  [ -z "$line" ] && continue
  case $fileB in
    *"${line%%=*}="*)  printf "%s\n" "$line" > fileB ;;
  esac
done < fileA

# 3  
Old 09-18-2008
Assuming the stuff behind the equals sign is what is permitted to vary,

Code:
sed -n 's/^\([^=]*=\)\(.*\)/s%\1.*%\1\2%/p' fileA | sed -f - fileB

# 4  
Old 09-18-2008
thanks guys (cfajohnson & era) you guys are a great help...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Extension Substitution

Hi, I have a script in which the file name is always known, but the extension could vary. I want to be able to use a single variable; no if-else statements. For example, if I have config.txt in the directory then that's what I want to use, but if config.xml is in the directory then use that. The... (9 Replies)
Discussion started by: ocbit
9 Replies

2. Shell Programming and Scripting

Multiple variable substitution in a file in one go

I have a huge script which is defining variables with full path of commands in the beginning of code and using those variables in the script. For Example: ECHO=/bin/echo LS=/bin/ls SED=/bin/sed AWK=/bin/awk UNAME=/bin/uname PS=/bin/ps DATE=/bin/date GREP=/bin/grep $ECHO "hello... (1 Reply)
Discussion started by: veeresh_15
1 Replies

3. Shell Programming and Scripting

Awk:substitution of characters between two file elements

Hi, I have file1 like this: a 64 b 66 c 67and file2 like this: @1234 1123 aabbcc @5453 5543 ccbaI want to replace each letter of the third line in file2 with corresponding number in file1. So desired output is, @1234 1123 646466666767 @5453 5543 67676664I tried something like... (4 Replies)
Discussion started by: polsum
4 Replies

4. Shell Programming and Scripting

cat file with variable substitution

MyFile contains: ALTER TABLE $DBN.$TBN ADD $COL $TYP COMPRESS ($VAL); I need to cat the file and have it substitute all of the variables with their contents. cat MyFile does not work. The following works for the first line, but errors on the second line because of the paren: $ while read... (2 Replies)
Discussion started by: Phil27577
2 Replies

5. Shell Programming and Scripting

Text substitution & getting file name from url

hi, sorry if this seems trivial. i have a file url.txt which consists of a list of urls (it was supposed to be my wget -i file). however, since the server from which i am trying to download uses redirect, wget dows not remeber the filename of ther original url will save to a file name which is... (3 Replies)
Discussion started by: texttoolong
3 Replies

6. Shell Programming and Scripting

How to change a substitution value in a file?

Hi, i want to change a subs. value in a file, with using a script.. this is my script... (example) #!/bin/bash NDIR=`zenity --file-selection --directory` mv $HOME/Desktop/myfile /tmp/myfile.temp XT='"' perl -pe "s/.*/DIR=$XT`echo -e "$NDIR"`$XT/ if $. == 40" <... (2 Replies)
Discussion started by: excsra
2 Replies

7. Solaris

Shell variables substitution in a file

Hi, I have to read a file and translate the contents including substituting the variables if any and write to another file without using sed or awk. For ex:- inputfile.txt ----------- servername=$SERVER application=$APPL outputfile.txt ------------ servername=actual server name... (2 Replies)
Discussion started by: axes
2 Replies

8. Shell Programming and Scripting

encrytion/substitution issue in a file

1) ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547425069636596::6:N:mrs charles:N:PH:00010031:0001' OUTPUT - ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547******636596::6:N:mrs charles:N:PH:00010031:0001' The... (5 Replies)
Discussion started by: mad_man12
5 Replies

9. Shell Programming and Scripting

Can process substitution be used as an input file to another program?

Hey, I was trying to figure out how to launch a program from the command line, and it works if you pass it a config file. I was thinking about writing a script to dynamically create the config file and pass it to the command using something like command substitution (so I don't actually have to... (3 Replies)
Discussion started by: bj0
3 Replies

10. Solaris

column wise substitution in a file

Hi, I have two files. Want to make an addition of the fifth column of from both the files and redirect it to a third file. Both files have same records except fifth field and same record should be inserted into new file having fifth field as addition of fifth fields of both files. for... (2 Replies)
Discussion started by: sanjay1979
2 Replies
Login or Register to Ask a Question