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+++++++++++++++++
9 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
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
2. Shell Programming and Scripting
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
3. Shell Programming and Scripting
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
4. Shell Programming and Scripting
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
5. UNIX for Dummies Questions & Answers
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
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
7. Shell Programming and Scripting
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
8. Shell Programming and Scripting
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
9. Shell Programming and Scripting
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
read(1) General Commands Manual read(1)
NAME
read - read a line from standard input
SYNOPSIS
var ...
DESCRIPTION
reads a single line from standard input. The line is split into fields as when processed by the shell (refer to shells in the first field
is assigned to the first variable var, the second field to the second variable var, and so forth. If there are more fields than there are
specified var operands, the remaining fields and their intervening separators are assigned to the last var. If there are more vars than
fields, the remaining vars are set to empty strings.
The setting of variables specified by the var operands affect the current shell execution environment.
Standard input to can be redirected from a text file.
Since affects the current shell execution environment, it is usually provided as a normal shell special (built-in) command. Thus, if it is
called in a subshell or separate utility execution environment similar to the following, it does not affect the shell variables in the
caller's environment:
Options
recognizes the following options:
Do not treat a backslash character in any special way.
Consider each backslash to be part of the input line.
Opperands
recognizes the following operands:
var The name of an existing or nonexisting shell variable.
EXTERNAL INFLUENCES
Environment Variables
determines the internal field separators used to delimit fields.
RETURN VALUE
exits with one of the following values:
0 Successful completion.
>0 End-of-file was detected or an error occurred.
EXAMPLES
Print a file with the first field of each line moved to the end of the line.
while read -r xx yy
do
printf "%s %s
" "$yy" "$xx"
done < input_file
SEE ALSO
csh(1), ksh(1), sh(1), sh-posix(1).
STANDARDS CONFORMANCE
read(1)