[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I need a script to remove lines from a file.

Posted on 2006-04-18
4
Medium Priority
?
171 Views
Last Modified: 2010-03-05
Hi,
    I work for an automotive group, and I need some help with a report file that we send out. The following is a sample.

S ORD/STK#  YEAR MAKE       MODEL      SERIES   BODY TYPE  D/S VIN
- --------- ---- ---------- ---------- -------- ---------- --- -----------------

W *0360089A 2004 ACURA      TL                  4DR SDN     13 19UUA65544A046034
R  003P1000 2003 ACURA      3.2TL               4DR SDN     42 19UUA56623A080487

It goes for about a page, and the the first two lines repeat themselves. I need the lines with the dashes removed, without disrupting the file otherwise.
I would appreciate the help, as I am just to busy to pull out my perl book, and hack this out. Thanks.
0
Comment
Question by:nalanbar
  • 2
4 Comments
 
LVL 5

Expert Comment

by:wlfs
ID: 16480314
unix:
perl -ne 'print unless m/^[\s-]$/' <infile.txt >outfile.txt

windows:
perl -ne "print unless m/^[\s-]$/" <infile.txt >outfile.txt
0
 
LVL 3

Author Comment

by:nalanbar
ID: 16484366
I ran the script, but the output still has the dashes...
0
 
LVL 9

Expert Comment

by:Suhas .
ID: 16484591
Hi nalanbar,
#!/usr/bin/perl

open(IN,"D://test.txt");
open(OUT,">D://test_res.txt");
@arr = <IN>;
foreach $i (@arr) {
@arr1 = split(//,$i);
#print @arr1;
foreach $j (@arr1) {
    print "$j\n";
    if($j ne "-"){
        print OUT $j;}
    }
   
}

input (file.txt):
khihkhdashd
--------
hdifhdhf00---

output(file_res.txt):
khihkhdashd

hdifhdhf00

Cheers!
Suhas
0
 
LVL 5

Accepted Solution

by:
wlfs earned 1000 total points
ID: 16484997
Sorry, you are of course right. I forgot the quantifier. In order to keep blank lines in, it becomes a little bit more elaborate:
perl -ne 'print unless m/^\s*-+[\s-]*$/' <infile.txt >outfile.txt

I ran and tested it, so it should work fine now.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question