Delete 2nd line of text files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Delete 2nd line of text files

#1 Post by SIMMS7400 » 18 Jun 2017 13:16

Hi Folks -

I've written a SQL process that export (3) files. As you may or may not know, when leveraging the SQLCMD Utility, you have the ability to export with or without headers. However, if you export with headers, the 2nd line is just dashes and pipes - which cannot obviously be loaded into a target system.

To get around that, I've been exporting without a header, and then concatenating (2) files together - 1 with the header and 1 with the contact.

I'm doing it like such:

Code: Select all

PUSHD "%LOCALEXPORTPATH%"

SET "F="
FOR %%F IN ( Program Project Task ) DO (

   IF EXIST "%%F.txt" DEL /F /Q "%%F.txt">nul
   
   ECHO Started   ^: %%F View SQL Export >>%LOGFILE%

   CALL "%SQL_CMD_PATH%" -S %MSSQL_SRVR% -U %MSSQL_USER% -P %MSSQL_PSSWRD% -W -s"|" -i "%SQLPATH%TEMPO_%%F.sql" -o "%%FZ.txt"

   IF %ERRORLEVEL%==0 (
      ECHO Completed ^: %%F View SQL Export >>%LOGFILE%
   ) ELSE (
      ECHO Failed ^: %%F View SQL Export >>%LOGFILE%
      SET "F=T"
      GOTO PreAbnormalExit
   )
   
   ECHO %%F | FINDSTR "Program">nul && (

      ECHO Parent_Node^|Name^|Alias^|ProjectType>>"%%F.txt"
      COPY /Y "%%F.txt" + "%%FZ.txt">nul
      DEL /F /Q "%%FZ.txt">nul
   )
   ECHO %%F | FINDSTR "Project">nul && (

      ECHO Parent_Node^|Name^|Alias^|PF_TherapeuticArea^|PrePostCS^|ProjectType>>"%%F.txt"
      COPY /Y "%%F.txt" + "%%FZ.txt">nul
      DEL /F /Q "%%FZ.txt">nul
   )
   ECHO %%F | FINDSTR "Task">nul && (

      ECHO Parent_Node^|Name^|Alias>>"%%F.txt"
      COPY /Y "%%F.txt" + "%%FZ.txt">nul
      DEL /F /Q "%%FZ.txt">nul
   )
)
POPD


Here is the file:

Code: Select all

Parent_Node|Name|Alias|ProjectType
-----------|----|-----|-----------
PF_RESEARCH_TOTAL|PF_CMC_Research|CMC Research|Compound
PF_RD_TOTAL|PF_GA|R&D platforms, initiatives & infrastructure|Non-Compound


Is there a way to just delete the 2nd line of all (3) files?

Thanks!

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Delete 2nd line of text files

#2 Post by einstein1969 » 18 Jun 2017 13:48

This (slice.cmd) may help you.

einstein1969

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Delete 2nd line of text files

#3 Post by penpen » 18 Jun 2017 15:13

If no other line contains just dashes and pipes only, then you might use findstr to filter those lines:

Code: Select all

findstr /V "^[-|]*$" "sample.file"


penpen

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: Delete 2nd line of text files

#4 Post by SIMMS7400 » 18 Jun 2017 15:18

Hi PenPen -

Many other lines do contain dashes.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Delete 2nd line of text files

#5 Post by einstein1969 » 18 Jun 2017 16:30

this skip line 2 of file test.txt and create test2.txt without 2nd line.
need line size <=8190 byte

Code: Select all

@echo off
setlocal disableDelayedexpansion

>test2.txt (
  break | for /F "tokens=*" %%F in (test.txt) do @(
            echo %%F
            exit /b
          )
  for /F "skip=2 tokens=*" %%F in (test.txt) do echo %%F
)


einstein1969

SokSareth
Posts: 1
Joined: 18 Jun 2017 20:52

Re: Delete 2nd line of text files

#6 Post by SokSareth » 18 Jun 2017 21:14

Thanks for your post!
I believe there are many who feel the same satisfaction as I read this article!
I hope you will continue to have such articles to share with everyone!

goldenslot

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Delete 2nd line of text files

#7 Post by penpen » 19 Jun 2017 02:09

SIMMS7400 wrote:Many other lines do contain dashes.
I'm unsure if you have misunderstood what i've written:
Containing a dash (or a pipe) won't remove the line when using the above findstr command.
If there are no other characters except pipes and dashes in a line then it is removed.

penpen

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: Delete 2nd line of text files

#8 Post by SIMMS7400 » 20 Jun 2017 01:31

Hi PenPen -

Oh - get it now. Yes, this works fantastically!!!!

Thank you so much!

Post Reply