简体   繁体   中英

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 $ak .

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

bash变量仅在双引号中被替换,例如

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

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

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