Create new file by searching another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create new file by searching another file
# 1  
Old 10-30-2003
Create new file by searching another file

Hi ,

I need to write a korn shell to read a file, say Test.txt, and search the file for key words like "email", "name", etc line by line. Whatever is found on a line, send it to an output file. Do this until the end of the file. I have the following code below, that I'm trying to tweak but it doesn't work. Could you please let me know what is wrong:

++++++++++Script Begins Here++++++++++++++++++++++

#!/bin/ksh
LDAPFILE=Test.txt
# Function Name: Initialize_Variables
# Description : This function initializes the employee variables.
Initialize_Emp_Variables()
{
FNAME=""
LNAME=""
MNAME=""
EMAIL=""
DEPT=""
}

# Function Name: Process_Ldap_File
# Description : This function reads the ldap file line-by-line and
# parse it and store values of first, middle, last names
# and email and department in member variables. At the end
# of the record, it calls Update_Email_For_Name function.
Process_Ldap_File()
{
Initialize_Emp_Variables
while read data
do
# get the attribute name and attribute value from the line just read
ATTNAME=`echo $data | cut -f 1 -d :`
ATTVALU=`echo $data | cut -f 2 -d :`

# if the attribute name collected in the previous step is not null
# then it is valid data
if test $ATTNAME
then
# if the attribute name is email, store data in email
if test $ATTNAME = "mail"
then
EMAIL=$ATTVALU
fi

# if the attribute name is givenname, store data in first name
if test $ATTNAME = "givenname"
then
FNAME=$ATTVALU
fi

# if the attribute name is middleinitial, store data in middle name
if test $ATTNAME = "middleinitial"
then
MNAME=$ATTVALU
fi

# if the attribute name is sn, store data in last name
if test $ATTNAME = "sn"
then
LNAME=$ATTVALU
fi

# if the attribute name is department, store data in department
if test $ATTNAME = "department"
then
DEPT=$ATTVALU
fi

# if the attribute name is dn, it is start of new data set.
# update the employee email (if it is not a valid email address)i
# and initialize the member variables
if test $ATTNAME = "dn"
then
if test $LNAME
then
# call update email routine to update email address of the employee
Update_Email_For_Name
fi
Create_Input_File
fi
fi
done < $LDAPFILE
#
# at end, display the last employee details
if test $LNAME
then
# call update email routine to update email address of the employee
Create_Input_File
fi
}

# **************************
# Create load file function
# **************************
Create_Input_File()
{
$EMAIL $FNAME $MNAME $LNAME $DEPT >>$HOME/trial.csv
}


++++++++++++Script Ends Here+++++++++++++++++
# 2  
Old 10-30-2003
wow thats entierly to much work for this assignment.

why dont you just use egrep and direct the output to a file?
# 3  
Old 10-30-2003
... or use awk to pattern matching on the words and perform a little bit of processing if required (e.g. putting the different matched lines into separate files).
# 4  
Old 10-30-2003
That's why I'm posting this thread - to get ideas. Could you provide an example.

Thanks in advance,
Leo
# 5  
Old 10-30-2003
If you're trying to look up several at once, one possible way is using grep:
grep -e email -e name Test.txt > resultFile

----
which, now that I reread through the thread, is what Optimus_P was telling you to try anyway

Last edited by oombera; 10-30-2003 at 02:38 PM..
# 6  
Old 10-30-2003
You could run:
Code:
for Pattern in email name etc
do
  grep "${Pattern}" in DataFile.txt >${Pattern}.txt
done

...but remember, you may get a 1->many occurence of lines that match more than one pattern.
# 7  
Old 10-30-2003
An even better way would be to just list all the values you want to search for inside a file (one word per line), and call it something like wordsToFind.

Then use:

grep -f wordsToFind Test.txt > resultFile
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

2. Shell Programming and Scripting

Searching a file inside a .tar.gz file by date

Hi, I would like to ask if there is a way to search for a file inside a .tar.gz file without extracting it? If there is, is there a way to search for that file by date? Thanks! (4 Replies)
Discussion started by: erin00
4 Replies

3. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

4. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

5. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

6. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

7. Shell Programming and Scripting

create file as variable for searching point

Hi Friends, I need expert help:), I have bellow script that function for searching string in multiple file, the script is working well. but I thing it still can be optimize since so many repetition in bellow command, where string that I marked BOLD italic is clue for what I am looking for... (2 Replies)
Discussion started by: budi.mulya
2 Replies

8. Shell Programming and Scripting

searching a log file and appending to a .txt file

I'm new to shell scripting and am writing a script to help me log the free memory and hd space on a server. As of now, the script just runs 'df -h' and appends the output to a file and then runs 'top' and appends the output to a log file. What I want to do, is have the script also search the... (3 Replies)
Discussion started by: enator45
3 Replies

9. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question