| 1. |
Solve : Need to pull info from a large number of text files in a directory? |
|
Answer» I need to pull info from a large number of textfiles in a directory and aggregate this to another textfile. The info needed is two lines (line 1 and line 9) in the textfile, and these lines are consistently lines 9 and 16 across all the textfiles. The info needed is two lines (line 1 and line 9) in the textfile, Then you said Quote these lines are consistently lines 9 and 16 across all the textfiles. Which is it? Dias, i think he wants to take the info from lines 1 and 9 in the text files then copy the info, and then paste the info to lines 9 and 16 on one txt file.dec27 i will find how to do that but in the mean while i need to know what operating sys u ar using so i can get the correct code. Quote from: macdad- on March 18, 2008, 05:25:03 PM Dias, i think he wants to take the info from lines 1 and 9 in the text files then copy the info, and then paste the info to lines 9 and 16 on one txt file. That's a very odd requirement indeed, if that is so. try this. Code: [Select] echo off cls md admin copy U:\abailey\NetLogon.txt \admin copy U:\ablay\NetLogon.txt \admin copy U:\abreen\NetLogon.txt \admin echo Login Information Archived pause exit i tried doing wat u said by copying specific lines but it cant be done automaticly. the code above will copy the "NetLogon" files from each of the folders to a folder that will be created automaticly called "admin". make sure u copy the code above to notepad and save it as a bat file. hope this works for u. Quote from: macdad- on March 19, 2008, 11:20:22 AM i tried doing wat u said by copying specific lines but it cant be done automaticly. Yes it can. how? the only way i can think of copying specific lines is by using edlin but u can only do that manually.Set a counter variable equal to 1 using set /a Read the text file line by line in a FOR loop, adding 1 to the variable each time with set /a If the variable value is equal to the line number number desired, do something with that line. Or you can use SED but that is not part of Windows so some PEOPLE MIGHT think it was cheating. I am not trying to hijack this or anything but I am having the same problem but I am looking at only trying to find a certain lien that could be anywhere in the text file but I am looking through at this time about 4,000 files. I am not ever sure where to start on the coding of this. Is there a way to do this since It is a bit different than what the original poster is trying to do.This is how you get a selected line from a text file without using SED Code: [Select] echo off setlocal enabledelayedexpansion REM create test file echo 1 Line one > test.txt echo 2 Line two >> test.txt echo 3 Line three >> test.txt echo 4 Line four >> test.txt echo 5 Line five >> test.txt echo 6 Line six >> test.txt echo 7 Line seven >> test.txt echo 8 Line eight >> test.txt echo 9 Line NINE >> test.txt echo 10 Line ten >> test.txt echo 11 Line eleven >> test.txt REM show test file echo. echo Here is the test file: echo. type test.txt echo. set /p chosenline=Choose a line to display? echo. set /a line=1 for /f "delims==" %%L in (test.txt) do ( if !line!==%chosenline% echo %%L set /a line=!line!+1 ) echo. Quote from: guardian on March 19, 2008, 12:06:01 PM I am not trying to hijack this or anything but I am having the same problem but I am looking at only trying to find a certain lien that could be anywhere in the text file but I am looking through at this time about 4,000 files. I am not ever sure where to start on the coding of this. Is there a way to do this since It is a bit different than what the original poster is trying to do. Describe some features of the line you are looking for but wont it be easier to just copy the files. Quote but wont it be easier to just copy the files. If you just want one line out of each file, why clutter up your disk space uselessly copying hundreds or thousands of files? When you still have to look through them to find the lines that you want? Quote from: dec27 on March 18, 2008, 10:55:09 AM I need to pull info from a large number of textfiles in a directory and aggregate this to another textfile. The info needed is two lines (line 1 and line 9) in the textfile, and these lines are consistently lines 9 and 16 across all the textfiles.if you can download and use gawk from here: Code: [Select]#save the below script as script.awk # assumes you only want to get line 1 and 5 { i=1 while( ( getline line < $0 ) > 0 ) { if (i==1 || i==5) print line > "newfile" i+=1 } } output: Code: [Select]C:\test>dir /B /S NetLogon.txt | gawk -f script.awk |
|