dingdong143 Posted September 28, 2022 Report Posted September 28, 2022 need urgent help I have a file with data like this(50K lines unnay file lo) cat file test:testA:testB:testC:testD:testE:testF test1:test1A:test1B:test1C:testD:test1E:test1F test2:test2A:test2B:test2C:testD:test2E:test2F test3:test3A:test3B:test3C:testD:test3E:test3F test4:test4A:test4B:test4C:testD:test4E:test4F My task is to change few lines from this file based on a match cat list test1 test2 I want to change testD to testXX only on the lines that matches with list i tried this for i in `cat list` do grep $i file | sed -i 's/testD/testXX/g' file done but it is replacing everyline. My task is to update these only but everyhting else stay as is. test1:test1A:test1B:test1C:testD:test1E:test1F test2:test2A:test2B:test2C:testD:test2E:test2F HELP please !!!! Quote
maverick19 Posted September 28, 2022 Report Posted September 28, 2022 if i understand this right. you want to replace testD with Testxxx for lines starting with test1 and test2? you could grep ^ to match only lines with start with test1 an test2 from list file. Quote
dingdong143 Posted September 28, 2022 Author Report Posted September 28, 2022 10 minutes ago, maverick19 said: if i understand this right. you want to replace testD with Testxxx for lines starting with test1 and test2? you could grep ^ to match only lines with start with test1 an test2 from list file. How do i use grep ^ bhayya .. I tried this and this is exactly what i want. But this is only printing result. How do i make changes directly in fil for i in `cat list`; do grep $i file | sed 's/testD/testXX/g' ; done test1:test1A:test1B:test1C:testXX:test1E:test1F test2:test2A:test2B:test2C:testXX:test2E:test2F Quote
dasari4kntr Posted September 28, 2022 Report Posted September 28, 2022 Quote tr '{match}' '{replace}' < input.txt > output.txt Quote
dingdong143 Posted September 28, 2022 Author Report Posted September 28, 2022 7 minutes ago, dasari4kntr said: I dont think tr will work in my case. I want to change only the lines that match with the file "list" not every single line cat list test1 test2 Quote
dasari4kntr Posted September 28, 2022 Report Posted September 28, 2022 3 minutes ago, dingdong143 said: I dont think tr will work in my case. I want to change only the lines that match with the file "list" not every single line cat list test1 test2 AWK. ..? Quote
chingchang Posted September 28, 2022 Report Posted September 28, 2022 58 minutes ago, dingdong143 said: need urgent help I have a file with data like this(50K lines unnay file lo) cat file test:testA:testB:testC:testD:testE:testF test1:test1A:test1B:test1C:testD:test1E:test1F test2:test2A:test2B:test2C:testD:test2E:test2F test3:test3A:test3B:test3C:testD:test3E:test3F test4:test4A:test4B:test4C:testD:test4E:test4F My task is to change few lines from this file based on a match cat list test1 test2 I want to change testD to testXX only on the lines that matches with list i tried this for i in `cat list` do grep $i file | sed -i 's/testD/testXX/g' file done but it is replacing everyline. My task is to update these only but everyhting else stay as is. test1:test1A:test1B:test1C:testD:test1E:test1F test2:test2A:test2B:test2C:testD:test2E:test2F HELP please !!!! You dont need to grep for it. Sed has an option to search for matching variables. try something like this for i in `cat list`; do sed -i.bak '/$i/s/testD/testXX/g' file ; done Quote
chingchang Posted September 28, 2022 Report Posted September 28, 2022 Just now, chingchang said: You dont need to grep for it. Sed has an option to search for matching variables. try something like this for i in `cat list`; do sed -i.bak '/$i/s/testD/testXX/g' file ; done 1 hour ago, dingdong143 said: need urgent help I have a file with data like this(50K lines unnay file lo) cat file test:testA:testB:testC:testD:testE:testF test1:test1A:test1B:test1C:testD:test1E:test1F test2:test2A:test2B:test2C:testD:test2E:test2F test3:test3A:test3B:test3C:testD:test3E:test3F test4:test4A:test4B:test4C:testD:test4E:test4F My task is to change few lines from this file based on a match cat list test1 test2 I want to change testD to testXX only on the lines that matches with list i tried this for i in `cat list` do grep $i file | sed -i 's/testD/testXX/g' file done but it is replacing everyline. My task is to update these only but everyhting else stay as is. test1:test1A:test1B:test1C:testD:test1E:test1F test2:test2A:test2B:test2C:testD:test2E:test2F HELP please !!!! Actually, use double quotes as it is a variable. Try this for i in `cat list`; do sed -i.bak "/$i/s/testD/testXX/g" file ; done Quote
maverick19 Posted September 28, 2022 Report Posted September 28, 2022 4 hours ago, dingdong143 said: How do i use grep ^ bhayya .. I tried this and this is exactly what i want. But this is only printing result. How do i make changes directly in fil for i in `cat list`; do grep $i file | sed 's/testD/testXX/g' ; done test1:test1A:test1B:test1C:testXX:test1E:test1F test2:test2A:test2B:test2C:testXX:test2E:test2F try sed directly on the file. string before /s is used to search the lines with that value and replace testXX with testD in those lines for i in `cat list`; do sed -i.txt '/^$1/s/testXX/testD/' text.txt done Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.