Replace string - searching from input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace string - searching from input file
# 1  
Old 09-21-2015
Replace string - searching from input file

Hi I need help with writing a script to change a string in a file.

The script needs to read an input list (list.txt) file line by line searching for that string in a text.file. Once the string is found the last few words in the string should be replaced.

eg list.txt will contain
Code:
hello my name is dave
hello our name is dave

text.file will contain data such as
Code:
hello my name is dave
hello mikes name is really dave
hello our name is dave
hello we are dave

once the string is matched it should be changed to :
Code:
hello my name is irrelevant now

The text.file should end up as follows if the script has worked:-
Code:
hello my name is irrelevant now
hello mikes name is really dave
hello our name is irrelevant now
hello we are dave

I hope i've explained the problem well enough

Last edited by Scrutinizer; 09-21-2015 at 12:12 PM.. Reason: code tags
# 2  
Old 09-21-2015
Any attempts from your side?
# 3  
Old 09-21-2015
sed '/^hello my name is /s/[^ ]*$/irrelevant now/g' myFile
# 4  
Old 09-21-2015
Indeed i've tried to do this using a for loop and sed but not sure if this is the best way of achieving this as i'll need another list for the replacement variable:-

Code:
for i in `cat text.file`
do
sed "s/\$i/$var1" text.file > text.file.new

# 5  
Old 09-21-2015
sed '/^hello my name is /s/[^ ]*$/irrelevant now/g' myFile
or expanded:
Code:
awk -v str='irrelevant now' 'FNR==NR{f1[$0];next} $0 in f1 {$NF=str}1' list.txt text.file

# 6  
Old 09-21-2015
Try also
Code:
awk  'NR==FNR {T[$0];next} $0 in T {sub (/ [^ ]*$/, " irrelevant now")}1' file1 file2
hello my name is irrelevant now
hello mikes name is really dave
hello our name is irrelevant now
hello we are dave

# 7  
Old 09-21-2015
Sorry, but this question reeks like homework so much i have to close it.

Moderator's Comments:
Mod Comment @sudobash: if this is homework please say so. (send a PM to me or any other moderator) The thread will be transferred to the homework and coursework forum and you can continue there. Please notice that in the homework forum special rules are in place.

If this is not homework, please explain what you try to achieve. We might find a better solution for your original problem instead of improving what you think is a solution. This thread is closed and will only be opened after a declaration of this either being or not being homework.



@all other contributors, especially vgersh99: we are quite proud of our continued efforts to avoid doing the homework for students (which would be counterproductive not only for us because we''ll get unfit colleagues but also for the students who will be robbed the chance to learn and improve their skills).

But to do this we need to rely on you, especially the longterm-members and contributing experts answering threads here. I suppose you have, over time, deveoped the same "homework-radar" as most of us have. Please use your good judgement and DO NOT ANSWER threads that are likely to be homework until this question is answered. Yes, i am aware that it is hard to know the answer and not post it. But weighing several aspects against each other we came to the conclusion that the overall good is best served this way. vgersh99's first post was hidden from public view for exactly that reason.

If you want to contribute to this (or any other) decision we make: you are invited to write in the Underground and we value your input. We always try to replace good systems and decisions with better ones and if you want to contribute to this process we will be indebted to no end.

But, for the reasons above, this thread is closed.


@thread-op:

Your solution:
Code:
for i in `cat text.file`
do
sed "s/\$i/$var1" text.file > text.file.new

has two shortcomings:

first, the sed-statement is incomplete and should read:

Code:
"s/\$i/$var1/"

Notice the "/" at the end. Second, your for-loop is not closed:

Code:
for <var> in <list of values>
do
   cmd1
   cmd2
   ...
done

You might want to correct these and then look what your code produces and if it matches what you want. Chances are there are still differences.

Btw., using "for"-loops with open-ended lists (you do not know beforehand how long text.file will be) is never a good idea, though not syntactically wrong. Better replace such constructs with a "while"-loop, which will do the same:

Code:
while read <var> ; do
    cmd1 "$VAR"
    cmd2 "$VAR"
    ....
done < /path/to/input.file

I hope this helps.

bakunin

Last edited by bakunin; 09-22-2015 at 09:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string based on input

I am trying to read a value from a mapping file and would need to replace the value based on country parameter source_table_@ctry_final Expected final_var=source_table_aus_final If the country is in nz,usa,uk then final_var=diff_table_nz_final final_var=diff_table_usa_final like that... (10 Replies)
Discussion started by: Master_Mind
10 Replies

2. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

3. Shell Programming and Scripting

Searching a string in a particular file name

Hello, I have a file name like FIRST_DPF_DAILY_CUST_0826152322.txt i need to extract the string after the third "_" underscore upto timestamp ends i.e CUST_0826152322 can anyone help me with the code Thank you! Regards Srikanth Sagi (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

4. Shell Programming and Scripting

[SOLVED] Replace a string in nextline after searching a pattern

Hi, I have a requirement where I need to replace a string in a line and this line will be identified by search criteria on previous line: E.g.: I have an xml file and Contents as below: <Root> <NameValue> <name>Global/Text/Data</name> <value>This is valid... (14 Replies)
Discussion started by: mailing2vamsi
14 Replies

5. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

6. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

7. Shell Programming and Scripting

Searching a string in a file

Hi, I am new to unix shell scripting. I have a requirement. Could anyone help me writing the script for the same? Here goes the requirement: I have a config file let's say temp.config. Here is the data in the config file temp.config : ------------- name=victor age=42 state=texas... (5 Replies)
Discussion started by: badrimohanty
5 Replies

8. Shell Programming and Scripting

Reading an Input file and searching for occurrences WIHOUT SED or AWK

Hi people. I am new to shell scripting, so I need a little help. I want to create a script named that takes an argument as a file, Read the input file and look for occurrences of the current username (say abc.xyz) who is executing the script. On finding an occurrence of the username take that line... (2 Replies)
Discussion started by: kartikkumar84@g
2 Replies

9. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

10. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies
Login or Register to Ask a Question