How to delete rows in a file based on the file name.


I have a request to delete rows in a file based on the file name.

File name: SMC_ELEMENTTest_ITEM_123_201_59_Base.txt



File name: SMC_ELEMENTTest_ITEM_456_201_59_Base.txt



The file name is basically the same except for the number I put in bold.

What is the best way to accomplish this task?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is Perl your tool of choice or the only tool at your disposal?
Bill PrewIT / Software Engineering ConsultantCommented:
Not sure if you are even working in Windows, but here is a basic BAT script that could do the job.  Save as a BAT and then run as follows:

EE28699979.bat SMC_ELEMENTTest_ITEM_123_201_59_Base.txt SMC_ELEMENTTest_ITEM_123_201_59_Base.out

See if that does what you want.

If you really want to replace the single input file with the new output we can add some logic to the script to cover that, but for a first test I kept it simple in case you were wanting a different OS or script language (like Perl).

@echo off

if "%~1" EQU "" (
  echo Missing input file name.
  exit /b

if "%~2" EQU "" (
  echo Missing output file name.
  exit /b

for /f "tokens=4 delims=_" %%A in ("%~n1") do (
  find /i "|%%A|" <%~1 >%~2

Open in new window

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Using Powershell:
create a folder called c:\temp\FileNums and copy few of your files in that folder and follow the steps to test.
Save the following as Processfiles.ps1 and right click the file and "run with Powershell"
$files = Get-ChildItem C:\temp\FileNums -Filter *.txt

foreach ($file in $files)
$num = $($[21..23] -join ''
$newcontent = Get-Content $($file.Fullname) | Select-String $num  
$newcontent | out-file $($file.Fullname) -Force


Open in new window

xbox360dpAuthor Commented:
Hi All,

Perl is the language of choice and I'm in a Linux environment.

This should do it...
use strict;
use warnings;
foreach my $fil (@ARGV) {
    die "$fil not found" unless (-f $fil);
    unless ($fil =~ m{_ITEM_(\d+)_}) {
        die "could not find item number in file name";
    my $itm = $1;
    rename $fil, "$fil.bak" or die "could not backup $fil: $!";
    open my $in, "$fil.bak" or die "could not open $fil: $!";
    open my $out, '>', $fil or die "could not write $fil: $!";
    while (<$in>) {
        print $out $_ if m{\|$itm\|};
    close $in;
    close $out;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.