Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Concatenate lines

156 views
Skip to first unread message

web...@yahoo.com

unread,
Jun 26, 2008, 4:50:40 PM6/26/08
to
I have 10,000 text files with different data in each file.

I need to capture line 6 and line 147 of each file, concatenate line 6
and line 147 and output that line to a file.

I would like to end up with a file of 10,000 lines, each line would
have each text file's line 6 and line 147 combined.

thanks

Tom Lavedas

unread,
Jun 26, 2008, 6:00:44 PM6/26/08
to

This seemed to work for me ...

@echo off
setlocal
if exist output.txt del output.txt
set source=d:\pathspec\filemask*.ext
:: set source=*.* % for all files in current directory %
attrib +h "%¬f0" % hide this file to avoid processing it %
for %%a in (%source%) do (
find /n /v "" %%a > %temp%\tmp.txt
for /f "tokens=1* delims=]" %%C in (
'find "[6]" ^< %temp%\tmp.txt') do (
set /p=%%D) >> output.txt <nul
for /f "tokens=1* delims=]" %%C in (
'find "[147]" ^< %temp%\tmp.txt') do (
echo.%%D) >> output.txt
set /p=.<nul% optional progress indicator %
)
del %temp%\tmp.txt
attrib -h "%¬f0" % restore %

I have no idea how fast (or low) it might be.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

web...@yahoo.com

unread,
Jun 26, 2008, 7:19:09 PM6/26/08
to

Thanks Tom, works great - the only problem is that it does not bring
over the entire line.

It seems to bring over about 1100 columns, many lines are over 2500
columns.
This must be a limitation in variable size or what find can handle?
Any thoughts?

thanks again

Ted Davis

unread,
Jun 26, 2008, 9:18:43 PM6/26/08
to
On Thu, 26 Jun 2008 16:19:09 -0700, webnntp wrote:

<snip>


> Thanks Tom, works great - the only problem is that it does not bring over
> the entire line.
>
> It seems to bring over about 1100 columns, many lines are over 2500
> columns.
> This must be a limitation in variable size or what find can handle? Any
> thoughts?

awk "NR==6{X=$0};NR==147{print X $0}" source > target

where awk is any decent Windows port - I prefer gawk.exe (free and open
source) from <http://gnuwin32.sourceforge.net/packages/gawk.htm>

If you want a tab between the two lines, change X $0 to X,$0
If you want a space make that X \" \" $0
otherwise, you get no delimiter at all between the lines.

(gawk handles lines at least 6KB long)

--

T.E.D. (tda...@mst.edu) MST (Missouri University of Science and Technology)
used to be UMR (University of Missouri - Rolla).

Tom Lavedas

unread,
Jun 26, 2008, 9:32:17 PM6/26/08
to

It's a command line limitation at 1024 lines.

I guess you need a third party application, like awk, as Ted Davis
suggested.

Tom Lavedas
===========

web...@yahoo.com

unread,
Jun 26, 2008, 11:50:31 PM6/26/08
to
> ===========- Hide quoted text -
>
> - Show quoted text -

Thanks, Gawk worked but I could only do one file at a time. How can I
do all 10,000 files at once ending up with one file with 10,000 lines
of the data I need?

Also I tried to add two other lines, (1 and 56) to the search but it
would only do two searches not four. Is this possible?

Thank you very much

Todd Vargo

unread,
Jun 27, 2008, 12:34:28 AM6/27/08
to

Or perhaps a different tool. i.e. VBScript

Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
fso.DeleteFile "output.txt"
On Error Goto 0

Set fout = fso.OpenTextFile("output.tmp",2,True)

Set f = fso.GetFolder(".")
Set fc = f.Files
For Each f1 in fc
If Lcase(Right(f1.name,3))="txt" Then
Set fin = fso.OpenTextFile(f1.name,1)
For i = 1 to 5:fin.SkipLine:Next
s1 = fin.ReadLine
For i = 7 to 146:fin.SkipLine:Next
s2 = fin.ReadLine
fout.WriteLine s1 & " " & s2
fin.Close
End If
Next

fout.Close
fso.MoveFile "output.tmp", "output.txt"

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Richard Bonner

unread,
Jun 27, 2008, 7:24:36 AM6/27/08
to

*** DOS users can do this with XSET. Depending on the DOS version, one
can use a FOR loop to either issue the commands for each file or have
FOR call a second batch file containing these lines:

TYPE %1 | XSET /LINE 6 LINE6
TYPE %1 | XSET /LINE 147 LINE147
ECHO %LINE6%%LINE147% >> BIGFILE.TXT

Don't forget to reset all variables to nothing at the end of
operations.

Richard Bonner
http://www.chebucto.ca/~ak621/DOS/

web...@yahoo.com

unread,
Jun 30, 2008, 2:19:03 PM6/30/08
to
On Jun 27, 7:24 am, ak...@chebucto.ns.ca (Richard Bonner) wrote:

Thanks to all for your help.

0 new messages