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
# 8  
Old 10-30-2003
Without further information regarding the problem, it is difficult to determine the amount of processing required for each field in turn. As an example, there may be a need to reformat certain fields. Using the original code as an indicator, I would suggest the following...

Assuming an input file with records of 2 fields:

e-mail john.doe@domain.com
forename John
middlename Franklin
surname Doe
END

e-mail mary.doe@domain.com
forename Mary
middlename Doris
surname Doe
END

etc

Code:
awk '

/^e-mail/ { Key=1 }
/^forename/ { Key=2 }
/^middlename/ { Key=3 }
/^surname/ { Key=4 }

Key > 0 {
   Data[Key]=$2
   Key=0
}

/^END/ {
   for (c=1; c < 4; c++) {
      printf "%s,",Data[c]
   }
   printf "%s\n",Data[4]

   for (c in Data) delete Data[c]
}
' inputfile.txt >outputfile.txt

...this replicates what I believe Leo is trying to achieve. It appears to be about creating a csv file of fields rather than just banging each record out "as is". Wouldn't you say? Each field can be dealt with in turn to allow field specific processing.

The END entry is just for triggering the output, although the last field name could be used (i.e. surname) if its ordinal position is always last.
# 9  
Old 10-30-2003
Of course, if you wished to proceed using solely the ksh, you could replace if the many if statements with a case/esac.

Also, I would cut up each record as follows:

Code:
cat inputfile | while read inputrecord
do
   echo ${inputrecord} | read tagfield datafield
   case ${tagfield} in
      "e-mail") F1=${datafield};;
      "forename") F2=${datafield};;
      "middlename") F3=${datafield};;
      "surname") F4=${datafield};;
         echo "${F1},${F2},${F3},${F4}"
         unset F1 F2 F3 F4
         ;;   
   esac
done

Note the code above assumes that the surname field is always last!
# 10  
Old 10-31-2003
Hi Simerian,

I tried your code (cut and paste) but it doesn't work. Got error:
syntax error at line 11 : `"${F1},${F2},${F3},${F4}"' unexpected


Any ideas why it did not work.
# 11  
Old 10-31-2003
As a quick guess, it looks to me like
"surname") F4=${datafield};;
should actually read:
"surname") F4=${datafield}

In other words, just remove the two semicolons. But i haven't tested it, nor did I write the original code...
# 12  
Old 10-31-2003
Thanks,

That worked without errors but no output was created. Let me explain what I'm trying to do again:

- I need to search the file for some key words such as "mail" "middleinitial" "sn"
- assign the word after the keyword to a variable.
- If the keyword is "dn" send all the values currently assigned to a file.
- Then begin next search.

Below is a sample of the input file:

+++++++++DATA BEGINS++++++++++++++++
mail: Administration_Requests%DoCDir@mit.edu
givenname: Administration Requests
middleinitial: dominoServerMailInDatabase
sn: top

dn: CN=Administrators
cn: Administrators
mail: Administrators@mit.edu
objectclass: dominoGroup
objectclass: groupOfNames
objectclass: top
# 13  
Old 10-31-2003
You gotta read what Simerian posted, not just cut and paste. He was writing a script to operate on the data that he posted. So the script was an example and you were expected to modify it as required to get what you want. Maybe this is a little closer. But fair warning...I don't have time to test it.
Code:
#! /usr/bin/ksh
exec < inputfile > outputfile
while read tagfield datafield ; do
     case ${tagfield} in
        "mail:") F1=${datafield};;
        "givennane:") F2=${datafield};;
        "middleinitial:") F3=${datafield};;
        "sn:") F4=${datafield};;
        "dn:")  echo "${F1},${F2},${F3},${F4}"
                unset F1 F2 F3 F4
                ;;   
    esac
done
exit 0

# 14  
Old 11-01-2003
Thanks Perderabo, those pesky semi-colons have a mind of their own sometimes! Serves me right for typing it directly into the reply box...

Leo, as was suggested, the idea is not to cut and paste but to read and learn - understand what the script is doing and why it is doing it. Only then will you be able to apply the knowledge gained to other scenarios.
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