Hi All,
I want to replace a string in a file and redirect the output to a new file,but this is slightly tricky.The input file contains data as A|B|C|D|E|F|G|H|I|K , now i want to replace alphabet �F� to �:� and redirect the output to a file.The final output must look like this:A|B|C|D|E|:|G|H|I|K. Please help it's urgent
sed 's/F/:/g' input_file > new_file
--ahamed
---------- Post updated at 09:12 AM ---------- Previous update was at 09:12 AM ----------
well, whats so tricky about this or did I get it wrong?
--ahamed
Or use tr ...
like...
tr "F" ":" < input_file > new_file
--ahamed
Thanks!Its my fault as i was not able to explain it properly.Here we go,the file contains data as ABC|DEF|GHI|AAABBB|GHI||G|K|P .Now i want only alphabet "G" to be replaced by ":"(as highlighted). The final output must look like this:
ABC|DEF|GHI|AAABBB|GHI|:|K|P
Also i dont know exactly at which position alphabet "G" will come which i want to replace.Please help
Try this
sed -e 's/|G|/:|/g' -e 's/||/|/g' input_file > new_file
--ahamed
echo 'G|ABC|G|DEF|GHI|AAABBB|GHI||G|K|P|G' | sed 's#^G|#:|#;s#|G|#|:|#g;s#|G$#|:#'
@ahamed:Can u please explain me the concept behind your methodology as i am new to shell script
---------- Post updated at 12:21 PM ---------- Previous update was at 12:10 PM ----------
Hi,
Please explain me the concept behind this as i am new to shell script
sed -e 's/|G|/:|/g' -e 's/||/|/g' input_file > new_file
s/search/replace/globally
's/|G|/:|/g'
search for |G| and replace with wherever you find it
's/||/|/g'
search for || and replace with | wherever you find it
> new_file
write into new file
Actually this command alone will do the job
sed 's/|G|/:|/g' input_file > new_file
Hope this helps.
--ahamed
But if "G" is the first or last field?
I think vgersh99 had all the bases covered. It just looks a bit more intimidated with "#" as separators.
sed 's/^G|/:|/;s/|G|/|:|/g;s/|G$/|:/'
Thanks for all your help!!But i came to know that data in the file can come as 12345|1|1111|131|678|134121|1|A|BCDEF
Now i want to replace "1" which is highlighted above with "2" then how it can be done??The final output must look like:
12345|1|1111|131|678|134121|2|A|BCDEF
One more thing though "1" can come at any index, this is sure that at 7th index "1" will definetly come and i want to replace this "1" which is present at 7th index with "2"..I tried the following:
sed 's/`cut -d"|" -f7`/"2"/g' but the problem is cut command will extract "1" from 7th index and then sed command is replacing all 1's in a file with 2's
Please help
You want to change the second occurence of |1| to |2| right?
You can't keep on changing the requirements. You will first need to find a pattern.
val = which value you want to replace
newval = value to be replaced with
need = which occurence
awk -F"|" -v val="1" -v newval=2 -v need=2 '{ for(i=1;i<=NF;i++){if($i==val){ocur++; if(ocur==need){$i=newval}} v=v"|"$i }}
END{sub(/^\|/,"",v);print v}' input_file
--ahamed
@ahamed:Appreciate your help and sorry about changing the requirements every now and then. Your solution seems to be perfect just that it is not fixed that always i want to replace 2nd occurrence of "1" to "2". The pattern in the file can be like this also:
1|1|1|2|11123|2|1|4|5|6
3333|456|1|345|1111|1|1|6|45|
1234|4|11|1|1|345|1|4|5
So it is not fixed that second occurrence of "1" is to be replaced,thing which is fixed is "1" present at 7th index is to be replaced.
so it looks like you want replace the last occurrence?
--ahamed
No No,the pattern can be 1|1|1|2|11123|2|1|4|5|6|1|3|1|1 this also...I always want to replace "1" present at 7th index.
Ok got it!
awk -F"|" -v search="1" -v replace="2" '{ for(i=1;i<=NF;i++){if(i==7 && $i==search){$i=replace} if(v){v=v"|"$i}else{v=i}}
print v;v=""} ' input_file
so we good?
--ahamed
Thanks a lot!!Just one last question can you explain me what does this "awk" command is and will it work in AIX flavor of unix. My system does not support bash shell script