Saved Bookmarks
| 1. |
Solve : Script for taking first 3 lines fromm input file? |
|
Answer» I need a script where it contains more than 100 lines. From that file i need first 3 lines and i need to send to another new file.Quote from: Gokul on July 06, 2010, 12:00:18 PM I need a script where it contains more than 100 lines. From that file i need first 3 lines and i need to send to another new file. Output First three lines with Unix Head Command. The Head command can be dowlnloaded to windows. C:\test>type x.bat @echo off rem CNU wrote: rem "I am not familiar with batch commands. I need HELP from you guys to create a batch file. rem I am using some kind of tool that starts Building periodically every 5 mins if there are rem ny new files or binaries placed in that folder. rem Now I need a batch file that recognises latest binaries and triggers the build. rem When the job is done, then it should inform the building tool that the binaries rem have been tested." rem It seems that CNU is suggesting a log of a folder as the folder grows. if EXIST timefile*.txt del timefile*.txt dir > folderlog.txt rem for four times add dir info to log each time interval ( command line arg %1 ) set /a c=0 :timer if %c%==4 goto end set /a c=%c% + 1 time /t > timefile%c%.txt sleep.exe %1 time /t >> timefile%c%.txt dir >> folderlog.txt type folderlog.txt goto timer :end Output First three lines with Unix Head Command. The Head command can be dowlnload to windows. C:\test>head -3 x.bat @echo off rem CNU wrote: rem "I am not familiar with batch commands. I need help from you guys to create a batch file. C:\test>Who is CNU? what's with the nonsensical stuff, marvin? I found this in the snippet closet. It's not bulletproof, but it does have some error trapping. You may want to add a path for the output file (output.txt). This solution does not require a 3rd party program download. Code: [Select]@echo off setlocal enabledelayedexpansion :doOver set /p fName=ENTER Qualified File Name: if not exist %fname% goto doOver :numeric set /p nLines=Enter Number Of Lines To Select: echo %nLines%|findstr /r "[^0-9]" > nul if not errorlevel 1 goto numeric for /f "tokens=* delims=" %%v in (%fName%) do ( echo %%v >> output.txt set /a count+=1 if !count! EQU %nLines% goto :eof ) Good luck. I thought of something along those lines, but I hesitated because it would not handle absolutely any text file; it would not handle correctly BLANK lines or lines containing certain characters, and would bomb if a line contained the & character. I wonder about VBscript? I found the original code does not handle file names with embedded spaces, so it was changed to use the short name. Special characters seem to be seen transparently by the for command and do not seem to be a problem. The was tested with all the characters found above the number keys. Code: [Select]@echo off setlocal enabledelayedexpansion :doOver set /p fName=Enter Qualified File Name: if not exist "%fname%" goto doOver for /f "tokens=* delims=" %%v in ('dir /s /b "%fName%"') do set fName=%%~sv :numeric set /p nLines=Enter Number Of Lines To Select: echo %nLines%|findstr /r "[^0-9]" > nul if not errorlevel 1 goto numeric for /f "tokens=* delims=" %%v in (%fName%) do ( echo %%v >> output.txt set /a count+=1 if !count! EQU %nLines% goto :eof ) Good luck. Input.txt (4 lines) Code: [Select]Oranges & lemons say the bells of St Clements 50% of people eat butter hello! )Goodbye < > !%%&( Asked for first 3 lines... Output.txt Code: [Select]Oranges & lemons say the bells of St Clements 50% of people eat butter hello%%&( Also any line starting with a ; is skipped altogether. Rather than digging deeper, a VBScript solution is looking better and better. Code: [Select]Const ForReading = 1 Const ForWriting = 2 Set fso = CreateObject("Scripting.FileSystemObject") Do WScript.STDOUT.Write "Please enter file name: " strFile = WScript.StdIn.ReadLine If fso.FileExists(strFile) Then Exit Do Loop Do WScript.StdOut.Write "Please enter number of lines: " pLines = WScript.StdIn.ReadLine If IsNumeric(pLines) Then Exit Do Loop Set inFile = fso.OpenTextFile("c:\temp\text.txt", ForReading) Set outFile = fso.OpenTextFile("c:\temp\output.txt", ForWriting, True) Do Until inFile.AtEndOfStream inLine = inFile.ReadLine outfile.WriteLine inLine cLines = cLines + 1 If cLines = CInt(pLines) Then Exit Do Loop inFile.Close outFile.Close Save script with a VBS etension and run from the command prompt as: cscript scriptname.vbs Do not use WScript with this script as stdin and stdout are not supported. Oops! Not fatal but small error in the previous code. Code: [Select]Const ForReading = 1 Const ForWriting = 2 Set fso = CreateObject("Scripting.FileSystemObject") Do WScript.StdOut.Write "Please enter file name: " strFile = WScript.StdIn.ReadLine If fso.FileExists(strFile) Then Exit Do Loop Do WScript.StdOut.Write "Please enter number of lines: " pLines = WScript.StdIn.ReadLine If IsNumeric(pLines) Then Exit Do Loop Set inFile = fso.OpenTextFile(strFile, ForReading) Set outFile = fso.OpenTextFile("c:\temp\output.txt", ForWriting, True) Do Until inFile.AtEndOfStream inLine = inFile.ReadLine outfile.WriteLine inLine cLines = cLines + 1 If cLines = CInt(pLines) Then Exit Do Loop inFile.Close outFile.Close Save script with a VBS etension and run from the command prompt as: cscript scriptname.vbs Do not use WScript with this script as stdin and stdout are not supported. download coreutils if you can. They are TOOLS that can help you with your admin tasks. then just do this Code: [Select]c:\> head -3 file >newfile that's all you need. |
|