How to search pattern and add that pattern in next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search pattern and add that pattern in next line
# 1  
Old 03-22-2012
Java How to search pattern and add that pattern in next line

Hi All,
I am new to shell scripting and need help in scripting using CSH.
Here is what I am trying to so,
1. Search a specific string e.g. "task" from "task (input1, out1)".
2. Extract the arguements "input1" and "out1"
3. 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.
# 2  
Old 03-22-2012
Hi

Not sure, if I got your question correctly. If so:

Input:
Code:
$ cat file
some dummy data
task (input1, out1)
some more dummy data

Output:

Code:
$ sed -r 's/task *\(([^,]+), *(.*)\)/&\nint \1;\ninteger \2;/' file
some dummy data
task (input1, out1)
int input1;
integer out1;
some more dummy data


Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 03-22-2012
Thanks guruprasad, you were spot on.
One more favour please, can you please tell me how that works?
# 4  
Old 03-22-2012
Code:
 
$ 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[i])}} !/\)/ {print}' test.txt
some dummy data
task (input1, out1)
int input1;
int  out1;
some more dummy data

# 5  
Old 03-22-2012
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.
# 6  
Old 03-22-2012
Thanks you itkamaraj.

---------- Post updated at 01:03 PM ---------- Previous update was at 01:02 PM ----------

Code:
$ 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?
# 7  
Old 03-22-2012
Code:
 
sed -r 's/task *\(([^,]+), *(.*)\)/&\nint \1;\ninteger \2;/' file > file.out
mv file.out file

or you can use -i

Code:
sed -i -r 's/task *\(([^,]+), *(.*)\)/&\nint \1;\ninteger \2;/' file

Code:
 
nawk -F"[)(]" '/\(/{print;n=split($2,a,",");for(i=1;i<=n;i++){printf("int %s;\n",a[i])}} !/\)/ {print}' test.txt > test.out
mv test.out test.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

3. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

4. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

Search a pattern and add new line below

Hi, I have 2 files like below. File A: apple mango File B: start abc def apple ghi end start cba fed (4 Replies)
Discussion started by: jayadanabalan
4 Replies

6. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

7. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

8. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies
Login or Register to Ask a Question