0

I am using sed in linux. I have done most of the job but need help in this: a part of the pattern is like these:

document=SomeRanddomAlphaNumeric/10DigitNumber, user=

some examples:

I am expecting to find:

ducument=LKJHuasfb89/9878476578, user=
ducument=jhdn uryhf 9adu/6767898893, user=
ducument=iujjnbmr Ohndj88isju/6767898734, user=

and turn them to:

ducument=replacetext, user=
ducument=replacetext, user=
ducument=replacetext, user=

I am using this as sed command :

$ sed -i 's/regex/replacetext/g' $file.txt

replace text is constant but I need the regex for above command.

2
  • 1
    Are the some examples expected output? Commented Aug 21, 2013 at 6:26
  • no they are pattern I want to replace them. I will update the q Commented Aug 21, 2013 at 6:28

3 Answers 3

1

If the preceding text does not contain any slash characters (/), you can match the 10 digit sequence like this:

sed 's|\(ducument=[^/]*/\)[0-9]\{10\}|\1replacetext|' infile
Sign up to request clarification or add additional context in comments.

2 Comments

uh, thanks! does this command works for multiple occurances of same string in the input file?
@Youhan: if they are on the same line then yes, otherwise you need to add the g flag the the sed command.
1

This would work,

sed  's/\(ducument=\)\([^,]\+\)/\1replacetext/g'  file

Comments

1

This sed should work:

sed -i.bak 's/^\(document=\)[^,]*/\1replacetext/' file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.