How to remove the lines from file using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove the lines from file using perl
# 1  
Old 06-03-2008
How to remove the lines from file using perl

Can anyone tell me what could be the solution to following :

I have one .txt file which contains some seed information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed.

here is the contents of .txt file :

187 TAL_SPI_CORRECT_REQ_OUT seed : 894328066;
187 TAL_SPI_NO_OF_BYTES_OUT seed : 894328066;
30445 TAL_SPI_MASTER_NO_MORE_DATA_OUT seed : 894328066;
352 TAL_SPI_MASTER_APPLY_COMMAND_OUT seed : 894328066;
944 TAL_SPI_MASTER_DATA_OUT seed : 894328066;
935 TAL_SPI_MASTER_ADDR_OUT seed : 894328066;
1 a_dsync100_no_wr_dsync_ucode_exec_nike seed : 1072076501;
1 a_dsync100_no_wr_dsync_ucode_exec_ath1 seed : 1805353229;
1 a_dsync100_no_wr_dsync_ucode_exec_ath0 seed : 1805353229;
1 a_dsync100_no_wr_dsync_ucode_exec_capt seed : 1805353229;
2 a_dsync_lcm_xm2_output seed : 894328066;
1 a_dsync_pulse_gen_to_rcsdi_clkgen seed : 894328066;
1 a_dsyncEnWrite_to_rcsdi_clkgen seed : 894328066;
3 a_extsynth_buf_en_ch1 seed : 1072076501;
3 a_extsynth_buf_en_ch0 seed : 1072076501;
3 a_extTrig2_width_ch1 seed : 1617187839;
2 a_extTrig1_width_ch1 seed : 1617187839;


So once seed : 894328066 appears in the file, the script should remove the line if it appears twice and so on.

Please help me ASAP.

Regards and Thanks,
DipakgSmilie
# 2  
Old 06-03-2008
Use nawk or /usr/xpg4/bin/awk on Solaris.

Code:
awk '!_[$NF]++' input

Edit: Just read the subject.

Code:
perl -ane'print unless $_{$F[-1]}++' input


Last edited by radoulov; 06-03-2008 at 06:14 AM..
# 3  
Old 06-03-2008
How to remove the lines from file using perl

Hi radoulov,

Thanks for your reply it worked but after some time it goes wrong or may be it worked wrong way...

What I did is
perl -ane'print unless $_{$F[-1]}++' YES.txt

As I manually does the analysis to find the seeds I get the result 71 seeds but by this command it gives only 21.

How is this possible I don't know.
Can you please figure it out?

Regards,
Dipak
# 4  
Old 06-03-2008
Quote:
Originally Posted by dipakg
Hi radoulov,

Thanks for your reply it worked but after some time it goes wrong or may be it worked wrong way...

What I did is
perl -ane'print unless $_{$F[-1]}++' YES.txt

As I manually does the analysis to find the seeds I get the result 71 seeds but by this command it gives only 21.
[...]
Please post more sampe data and the desired output using code tags
HTML Code:
[code]your text[/code]
What's the expected output given your first sample?
Is the below output the one you expect?

Code:
$ cat file
187 TAL_SPI_CORRECT_REQ_OUT seed : 894328066;
187 TAL_SPI_NO_OF_BYTES_OUT seed : 894328066;
30445 TAL_SPI_MASTER_NO_MORE_DATA_OUT seed : 894328066;
352 TAL_SPI_MASTER_APPLY_COMMAND_OUT seed : 894328066;
944 TAL_SPI_MASTER_DATA_OUT seed : 894328066;
935 TAL_SPI_MASTER_ADDR_OUT seed : 894328066;
1 a_dsync100_no_wr_dsync_ucode_exec_nike seed : 1072076501;
1 a_dsync100_no_wr_dsync_ucode_exec_ath1 seed : 1805353229;
1 a_dsync100_no_wr_dsync_ucode_exec_ath0 seed : 1805353229;
1 a_dsync100_no_wr_dsync_ucode_exec_capt seed : 1805353229;
2 a_dsync_lcm_xm2_output seed : 894328066;
1 a_dsync_pulse_gen_to_rcsdi_clkgen seed : 894328066;
1 a_dsyncEnWrite_to_rcsdi_clkgen seed : 894328066;
3 a_extsynth_buf_en_ch1 seed : 1072076501;
3 a_extsynth_buf_en_ch0 seed : 1072076501;
3 a_extTrig2_width_ch1 seed : 1617187839;
2 a_extTrig1_width_ch1 seed : 1617187839;
$ perl -ane'print unless $_{$F[-1]}++' file
187 TAL_SPI_CORRECT_REQ_OUT seed : 894328066;
1 a_dsync100_no_wr_dsync_ucode_exec_nike seed : 1072076501;
1 a_dsync100_no_wr_dsync_ucode_exec_ath1 seed : 1805353229;
3 a_extTrig2_width_ch1 seed : 1617187839;


Last edited by radoulov; 06-03-2008 at 06:49 AM..
# 5  
Old 06-03-2008
How to remove the lines from file using perl

Here in I am attaching the text file.

For better view you can open it in wordpad.
Now this text file should give me total 71 seeds as output(I mean total 71 lines should be appear as output).

Last edited by dipakg; 06-03-2008 at 08:10 AM..
# 6  
Old 06-03-2008
Code:
perl -ne'/: (\d+?);/;print unless $_{$1}++' YES.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines that are subsets of other lines in File

Hello everyone, Although it seems easy, I've been stuck with this problem for a moment now and I can't figure out a way to get it done. My problem is the following: I have a file where each line is a sequence of IP addresses, example : 10.0.0.1 10.0.0.2 10.0.0.5 10.0.0.1 10.0.0.2... (5 Replies)
Discussion started by: MisterJellyBean
5 Replies

2. UNIX for Dummies Questions & Answers

Want to remove all lines but not latest 50 lines from a file

Hi, I have a huge file which has Lacs of lines. File system got full. I want your guys help to suggest me a solution so that I can remove all lines from that file but not last 50,000 lines. I want solution which can remove lines from existing file so that I can have some space left with. (28 Replies)
Discussion started by: prashant2507198
28 Replies

3. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

4. Shell Programming and Scripting

Need to remove first 6 lines and last line in a array ---- perl scripting

Hi I have stored a command output in an array like below @a = `xyz`; actually xyz comnad will give the output like this tracker date xxxxxxx xxxxxxx --------------------- 1 a 2 b ---------------------- i have stored the "xyz" output to an... (3 Replies)
Discussion started by: siva kumar
3 Replies

5. Shell Programming and Scripting

perl/shell need help to remove duplicate lines from files

Dear All, I have multiple files having number of records, consist of more than 10 columns some column values are duplicate and i want to remove these duplicate values from these files. Duplicate values may come in different files.... all files laying in single directory.. Need help to... (3 Replies)
Discussion started by: arvindng
3 Replies

6. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

7. Shell Programming and Scripting

perl or awk remove empty lines when condition

Hi Everyone, # cat 1 a b b cc 1 2 3 3 3 4 55 5 a b (2 Replies)
Discussion started by: jimmy_y
2 Replies

8. UNIX for Advanced & Expert Users

Remove lines using perl

Hi, Using the egrep ,i can exclude header and trailer records in file.I want to do in perl.Please help egrep -iv 'head|trail' filename Thanks in advance MR (6 Replies)
Discussion started by: mohan705
6 Replies

9. Shell Programming and Scripting

How to remove the specific lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some "seed" information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. Please provide the script code... (4 Replies)
Discussion started by: dipakg
4 Replies

10. Shell Programming and Scripting

remove specific lines from flat file using perl

Hi, Here is wat im looking for.. i have a flat file which looks like this.. 00 * * * * .. .. * * text text text COL1 COL2 ----- ----- 1 a (12 Replies)
Discussion started by: meghana
12 Replies
Login or Register to Ask a Question