Jump to content

Linux/Shell/Bash/sed experts ravali


dingdong143

Recommended Posts

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 !!!!

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...