I am trying to perform a two step string editing:
- Match for the string "payload". If found, then add some "#if #endif" code as shown in output file.
- Then copy the line containing "payload", replace it with "upper_payload" and then paste it within the "if endif" conditions.
Input File:
Let's see if this works or not payload.
alskdjaslkdjn.
aslkdjbnalkgfjbaglbjbjdgfsbkgjbsg;
Also here is another_payload.
alskdjbnalgkjb.
kjhsdfjlkgfjlknbsgdfljkbgsbljk((*&)(&;
Expected Output File:
Let's see if this works or not payload.
#if defined(NV_C2C_UPPER_PD)
Let's see if this works or not upper_payload.
#endif
alskdjaslkdjn.
aslkdjbnalkgfjbaglbjbjdgfsbkgjbsg;
Also here is another_payload.
#if defined(NV_C2C_UPPER_PD)
Also here is another_upper_payload.
#endif
alskdjbnalgkjb.
kjhsdfjlkgfjlknbsgdfljkbgsbljk((*&)(&;
I finally came up with the following code in a shell file so that I can run this on multiple text files:
#!/bin/csh -f
sed -i '/payload/a #if defined(NV_C2C_UPPER_PD)\n#endif' $1
sed -i "/NV_C2C_UPPER_PD/a `sed -n '/payload/s/payload/upper_payload/p' $1`" $1
With the above shell script when I pass a command such as:
> ./my_shell.sh test.txt
the text file is modified to the following output:
Let's see if this works or not payload.
#if defined(NV_C2C_UPPER_PD)
Let's see if this works or not upper_payload.
#endif
alskdjaslkdjn.
aslkdjbnalkgfjbaglbjbjdgfsbkgjbsg;
Also here is another_payload.
#if defined(NV_C2C_UPPER_PD)
Let's see if this works or not upper_payload.
#endif
alskdjbnalgkjb.
kjhsdfjlkgfjlknbsgdfljkbgsbljk((*&)(&;
And it also gives this error/warning on the terminal:
sed: can't read Also here is another_upper_payload.: No such file or directory
In the output we can observe that the second "upper_payload" substitution was incorrect because the line copying is incorrect. It copied the first line instead of the newer line containing "another_payload".
I have tried different options but I am failing in solving this problem. Would appreciate some help.
sedonly or isawkok?