Copy Lines between Keywords & paste them to another file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copy Lines between Keywords & paste them to another file
# 1  
Old 04-03-2014
Scissors Copy Lines between Keywords & paste them to another file

hi,
I have Multiple files with the following data :

File1

Code:
100414 DR1
END
XXXXX
Test1 Test2 Test3
Test4 Test5 Test6
END

100514 DR2
END
XXXXX
Test7 Test8 Test9
Test10 Test11 Test12
END

100614 DR3
END
XXXXX
Test13 Test14 Test15
Test16 Test17 Test18
END


Required Output:

1. Search for the Date(Lets Say: 100614) and copy the line
2. append at the end of every line after the immediate XXXXX till "END" is encountered along with the file name
3. Same will be done for all files in the folder & the Output will be in another file in the same Folder

Output

Code:
Test13 Test14 Test15 100614 DR3 FILE1
Test16 Test17 Test18 100614 DR3 FILE1

here data from Other files in the same Folder will also reflect

Code:
Test13 Test14 Test15 100614 DR3 FILE1
Test16 Test17 Test18 100614 DR3 FILE1
Test13 Test14 Test15 100614 DR3 FILE2
Test16 Test17 Test18 100614 DR3 FILE2
Test13 Test14 Test15 100614 DR3 FILE3
Test16 Test17 Test18 100614 DR3 FILE3

Plz suggest some scripts or Logics to implement the same ...

Last edited by Scrutinizer; 04-03-2014 at 08:19 AM.. Reason: code tags
# 2  
Old 04-03-2014
Something like this:

Code:
awk '/100614/ {a=$0;c=1;next} /XXXXX/&&c {b=1;next} /END/ { b=0;next} b { printf "%s %s %s \n",$0,a,FILENAME  } ' file1

Extend the same logic to handle all files.
This User Gave Thanks to panyam For This Post:
# 3  
Old 04-03-2014
If the file structure is same, below will work
Code:
awk '$1 ~ pat {for(i = 4; i < NF; i++) {printf "%s %s %s\n", $i, $1, FILENAME}}' FS='\n' RS= pat='100614' file*

# 4  
Old 04-12-2014
Signal 5 TImes Print

Quote:
Originally Posted by panyam
Something like this:

Code:
awk '/100614/ {a=$0;c=1;next} /XXXXX/&&c {b=1;next} /END/ { b=0;next} b { printf "%s %s %s \n",$0,a,FILENAME  } ' file1

Extend the same logic to handle all files.
Sorry for delayed reply, but your one liner worked like a Charm. Output is just the way I wanted.
But for every file data is appearing 5 times & 1st data is correct, but the remaining 4 times are Wrong. i.e. Suppose I ran the above line,

Test1 Test11 Test16 100614 DR3 FILE1
Test2 Test12 Test17 100614 DR3 FILE1
Test3 Test13 Test18 100614 DR3 FILE1
Test4 Test14 Test19 100614 DR3 FILE1
Test5 Test15 Test20 100614 DR3 FILE1


Test10 Test13 Test20 100614 DR3 FILE1
Test20 Test14 Test21 100614 DR3 FILE1
Test30 Test15 Test22 100614 DR3 FILE1
Test40 Test16 Test23 100614 DR3 FILE1
Test50 Test17 Test24 100614 DR3 FILE1
in the above example, 1st Paragraph data is Ok, but 2nd paragraph data is from a different date but comes with 100614 stamp

and Same thing happening for all remaining files.

Hope I 'm able to explain the issue ...

---------- Post updated at 12:15 AM ---------- Previous update was at 12:14 AM ----------

Quote:
Originally Posted by SriniShoo
If the file structure is same, below will work
Code:
awk '$1 ~ pat {for(i = 4; i < NF; i++) {printf "%s %s %s\n", $i, $1, FILENAME}}' FS='\n' RS= pat='100614' file*

Dear.. not able to test the code till now, will test & reply by tomorrow...Smilie

---------- Post updated at 09:18 AM ---------- Previous update was at 12:15 AM ----------

Quote:
Originally Posted by SriniShoo
If the file structure is same, below will work
Code:
awk '$1 ~ pat {for(i = 4; i < NF; i++) {printf "%s %s %s\n", $i, $1, FILENAME}}' FS='\n' RS= pat='100614' file*


Hi Srini...

When I run the code, there is no Op.. this is the exact code I executed in Cygwin terminal ...
awk '$1 ~ pat {for(i = 4; i < NF; i++) {printf "%s %s %s\n", $i, $1, FILENAME}}' FS='\n' RS= pat='140320' *.* > Output.log
# 5  
Old 04-12-2014
I could see the output

