Hi All,
I am new to shell scripting and need help in scripting using CSH.
Here is what I am trying to so,
- Search a specific string e.g. "task" from "task (input1, out1)".
- Extract the arguements "input1" and "out1"
- Add them in separate lines below. eg. "int input1" , " integer out1"
So final output should look like,
task (input1, out1)
int input1;
integer out1;
Thanks in advance.
Hi
Not sure, if I got your question correctly. If so:
Input:
$ cat file
some dummy data
task (input1, out1)
some more dummy data
Output:
$ sed -r 's/task *\(([^,]+), *(.*)\)/&\nint \1;\ninteger \2;/' file
some dummy data
task (input1, out1)
int input1;
integer out1;
some more dummy data
Guru.
1 Like
Thanks guruprasad, you were spot on.
One more favour please, can you please tell me how that works?
$ cat test.txt
some dummy data
task (input1, out1)
some more dummy data
$ nawk -F"[)(]" '/\(/{print;n=split($2,a,",");for(i=1;i<=n;i++){printf("int %s;\n",a)}} !/\)/ {print}' test.txt
some dummy data
task (input1, out1)
int input1;
int out1;
some more dummy data
We try to split the line into patterns by extracting the values "input1" and "out1". and then substituting(s) it with the original line(&), and then the first pattern preceeded by "int"(int \1 and newline) followed by second pattern(integer \2).
Guru.
Thanks you itkamaraj.
---------- Post updated at 01:03 PM ---------- Previous update was at 01:02 PM ----------
$ sed -r 's/task *\(([^,]+), *(.*)\)/&\nint \1;\ninteger \2;/' file
some dummy data
task (input1, out1)
int input1;
integer out1;
some more dummy data
What do I need to do to append the file instead of output on stdio?
sed -r 's/task *\(([^,]+), *(.*)\)/&\nint \1;\ninteger \2;/' file > file.out
mv file.out file
or you can use -i
sed -i -r 's/task *\(([^,]+), *(.*)\)/&\nint \1;\ninteger \2;/' file
nawk -F"[)(]" '/\(/{print;n=split($2,a,",");for(i=1;i<=n;i++){printf("int %s;\n",a)}} !/\)/ {print}' test.txt > test.out
mv test.out test.txt