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
line(1) General Commands Manual line(1)
NAME
line - Reads one line from standard input
SYNOPSIS
line
STANDARDS
Interfaces documented on this reference page conform to industry standards as follows:
line: XCU5.0
Refer to the standards(5) reference page for more information about industry standards and associated tags.
OPTIONS
None
DESCRIPTION
The line command copies one line, up to and including a newline, from standard input and writes it to standard output. Use this command
within a shell command file to read from your terminal. The line command always writes at least a newline character.
NOTES
The line utility has no internationalization features and is marked LEGACY in XCU Issue 5. Use the read utility instead.
EXIT STATUS
Success. End-of-File.
EXAMPLES
To read a line from the keyboard and append it to a file, enter: echo 'Enter comments for the log:' echo ': c' line >>log
This shell procedure displays the message: Enter comments for the log:
It then reads a line of text from the keyboard and adds it to the end of the file log. The echo ': c' command displays a : (colon)
prompt. See the echo command for information about the c escape sequence.
SEE ALSO
Commands: echo(1), ksh(1), read(1), Bourne shell sh(1b), POSIX shell sh(1p)
Functions: read(2)
Standards: standards(5)
line(1)