Code:
srinus@ubuntu:~$ awk '$1 ~ pat {for(i = 4; i < NF; i++) {printf "%s %s %s\n", $i, $1, FILENAME}}' FS='\n' RS= pat='100614' file*
Test13 Test14 Test15 100614 DR3 file1
Test16 Test17 Test18 100614 DR3 file1
srinus@ubuntu:~$
srinus@ubuntu:~$ awk '$1 ~ /100614/ {for(i = 4; i < NF; i++) {print ($i " " $1 " " FILENAME)}}' FS='\n' RS='' file*
Test13 Test14 Test15 100614 DR3 file1
Test16 Test17 Test18 100614 DR3 file1
srinus@ubuntu:~$

Try the below
Code:
awk '$1 ~ /100614/ {for(i = 4; i < NF; i++) {print ($i " " $1 " " FILENAME)}}' FS='\n' RS='' file*

And, if the filenames are FILE1, FILE2, ..., you cannon use "*.*" in the command for files...Confirm your files have extension if you are using the "*.*" in place of file name
# 6  
Old 04-12-2014
Hi Panyam ....

with slight modification of Logic the multi printing issue resolved. final lines looks like following:

Code:
awk '/100614/ {a=$0;c=1;b=0;next} /xxxxxxxxx/&&c==2 {b=1;next}  /END/ {c=c+1;b=0;next} b&&c&&NF>=6 { printf "%s %s %s  \n",$0,a,FILENAME  } ' file*

Thanks for the Support ...

---------- Post updated at 04:55 PM ---------- Previous update was at 04:52 PM ----------

@Srini

still not getting any output for your line, mmm

$i in the for loop will print fields or lines !!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash copy and paste text in file from one position to another

Hi I have a text file with lines beginning with 71303, 71403, 71602, I need to copy the 10 digit text at position 30 on lines beginning with 71303 (5500011446) to position 99 on every line beginning with 71602 (see example below), There may be many 71303 lines but I need the text copying to... (2 Replies)
Discussion started by: firefox2k2
2 Replies

2. Shell Programming and Scripting

Copy and paste text inside a xml file

I have a really big XML file. I need copy the value of one tag inside another one tag. I try to publish one example. <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1">Rai 1</channel> <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1 +2HD">Rai 1... (6 Replies)
Discussion started by: Tapiocapioca
6 Replies

3. Shell Programming and Scripting

Extract lines from file using keywords using script

Hi I need some lines of text from input file using keywords. Inputfile IP IS 10.238.52.65 pun-ras-bng-mhs-01#context bsnl.in Card Status : 1:0, 2:1, 3:1, 4:1, 5:0, 6:0, 7:0, 8:0, 9:1, 10:0, 11:0, 12:0, 13:0, 14:1, Max Circuits: 1: 0, 2: 32768, ... (5 Replies)
Discussion started by: surender reddy
5 Replies

4. UNIX for Dummies Questions & Answers

Reading specific lines from a file using index or keywords

Hello I want to read from a file which contains email addresses. The file format is like this. from@mail.com to1@mail.com to2@mail.com cc@mail.com bcc@mail.com I'll have to read from such file and assign the email addresses to respective variables. frommail =... (11 Replies)
Discussion started by: Kyaw Lwin Phyo
11 Replies

5. Shell Programming and Scripting

Copy a column and paste to other file question

Please help me. This is simple, but urgent problem for me. :( I have a two files file1 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 ..... file2 11 12 13 14 15 11 12 13 14 15 11 12 13 14 15 ..... 1) I hope to make a new file, file 3, that consists of 2nd... (2 Replies)
Discussion started by: exsonic
2 Replies

6. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

7. Shell Programming and Scripting

copy the contents between two keywords to a new file.

Hi All, I want to edit my gate level netlists by searching for the content between two patterns eg: ff1 \test/a0 ( .CLK(\test/ClkInt0_acb_00x1 ),.D(\test/Rakicc ), .QB(\test/X ), .VDD(1'b1), .VSS(1'b0)); ff1 \test/a1 ( .CLK(\test/medis0_acb_00x1 ),.D(\test/hedwc ), .QB(\test/X ),... (6 Replies)
Discussion started by: naveen@
6 Replies

8. UNIX for Dummies Questions & Answers

copy and paste certain many lines of huge file in linux

Dear All, I am working with windoes OS but remote a linux machine. I wonder the way to copy an paste some part of a huge file in linux machine. the contain of file like as follow: ... dump annealling all custom 10 anneal_*.dat id type x y z q timestep 0.02 run 200000 Memory... (2 Replies)
Discussion started by: ariesto
2 Replies

9. Shell Programming and Scripting

awk/grep copy and paste and insert in between lines.

Hi all, I'm a unix newb andI'm trying to write a script that can copy some text paste it in a certian place and then add a number. It's not really clear but I'll show an example. what the file looks like right now: Linux 2.6.24-24-generic (abc) 07/15/09 23:25:01 CPU ... (6 Replies)
Discussion started by: the1hand3r
6 Replies
Login or Register to Ask a Question