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
text.file will contain data such as
once the string is matched it should be changed to :
The text.file should end up as follows if the script has worked:-
I hope i've explained the problem well enough
Last edited by Scrutinizer; 09-21-2015 at 12:12 PM..
Reason: code tags
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:-
Sorry, but this question reeks like homework so much i have to close it.
Moderator's Comments:
@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:
has two shortcomings:
first, the sed-statement is incomplete and should read:
Notice the "/" at the end. Second, your for-loop is not closed:
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)