[NTLUG:Discuss] regexp/script
Eric Schnoebelen
eric at cirr.com
Tue Oct 23 14:10:02 CDT 2001
"Daniel Hauck" writes:
- You know? For lack of a simple shell-based solution popping into
- my mind, I'd write a short and simple C program do to the work
- for me.
You're not thinking of all the features of the shell
then.
Problem Statement:
Given a list files that contains a finite number
of lines of input, generate a single output stream
consisting of the name of the file, followed by
the lines of the file, concatenated into a single
line with commas.
Solution (in Bourne Shell):
#! /bin/sh
# iterate over the list of files provided on the
# command line. "$@" is the quoted list of
# arguments
for file in "$@"; do
# assuming BSD echo here, sysv would look like:
# echo "${file},\c"
echo -n "${file},"
# read the line from the file
while read line ; do
echo -n "${line},"
# < ${file} means that the loop gets
# its input from ${file}
done < ${file}
# dump out a trailing newline, since we've
# been suppressing them up until now.
echo ""
done
- ----- Original Message -----
- From: "Wrenn, Bobby J." <Bobby.Wrenn at banctec.com>
- To: <discuss at ntlug.org>
- Sent: Tuesday, October 23, 2001 12:10 PM
- Subject: [NTLUG:Discuss] regexp/script
-
-
- > I have some files (209 total) with customer address information.
- > I need to get the customer address into a comma delimited file
- > with the file name (minus extension) as the first field.
- >
- > The file names have spaces in the name and a .txt extension
- > Each file has the address embedded like this:
- > ============================
- > Equipment Location
- > __________________
- > Comments
- >
- > _________
- > Customer Name
- > 123 Somestreet
- > Somecity ST 12345
- >
- > ===========================
If there are headers involved, you'll need to apply some
more advanced parsing to the while loop above, to drop the
headings. A case or if statement should be able to do the
needed pattern matching.
- > Is there an easy way to get the addresses out of the files?
- > Because I am trying to learn this, an explanation of the process
- > would be helpful. Ifit is more complicated than I anticipate,
- > please contact me off list with your price for the answer. I
- > can expense a small fee.
How about a beer?? :-)
--
Eric Schnoebelen eric at cirr.com http://www.cirr.com
When your hammer is C++, everything begins to look like a thumb.
-- Steve Haflich, comp.lang.c++
More information about the Discuss
mailing list