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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strip the first record off a file and isolate fields off of it
# 1  
Old 05-26-2016
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.
# 2  
Old 05-26-2016
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.
# 3  
Old 05-26-2016
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
Code:
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
# 4  
Old 05-27-2016
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.
# 5  
Old 05-27-2016
You could also consider something like:
Code:
#!/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:
Code:
#!/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:
Code:
ABC, email address
Detail 1
Detail 2
Detail 3
.
.
.

This last script will produce firstfile containing:
Code:
ABC, email address

and secondfile containing:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

2. Shell Programming and Scripting

Delete last 2 fields from every record in a file

Sample file record : "20130617003","2013-06-18T07:00:03","OUTWARD","01001011","TEST PLC","","HFX834346364364","20130617","10","DUM87534758","","1.28","826","020201","65879278","","","","","","010101","56789","DUMMY... (3 Replies)
Discussion started by: bigbuk
3 Replies

3. Shell Programming and Scripting

Print all the fields of record using awk

Hi, i want to generate print statement using awk. i have 20+ and 30+ fields in each line Now its priting only first eight fields print statement as output not all. my record is as shown below filename ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

4. Shell Programming and Scripting

Making changes in the fields of a record

:confused: Hi Friends, In the record below i have to make changes in the fields by putting the values stored in the temporary variables, x, y, z, p, q, r: 2) In the TBT record store the values in the various fields as: a) X in a field position 51 to 56 b) Y... (5 Replies)
Discussion started by: kanu_pathak
5 Replies

5. Shell Programming and Scripting

How count number of fields in a record

Dear All , I have the query cat temp.txt |28-07-1997|IF_LEG_DCCT|TOV JV sdfsdfdsfdsfdsCLOSED* KIEV|381015280 I need to count the number of fields in this pipe-seperated file. I beleive this is possible via AWK command. The in above file, output of the count should be 5.... Can some-one... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

6. Shell Programming and Scripting

How do I strip and add tabbed fields to a long text file?

I was wondering if there was a way to modify a tab delimited text file, up to 185,000 lines long by adding a repeated field to a block of 20 data , then repeat the process until the file is processed. The current file looks like: ... (3 Replies)
Discussion started by: saint65
3 Replies

7. Shell Programming and Scripting

Manipulating fields record wise

Hi all, I have an input file with no delimiter. Let us say the file is abc.txt having values for fields namely, EmpNumEnameDesigSalDept. Ofcourse the file has got several records. Every field has got a fixed start and end position. I need to assign the fields to corresponding varibles say... (1 Reply)
Discussion started by: rinku11
1 Replies

8. Shell Programming and Scripting

awk: record has too many fields

Hi, I'm trying this command - but get this error. Do you guys have any workaround for this? cat tf|sed 's/{//g'|sed 's/,//g'|awk '{for (i=1;i<=NF;i++) {if ($i == "OPTIME") {k = i + 2; print $i,$k}}}' awk: record `2005 Jul 28 17:35:29...' has too many fields record number 15 This is how... (3 Replies)
Discussion started by: chaandana
3 Replies

9. Shell Programming and Scripting

How to screen each variable record for right # of fields

Hi, In a script I have to check that input text files with a variable number of tab delimited fields have at least n fields and no more than m fields. Records are delimited by <CR> and <LF>. I have figured out code that will strip out all the alpha-numeric characters, convert the tabs to... (3 Replies)
Discussion started by: jvander
3 Replies
Login or Register to Ask a Question