user2901827
user2901827

Reputation: 35

Sed to replace a word from another file content

Hi I am looking some help for sed from gurus , basically i git two files

First :- serial.txt
second :- info.txt

file serial.txt has a unique information. and file info.txt has

"http://irequestedserver1?u=user:p=123"
"http://irequestedserver2?u=user:p=123"
and more and more

I want to replace user word with the info stored in first file serial.txt.

Upvotes: 0

Views: 98

Answers (3)

mklement0
mklement0

Reputation: 437998

Assuming that serial.txt contains a single line of information that is to be used for all lines in info.txt:

 sed -r 's/\?u=[^:]+:/\?u='"$(tr -d '\n' < serial.txt)"':/' info.txt

Upvotes: 1

BMW
BMW

Reputation: 45243

Using sed

var=$(<serial.txt)
sed -r "s/(u=user:)([^\"]*)/\1${var}/" info.txt

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77105

If your serial.txt file contains a name, then you can try something like this:

$ cat serial.txt 
jaypal

$ cat info.txt 
"http://irequestedserver1?u=user:p=123"
"http://irequestedserver2?u=user:p=123"

$ awk 'NR==FNR{a[$1];next}{for (name in a) {sub(/u=.*:/,"u="name":")}}1' serial.txt info.txt 
"http://irequestedserver1?u=jaypal:p=123"
"http://irequestedserver2?u=jaypal:p=123"

Upvotes: 1

Related Questions