Operating System - HP-UX
1833467 Members
2535 Online
110052 Solutions
New Discussion

Trying to extract data from multiple files

 
SOLVED
Go to solution
Ken Opersteny
Occasional Contributor

Trying to extract data from multiple files

I'm trying to write a script that will extract several lines of information from multiple ascii files and then parse it to a single file.

I'm trying to get the FILENAME of the file and 1 or more lines that start off with "#Description:". The "Description" lines are not always on the same line number. I want the output file to read something like:

FILENAME DESCRIPTION

filename1 does whatever
filename2 checks whatever
also does whatever

Thanks in advance!

Ken
3 REPLIES 3
Rodney Hills
Honored Contributor

Re: Trying to extract data from multiple files

A simple "grep"-

grep "^#Description" yourfiles*

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Trying to extract data from multiple files

Here's one method that takes advantage of the shell's read command.

read V1 V2 would read
#DESCRIPTION This is a test
and stuff "#DESCRIPTION" into V1 and everything else (because V2 is the last variable) into V2.

#!/usr/bin/sh

FILES="myfile1 myfile2 myfile3"
echo "FILENAME\tDESCRIPTION"
echo
for FILE in ${FILES}
do
grep -E -q "^#DESCRIPTION" ${FILE}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then #found
grep -E "^#DESCRIPTION" ${FILE} | while read V1 V2
do
echo "${FILE}\t${V2}"
done
fi
done

Note that we intentionally ignore ${V1} because it contains the known string "#DESCRIPTION".
If it ain't broke, I can fix that.
Ken Opersteny
Occasional Contributor

Re: Trying to extract data from multiple files

Thanks for the help.

Clay's script is exactly what I was trying to do.