Modify a file by another file: add new line and variable after string is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify a file by another file: add new line and variable after string is found
# 1  
Old 05-04-2012
Modify a file by another file: add new line and variable after string is found

hello,

I have problem with writing/adjusting a shell script.
I searched forum and unfortunately couldn't write scipt based on the information I found.
I never wtire such so it's hard for me and I do need to modify one script immediately.

case looks like:
1. 'file' that needs to be modified
2. 'script' - it has commands in order to modify a 'file'

'script' has to modify 'file' according to the rule:
Each time when a 'string' is found inside a 'file' then a new line should be added with variable and its value.
example:
'file' has:

Code:
something, something
something string more
something, something
hello world, string

'script' should serach for 'string' and each time when it find it then a new line with variable $$var1=5 should be added.
after modification 'file' should look like:

Code:
something, something
something string more
$$var1=5
something, something
hello world, string
$$var1=5

can someone tell me how to do this?

thanks a lot!

Last edited by Scrutinizer; 05-04-2012 at 02:15 PM..
# 2  
Old 05-04-2012
What shell is this?

What have you tried?
# 3  
Old 05-07-2012
Hi, do you something mean like this?
Code:
awk '/string/{ $0=$0 RS v }1' v='$$var1=5' infile

Why the double $-signs and should it be always the same variable?
# 4  
Old 05-07-2012
Try this in bash shell....

code:

Code:
while read line
do
count=`echo $line | grep 'string' | wc -l `
if [ $count = 1 ]
then
        echo $line
        echo '$$var1=5'
else
        echo $line
fi
done < infile.txt > temp
rm infile.txt
mv temp infile.txt


Last edited by raadhaakrishnan; 05-07-2012 at 04:04 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

2. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

3. Shell Programming and Scripting

How to pre-append a variable string to each line in a file?

How to pre-append a variable string to each line in a file contains both single and double quotes? The variable string has no quotes in it. thank you very much. :confused: (8 Replies)
Discussion started by: dtdt
8 Replies

4. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

8. Shell Programming and Scripting

add string and time stamp on each line of file

I have file A.txt A 1023 B 123 C 1223 I want output Hello_12PM_A 1023 Hello_12PM_B 123 Helll_12PM_C 1223 Add Hello and time stamp in AM and PM. (4 Replies)
Discussion started by: asavaliya
4 Replies

9. Shell Programming and Scripting

Add Some String on each line of file

HI Guys, I am new user its my first post. I have one file in DIR XYZ. File name is ABCDEF.log Now i want to add that file in each line of file. I have data in file ABCDEF.log TestKLFH TEstLKHU HESTLJHG Now i want to update the file with below data ABCDEFG.log:TestKLFH... (5 Replies)
Discussion started by: asavaliya
5 Replies

10. UNIX for Advanced & Expert Users

delete line from file if successful partial string found

Id like to delete a line from a file using (preferably a single line unix command) if it contains a certain string pattern. If line contains "abcdef" then delete that line. Help greatly appreciated. (7 Replies)
Discussion started by: cronjob78
7 Replies
Login or Register to Ask a Question