Hi, i have a problem with a sed command runned in a for cycle...
I have a file named fileX which contains (i have crippled the line to minimal for easy):
load data dbfoo - TOCHANGE - comment
load data dbboo - TOCHANGE - comment
load data dbzoo - TOCHANGE - comment
...
And a file named fileY which contains:
value1
value2
value3
...
I don't understand why sed change every occurrence of TOCHANGE in fileX, i have read that sed change the first occurrence and then proceed if switch "g" is omitted...
The portion of the code where i surely have made an error is:
for TC in `cat fileY`
do
sed s/"TOCHANGE"/"$TC" fileX > results
done
I've thinked that the file results will be like:
load data dbfoo - value1 - comment
load data dbboo - value2 - comment
load data dbzoo - value3 - comment
...
Because the echo on variable TC show me the values in order...
But instead i have this:
load data dbfoo - valueX - comment
load data dbboo - valueX - comment
load data dbzoo - valueX - comment
...
Let me guess, your script leaves the last value in the fileY.
You say sed change the first occurrence and then proceed if switch "g" is omitted, this is correct, but for each line. fileX includes TOCHANGE in every line, so for every line sed will change the first occurrence.
I think you can apply sed substitution only for a 1 line range, but I don't know how.
I think an issue is that each iteration will change for filex to results. But, you never in-between copy results back to filex before doing the next step.
So, after your sed, you would need something like
I'm not exactly certain what it is you're trying to do, so hopefully some general advice might be of use:
To change the first instance of "a" to "b" on _every_ line:
sed 's/a/b/' file
]
To change the first instance of "a" to "b" in a file:
sed '/a/{s/a/b/;q;}' file
In sed's substitution command, an empty regular expression is equal to the most recent regular expression used, so "s/a/b/" could have been written as "s//b/".
Thx to all, i've tried both sed solution but it doesn't work...
Like kcoder24 said the intent was to change the first occurrence for a single line and pass to next line on every cicle of for statement...
I try to explain myself better, in fileX i have the lines to change and in fileY i have the values to insert sequentially on every line of fileX.
Both fileX and fileY has the same number of lines because the origin of both files is unique.
Then i wish the results may be this:
line1 of file fileX with string-to-replace substituted by string line1 of fileY
line2 of file fileX with string-to-replace substituted by string line2 of fileY
and so on...
I try to make a good example with some values...
fileY contains:
goose
mickey
donald
...
fileX contains:
My name is frank ZORRO and i live in Bilbao
My name is paul ZORRO and i live in Bilbao
My name is john ZORRO and i live in Bilbao
...
The result i wish at last would be:
result contains:
My name is frank goose and i live in Bilbao
My name is paul mickey and i live in Bilbao
My name is john donald and i live in Bilbao
...
Thx a lot Scrutinizer i have tested your code and is working exacly as i need...
Let me see an awk tutorial and then i try to explain your code, if u find that i have misunderstood someone scold me...
---------- Post updated at 07:40 AM ---------- Previous update was at 06:55 AM ----------
May someone tell me if i have understood the code meaning?
It's important to learn, more than find the solution right?
Thx
awk '{getline x<f;sub(/ZORRO/,x)}1' f=file1 file2
............................................^___^ Target of awk command
....................................^_____^ Variable defined for awk that's pointing at file1
.................................^ I give up... Hope this "1" means to make sub on the first occurence...
.....................^_________^ replace ZORRO with the value in x variable
......^_________^ Read input line from f where f is the file1 and then put in x variable
....^_____________________________^ Entire program - switches - options parts of the command
.....^__________________________^ Program part that awk executes (code between the graphs)
As you've probably figured out by now, AWK code consists of pattern and action pairs; either can be absent but not both. an absent pattern defaults to true. an absent action defaults to printing the line (aka $0). a solitary 1 is a pattern which evaluates to true, triggering the absent action which prints the line. in short, 1 triggers printing of $0 and is equivalent to