Nitika Chowdhary
Nitika Chowdhary

Reputation: 3

Replace string got from pipe in a file in command line

How can I use sed or tr or replace to replace the string I get from pipe like:

head -n 1 myfile | sed -i 's/'-'/leg/g' new.log

Upvotes: 0

Views: 2471

Answers (2)

Kent
Kent

Reputation: 195039

try this:

MY_PAT=$(head -n 1 myfile)
sed -i "s/$MY_PAT/leg/g" new.log

or

sed -i "s/$(head -n 1 myfile)/leg/g" new.log

if your myfile contains special characters, better give concrete example.

Upvotes: 1

pjz
pjz

Reputation: 43057

use the read shell builtin to get the first line from stdin.

#!/bin/bash
read TO_REPLACE
head -n 1 myfile | sed -i "s/$TO_REPLACE/leg/g" new.log

Upvotes: 0

Related Questions