Strip the first record off a file and isolate fields off of it

I have a 2 part question on how to this in unix scripting using kshell or c shell.

I have a file described below:
1st record has 2 fields on it
every other record has 22 fields on it.

Example
ABC, email address
Detail 1
Detail 2
Detail 3
.
.
.

1st question is how can I create 2 files from the one file. The first file will just have the first record on it and the second file will contain all the other records on it.

2nd question is then, how can I assign the data from the 1st record to environment variables.

Based on the example above. I want to take the first record and take the ABC and assign that to environment variable and then assign the email address to an environment variable.

Any feedback would be greatly appreciated.

You have been getting our help writing scripts for six and a half years. What have you tried to solve this problem on your own?

What two files do you want to create?

What two environment variables do you want to create? What is the rest of your script going to do with these environment variables? If your goal is to split one file into two files and add two variables to your environment. It is easy to do, but adding variables to the environment is a waste of time. As soon as your script exits, anything that your script added to the environment will disappear because environment variables created in a child process DO NOT magically appear in the environment of any parent processes; they only appear in the shell execution environment of your shell script and in the environment of children created by your script and its descendants.

Hi ,

the first portion of your problem seems to be a easy one.
you can use head and tail to extract the required record from the parent file and then redirect to create other files as required.

in current scenario you can do

head -1 parentfile > firstfile
tail +2 parentfile > secondfile

here, the firstfile would contain the only the header record
[head -1 is first record from top]
and the secondfile would contain all the records except the first one
[tail -1 is the first record from bottom, tail +1 is the all the records from top from 1st record onwards]

And regarding the 2nd issue, as Don has correctly pointed out there is no point in creating env variables, as it does not solve the purpose.
may be we can help better if you clearly mention what exactly is your requirement.

Thanks
Goutam

Thanks for the input. I just want to say that I support a system from unix, oracle, web, and mainframe. I work in unix and with scripts about once a year, so even though I have been a member of this forum for 6.5 years, I do not work with this day in and day out. I had been playing with split and csplit. It didn't even occur to me to use head and tail, but once I saw the solution presented it made perfect sense. As I said, I don't do much of this on a regular basis. As far as the env variables, I will be using a grep and awk to do what I want. I just need to set the env vars, to pass to another process. Thanks again.

You could also consider something like:

#!/bin/ksh
export var1 var2
exec 3< parentfile
IFS=, read -r var1 var2 <&3
printf '%s,%s\n' "$var1" "$var2" > firstfile
cat <&3 > secondfile

Or, if you'd like to use the variables read from the 1st line while processing remaining lines in your input file:

#!/bin/ksh
export var1 var2
exec 3< parentfile
IFS=, read -r var1 var2 <&3
printf '%s,%s\n' "$var1" "$var2" > firstfile
while IFS= read -r line
do	printf '%s %s %s\n' "$var1" "$line" "$var2"
done <&3 > secondfile

If parentfile contains the text shown in post #1 in this thread:

ABC, email address
Detail 1
Detail 2
Detail 3
.
.
.

This last script will produce firstfile containing:

ABC, email address

and secondfile containing:

ABC Detail 1  email address
ABC Detail 2  email address
ABC Detail 3  email address
ABC .  email address
ABC .  email address
ABC .  email address

Note that there is an unwanted space before the email address in secondfile because there is a space after the field separator (a comma) in the first line of your sample input file that becomes part of the value assigned to var2 .

Although this these scripts were written and tested using a Korn shell, both of these will work with any shell that uses standard POSIX shell command language syntax.