I have a file like this (this is a sample, the file contains more lines like these):first line sss case-2-hello-world other wordssecond line other wordsthird line sss case-1-love-you other wordsfourth line other wordsfifth line other wordssixth line sss case-6-not-work other wordsAnd I would like to convert it to:pp:12 pme:4 plan:cpu_bind=hello mem_bind=world second line other words pp:6 pme:2 plan:cpu_bind=love mem_bind=youfourth line other words fifth line other words pp:36 pme:12 plan:cpu_bind=not mem_bind=work First, identify the lines with the pattern sss. Second, extract the number. Third, calculate the pp and pme: pp=number*6 and pme=number*2. Fourth, split the words that contain numbers in that line and assign them to cpu_bind and mem_bind. Fifth, put them together to replace the lines.For example, I identify the line first line sss case-2-hello-world other wordsby sss, the number is 2. After that, I need to calculate pp=2*6 pme=2*2. Split the string case-2-hello-world into parts and assign hello to cpu_bind and world to mem_bind. At the end, I need to get pp:12 pme:4 plan:cpu_bind=hello mem_bind=worldreplacing the original line.Note: sss can be anywhere in that line but occur only one time. sss is only pattern can be used to identify which line need to be substituted. There are other words contain number and other numbers int the line. The pattern case-number-cpu_bind-mem_bind have four parts. Its order is fixed and can be spit by -.
how to substitute some lines of one file according to the line's content
text processing
This is complex enough that I would use a full fledged programming language to do it. For example, in Perl:$ perl -ne 'if(/\ssss\s+/ && /(\S+-\d+-\S+)/){               @F=split(/-/,$1);                print pp:,                6 * $F[1],                 pme:,2*$F[1],                 plan:cpu_bind=$F[2] mem_bind=$F[3]\n             }else{print}' file Or, golfed a little but following the same idea : $ perl -lpe '/\ssss\s+/&&do{/(\S+-\d+-\S+)/;@F=split(/-/,$1);  $_=pp:.6*$F[1]. pme:.2*$F[1]. plan:cpu_bind=$F[2] mem_bind=$F[3]}' file Note that this makes a few assumptions that might not be true (but I can't know from your question):It assumes that the word immediately after sss is the one you care about. It assumes that this word will always be divided by - into sub words.It assumes that the word will always have 4 parts, with case being the first, the number second, then two words that should be assigned to cpu_bind and mem_bind. Assuming these assumptions are correct, here is the same thing as a commented script:#!/usr/bin/env perl## Read the input file line by linewhile (<>) {    ## If this line matches whitespace (\s), then sss, then one    ## or more whitespace character, identify the string of interest    ## by looking for non-whitespace characters (\S+), -, then     ## numbers (\d+), then - and more non-whitespace characters and     ## save them as $1.    if(/\ssss\s+/ && /(\S+-\d+-\S+)/){        ## Split the word captured above into the @F array        ## by cutting it on -        @F=split(/-/,$1);         ## Start printing.        print pp:,            ## 6 * the 2nd element in the array (the number)            6 * $F[1],                 pme:,2*$F[1],                    ## The third element ($F[2]) is the 1st word                    ## and the fourth element ($F[3]) is the 2nd word.                     plan:cpu_bind=$F[2] mem_bind=$F[3]\n                }    ## If this line does not match sss, print it.    else{print}}