简体   繁体   中英

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.

假设serial.txt包含一行信息,用于info.txt 所有行:

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

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"

Using sed

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM