Line deletion help using perl -ne


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Line deletion help using perl -ne
# 1  
Old 01-02-2008
Line deletion help using perl -ne

Hi all,

I know the one liner to delete all the lines in a file which matches a pattern
i.e

perl -i.old -ne 'print unless /pattern/' file

Now i need the perl onliner to delete all the lines which doesnt match the pattern. Also what is the difference between perl -i and perl -i.old.
Does perl -i.old archive the existing file?

Your help in this will be appreciated.

Thanks in advance.

Lijju
# 2  
Old 01-02-2008
Only print lines matching the pattern:
Code:
perl -i.old -ne 'print if /pattern/' file

-i tells perl to modify a file in-place, -i.old or -i.bak or whatever you like, tells perl to make a backup first using the specified extension. See 'man perlrun' for the specifics.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Make Multile line is one line using window Perl

Hi All I have a .csv file which is showing data as ESP Client,ESP Engagement,Misc_Projects_120101,DEFAULT,HA,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected-Done,"she,joy.",200111,Unknown,Full Time,,Delivery_DONE AMO,Approved,2012-12-03,2012-12-06,2012-12-06,"Occupied Hours ... (7 Replies)
Discussion started by: adisky123
7 Replies

3. UNIX for Dummies Questions & Answers

sed - combination of line deletion and pattern matching

I want to delete all the blank lines from a file before a certain line number. e.g. Input file (n: denotes line number) 1: a 2: 3: b 4: c 5: 6: d I want to delete all blank lines before line number 3, such that my output is: a b c d I see that sed '/^$/d' in_file works... (9 Replies)
Discussion started by: jawsnnn
9 Replies

4. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

5. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

6. Shell Programming and Scripting

Deletion of characters in every second line

Hi, I have a file with four types of lines: @HWUSI-EAS656_0022:8:1:175:376#0/1 CTCGACACGCTGGTGGAGATCCTCGGCCTGACCGAGGACGACCGGGCCATCTTCGAGCAGCGC + BBBBBBBA?AABB;<=B9<===AA1AA==>99===.9?:9A4A956%%%%%%%%%%%%%%%%% I want to remove the first 6 characters of every second line. This means in... (10 Replies)
Discussion started by: s052866
10 Replies

7. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

8. Shell Programming and Scripting

How to use Perl to merge multi-line into single line

Hi, Can anyone know how to use perl to merge the following multi-line information which beginning with "BAM" into one line. For each line need to delete the return and add a space. Please see the red color line. ******Org. Multi-line) BAM admin 101.203.57.22 ... (3 Replies)
Discussion started by: happyday
3 Replies

9. Shell Programming and Scripting

blank line deletion in a file using perl

Dear All, I am looking for an option in perl using which i could delete empty lines in a file. Or the alternative of sed '/^$/d' <filename> in perl. Sed is not working in my perl script :( Pls help me out . Thanks, VG (4 Replies)
Discussion started by: vguleria
4 Replies

10. Shell Programming and Scripting

Multiple Files deletion in perl

Hello Friends, I want to delete all the "*.trg" files in a directory but i don't want to do it by system("rm -r *.trg"); Can i do it thru unlink or by any other mean Thanks, Pulkit (1 Reply)
Discussion started by: pulkit
1 Replies
Login or Register to Ask a Question