|
| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
Batch file
|
Original Message
|
Name: msammut
Date: April 15, 2005 at 05:46:02 Pacific
Subject: Batch fileOS: Win2kCPU/Ram: P4 256 |
Comment: Does anyone know how I can extract the first 8 and the last 10 lines of a text file and output them into another text file, or two seperate text files, either way will do.
Report Offensive Message For Removal
|
|
Response Number 3
|
Name: dtech10
Date: April 17, 2005 at 08:55:24 Pacific
|
Reply: (edit)Hi Matthew This I think would be difficult to write in Standalone DOS. WinXP/2000 extended commands would help. I could write a C.Exe program for you to if nobody comes up with a batch file solution.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: FishMonger
Date: April 17, 2005 at 13:48:48 Pacific
|
Reply: (edit)C would have the fastest execution time (by fractions of a second) but Perl would be the shortest and easiest to read/write. #!perl -w @lines = <>; print $lines[$_] for (0..7); print splice(@lines, -10);
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: Mechanix2Go
Date: April 18, 2005 at 00:15:50 Pacific
|
Reply: (edit)Hi FishMonger, Neat stuff. I don't know any of these languages, but the perl must ba as short and sweet as it gets. I have a few unix-like utils which are indispensable. As ever, it's challending to do stuff like this in DOS. A DOS script for this would probably look like a family reuniun size bowl of spaghetti. M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: msammut
Date: April 18, 2005 at 03:02:29 Pacific
|
Reply: (edit)i'm running win2k server but the command needs to be in a batch file... can anyone give me some code to try out? i tried using the code posted on another question (http://computing.net/dos/wwwboard/forum/13852.html) which takes the last n lines. it worked fine. I need something like this but to take the first and last n lines. anyone please?
Report Offensive Follow Up For Removal
|
|
Response Number 11
|
Name: Mechanix2Go
Date: April 18, 2005 at 05:12:48 Pacific
|
Reply: (edit)Hi IVO and everybody. The bat at the link in #9 looks pretty slick. I'm still trying to figure out the syntax. M2
If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 12
|
Name: dtech10
Date: April 18, 2005 at 09:20:45 Pacific
|
Reply: (edit)Hi Thats Win2k syntax. If thats what you want and not stanalone dos. Try this. @echo off rem put something in the quotes of the find rem that would not be found in your file. type YourFile.txt | find /v /n "~@~" >~tmp.txt for /f "tokens=1,* delims=[]" %%a in ('type ~tmp.txt') do set NoLines=%%a set Count=0 set /a NoLines=%NoLines%-11 if exist ~NewFile.txt del ~NewFile.txt for /f "tokens=*" %%a in (YourFile.txt) do call :GetLines %%a del ~tmp.txt exit /b :GetLines %* if %Count% LSS 8 echo %* >> ~NewFile.txt if %Count% GTR %NoLines% echo %* >> ~NewFile.txt set /a Count+=1 exit /b
Report Offensive Follow Up For Removal
|
|
Response Number 13
|
Name: FishMonger
Date: April 18, 2005 at 09:57:33 Pacific
|
Reply: (edit)Mechanix2Go, It's clear (from this thread & others) that Perl is my favorite language. For this task the Perl script can be rewritten and executed as a command line instead of a seperate script. If required, the batch file could be used as a wrapper to the Perl command/script. There are only 2 reasons I can see for Mathtew not to use Perl or head and tail. 1) This is a homework assignment. 2) Matthew doesn't have admin rights to install the proper tools. Have fun everyone! %00
Report Offensive Follow Up For Removal
|
|
Response Number 14
|
Name: Mechanix2Go
Date: April 18, 2005 at 10:04:39 Pacific
|
Reply: (edit)Hi dtech10, That's DOS? Back on the subject of the FIRST several lines, try this: ::** outputs first x lines @echo off > quit.bat if %2'==' echo needs two parameters: filename number_of_lines & goto :eof set /a #=%2 for /f "tokens=*" %%L in (%~f1) do call :output %%L goto :eof :output if %#% LEQ 0 goto :eof set /a #=%#%-1 echo %* ::** M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 15
|
Name: dtech10
Date: April 18, 2005 at 13:44:04 Pacific
|
Reply: (edit)Hi Mechanix Thanks for the code, We all learn from the way other programmers code, I myself have learned a lot from other peoples code on this site. Fishmonger maybe right about admin rights. Perl would be much neater solution. if required nobody in their right would do it with a MsDos Batch file if they did'nt need to. I and others do it because it a challenge to do it the hard way. No disrespect intended to anyone.
Report Offensive Follow Up For Removal
|
|
Response Number 17
|
Name: FishMonger
Date: April 19, 2005 at 04:53:50 Pacific
|
Reply: (edit)The Perl script would be called from within the batch file just like any other command, e.g., @echo off cls call perlscript.pl file.txt > out.txt "out of commission" for awhile, I'm leaving for the hospital for heart surgury so, I'll check back-in in a coulple weeks. %00
Report Offensive Follow Up For Removal
|
|
Response Number 18
|
Name: msammut
Date: April 19, 2005 at 05:12:14 Pacific
|
Reply: (edit)Hi dTech10... your code seems to work fine. but it gives different results with different lengths of text files....and on the large text file i have it does not return the end lines, just the first few. M2Go... havent managed to work your code. though that would just give me the first lines? not the end ones too? could you give me an example of the code taking 5 lines from top, 8 lines from bottom of example.txt and putting into output.txt please :)
Report Offensive Follow Up For Removal
|
|
Response Number 19
|
Name: Mechanix2Go
Date: April 19, 2005 at 05:45:33 Pacific
|
Reply: (edit)Hi Matthew, In #9 you said the code worked for 'last lines' so is that not useable? Or is it simply a matter of integrating the two scripts together to get fist# and last# in one go? I have not studied dtech10's code, but while testing mine, I happened to use it on a file which had email addresses like this: <joe@domain.org> [all with leading and trailing 'angle brackets' or whatever you call them] and the script put out NO lines. Whithout extensive testing, my instinct is that the < & > trip up the BAT because they are 'redirect' symbols. With luck, this may only be an issue with those symbols at the beginning or end of a line. But if your files contain these chars, I would do some testing. *** I just trie dtech10's and with a file of twenty lines, it got ALL BUT lines 9 and ten. ?? M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 20
|
Name: dtech10
Date: April 19, 2005 at 05:56:39 Pacific
|
Reply: (edit)Hi The code as written assume's that the your txt file is greater than the 18 lines required. This could be modified. Why it's not working on longer files, I'm not sure, Rem out the line "del ~tmp.txt" and look at it, you will see that it's line-numbered. Is the lines in your file numbered correctly. How long is the txt.files you are trying to read, maybe the find /n as a limit for it's line numbers.
Report Offensive Follow Up For Removal
|
|
Response Number 21
|
Name: Mechanix2Go
Date: April 19, 2005 at 06:06:39 Pacific
|
Reply: (edit)Hi dtech10, I REMd out the del and the ~tmp has all 20 lines. The original yourfile.txt has 20 lines. Short ones; one word each. Size: 152 bytes. M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 22
|
Name: msammut
Date: April 19, 2005 at 06:36:49 Pacific
|
Reply: (edit)M2Go... Yes is it possible to join the two scripts into one to get the first AND last n lines of a text file? can you give me an example of the script using the values i gave u in my previous post please?
Report Offensive Follow Up For Removal
|
|
Response Number 23
|
Name: Mechanix2Go
Date: April 19, 2005 at 08:24:56 Pacific
|
Reply: (edit)Hi Matthew, Well, we're getting close. Try this: ::** @echo off if %4'==' echo 4 params #top #bottom infile outfile & goto :eof If not exist %~f3 Echo File %~f3 not found & Goto :EOF type nul > %4 set outfile=%4 :main call :first# %1 %3 set outfile= set #= call :last# %2 %3 %4 goto :eof :first# set /a #=%1 for /f "tokens=*" %%L in (%2) do call :output %%L goto :eof :output if %#% LEQ 0 goto :eof set /a #=%#%-1 echo %* >> %outfile% goto :eof :last# Echo Extracting last %1 lines from %~f2 into %~f3 For /F "tokens=3 delims=:" %%A in ('Find /V /C "#~#" %~f2') Do Set RN=%%A Set /A SN=%RN%-%1 If %SN% leq 0 Echo Too many lines [File contains %RN% lines] & GoTo :EOF Echo Skipping %SN% lines from %RN% For /F "tokens=* skip=%SN% delims=" %%A in (%~f2) Do Echo %%A>> %~f3 Set RN= Set SN= goto :eof ::** M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 24
|
Name: wizard-fred
Date: April 19, 2005 at 12:49:04 Pacific
|
Reply: (edit)As a person who can't keep track of the variables in a batch, I tried this in BASIC. I made a possible solution. Worked from 6 to 210,000 lines (5 seconds). If you are interested, email me. Present version runs in DOS prompt, 8.3 file names. EXE Compiled PowerBasic.
Report Offensive Follow Up For Removal
|
|
Response Number 25
|
Name: Mechanix2Go
Date: April 19, 2005 at 12:53:20 Pacific
|
Reply: (edit)Hi wizard-fred, Yeah, join the club on losing track of cariables. I'm studying a QBasic tutorial now. ;) M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 28
|
Name: msammut
Date: April 20, 2005 at 00:35:29 Pacific
|
Reply: (edit)Hi M2Go... thanks for the code but i'm having a bit of trouble interpreting it.... which are the variables i need to change? where do i put the inputfile.txt? where do i put the outputfile.txt? where do i put the number of lines from top? where do i put the number of lines from bottom? excuse my ignorance :)
Report Offensive Follow Up For Removal
|
|
Response Number 29
|
Name: Mechanix2Go
Date: April 20, 2005 at 00:47:41 Pacific
|
Reply: (edit)Hi Matthew, No prob. If you type the name of the BAT with fewer than 4 parameters, you'll get a syuntax hint. The first param is the number of lines off the top. The second is the number of lines off the bottom. Third is the input file. Fourth is the output file. So for your example in #18: BATname 5 8 example.txt output.txt M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 30
|
Name: msammut
Date: April 20, 2005 at 01:27:47 Pacific
|
Reply: (edit)Great! that worked very well. I have one more request though... is there any way of not ommitting tab indentation when extracting the lines of text? basically the lines i'm extracting are in a sort of columnar format, when i extract them the headings of the columns are not where they are meant to be. help?!
Report Offensive Follow Up For Removal
|
|
Response Number 31
|
Name: Mechanix2Go
Date: April 20, 2005 at 01:39:42 Pacific
|
Reply: (edit)Hi Matthew, Not at all obvious how to keep from losing tabs. Maybe IVO knows. I think the 'token' facility sees each string [some chars together with no 'white sapce'] separated by 'white space' which can be a 'space' or a 'tab'. Hence the tabs get lost in the shuffle. *** Are the disappearing tabs which cause your columns to get messed up, ONLY ion the first line? M2
If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 32
|
Name: msammut
Date: April 20, 2005 at 02:19:58 Pacific
|
Reply: (edit)Nope they're in the end lines actually. For example: ---------------- Total Copied Skipped Dirs : 86 0 Files : 402 26 Bytes : 104.1 m 22.1 m 82.0 Ended : Tue Apr 19 20:04:44 2005
Report Offensive Follow Up For Removal
|
|
Response Number 33
|
Name: Mechanix2Go
Date: April 20, 2005 at 02:45:48 Pacific
|
Reply: (edit)Hi Matthew, I took this block: Total Copied Skipped Dirs : 86 0 Files : 402 26 Bytes : 104.1 m 22.1 m 82.0 *** and replaced the spaces with tabs. Now it looks like this: Total Copied Skipped Dirs : 86 0 Files : 402 26 Bytes : 104.1 m 22.1 m 82.0 *** I tsurely looks different, but I'm not so sure it looks better. Notice that the line beginning with 'Bytes' would 'look better' if the was no space [subsequently changed to tab] between the number of bytes and the 'm'. *** I'm willing to work more on this but we need to be very clear about what we're dealing with. Are you ALWAYS taking the first 5 and last 8 lines? Can you zip ut the output.txt and email it? BUT name it output.pzi because I have zips blocked. mailto: 15622 at golden-triangle.com M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 34
|
Name: Mechanix2Go
Date: April 20, 2005 at 02:47:33 Pacific
|
Reply: (edit)Well, nice try. It dowsn't look any different posted here. But in a file viewer it dos. M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|
|
Response Number 35
|
Name: msammut
Date: April 20, 2005 at 03:32:59 Pacific
|
Reply: (edit)Sorry about that mate.... seems that your batch file was working correctly and keeping tabs. it was the mailing program i was using that removed the tabs!! Thanks for all your help! Much appreciated.
Report Offensive Follow Up For Removal
|
|
Response Number 37
|
Name: Mechanix2Go
Date: April 22, 2005 at 20:50:40 Pacific
|
Reply: (edit)Hi folks, Just for drill, here's an EXE version: http://www.Golden-Triangle.com/FrstLast.zip M2 If at first you don't succeed, you're about average.
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Disk Operating System Forum Home
|
|
|