hamad khan
hamad khan

Reputation: 369

how to edit a line using sed or awk in linux containing a certain number or string

My Stress.k file is as follows

180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION 
*END

I want it to be like

180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION 
*/home/hassan/534.k 
*END

for that I used sed as follows

a="$(cat flow.k)"
sed  -i -e '/*END/i \*/home/hassan/$a.k ' Stress.k

where flow.k has only a single number like 534.k or something . Here sed put the line before END but it doesn't take the value of a , instead it puts the same alphabet and it doesn't understand $a.k.

Please also tell me how to delete the second last line or the line with a string hassan for example so that I can delete it first and the for the next step I use it to enter my required line.

if possible please also suggest the alternatives.

best regards

Upvotes: 0

Views: 647

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 360445

Use double quotes to allow the variable to be expanded.

sed  -i -e "/*END/i \*/home/hassan/$a.k " Stress.k

To replace the string, do it as you read in the file:

a=$(sed 's/534/100/' flow.k)

To delete a line:

sed '/hassan/d' inputfile

To read a file into the stream after the current line:

sed '/foo/r filename' inputfile

Upvotes: 0

K3---rnc
K3---rnc

Reputation: 7049

bash variables are only replaced when in double quotes, e.g.

sed  -i -e "/*END/i \*/home/hassan/$a.k " Stress.k

Upvotes: 1

Related Questions