Filtering out required records from a flat file and merging multiple records into one output record

elinzeyjr

New member
Joined
Oct 31, 2022
Messages
1
Programming Experience
10+
Hope Everyone is doing fine.

I'm still new to C# coding and was hoping someone may have already had a similar requirement and could point me in the right direction. I had originally posted this question in an SSIS forum but am now thinking it may make more sense to and see if this could be resolved using C# code.

Requirements: Read in a text file that has multiple record types. Each time a record starting with the characters 'J9' is found, take that record and the 4 records that follow, merge them into one output record to be written out to a new text file. All output records would be written to the same output file (1 file in, and 1 file out).


Input Data Sample:
C#:
Header
KC FLDA FLDB FLDC
FLDA FLDB FLDC

J9 FLDA FLDB FLDC
FLDD FLDE FLDF
FLDG FLDH FLDI
FLDJ FLDK FLDL
FLDM FLDN FLDO
KC FLDA FLDB FLDC
FLDA FLDB FLDC
J9 FLDA FLDB FLDC
FLDD FLDE FLDF
FLDG FLDH FLDI
FLDJ FLDK FLDL
FLDM FLDN FLDO
Desired Ouput:
C#:
J9 FLDA FLDB FLDC FLDD FLDE FLDF FLDG FLDH FLDI FLDJ FLDK FLDL FLDM FLDN FLDO
J9 FLDA FLDB FLDC FLDD FLDE FLDF FLDG FLDH FLDI FLDJ FLDK FLDL FLDM FLDN FLDO

Any feedback is appreciated.

Thank you
 
Last edited:
In general, code in C# is called "code", not "script", and therefore the act of writing C# code is called "coding" or "programming", not "scripting". The only time you would call it "scripting" is if you are writing code for Unity where they prefer to call C# code "script".
 
Anyway, regarding the problem you posted, what have you thought about and considered? What specific problem are you running into? Please share your code.

We are not a code writing service. We can guide you towards a solution if you take time to share your code and explain what specific issue you are running into.
 
Here is how you approach any programming problem as a newbie

It's how professional developers approach hard problems; as a newbie every problem you face is a hard problem because you don't think in C#. You think in English so write in English first

Write comments, in English, then write c# underneath them. Do not erase the comments after. Do not write comments after, because that tends to make newbies write junk like this:

C#:
//make a variable called lineNum and set it to 1
var lineNum = 1;

That comment is a longer, useless version of the c# below it

Had the comment been written before, it would probably have done a better job of explaining the algorithm:

C#:
//establish a variable that will keep track of the line number
var lineNum = 1;

So, algorithm first. here is an example algorithm to solve your problem:

C#:
//read the lines of the file into an array, one line per array entry, so we can have easy random access to them

//loop over the array, but using a  for loop rather than a foreach because we want to use the loop counter to pull up to 4 successive lines (random access)

  //check if the line starts with J9
    //if it does make a string out of the current line and the 4 following it
    //append this string to the output file
    //bump the loop counter up by 4 so we don't examine the lines we've just written

Here are a selection of things that will help you reach your goal:

C#:
//reading a file to an array
var lines = File.ReadAllLines(...);

//looping an array, stopping 4 lines short 
for(int x = 0; x < lines.Length - 4; x++)

//checking a string starts with another string
lines[x].StartsWith("J9")

//appending a string onto the end of a file (if the file doesn't exist it is created)
File.AppendAllText(..., ...);
 

Latest posts

Back
Top Bottom