# How to remove the last line in flat file using Arc script ??

**URL:** <https://community.cdata.com/t/how-to-remove-the-last-line-in-flat-file-using-arc-script/994>\
**Category:** CData Arc\
**Tags:** question, cdata-arc, arcscript\
**Created:** [January 10, 2025, 2:24pm UTC](https://community.cdata.com/t/how-to-remove-the-last-line-in-flat-file-using-arc-script/994 "2025-01-10T14:24:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bala\_bk](https://avatars.discourse-cdn.com/v4/letter/b/f04885/32.png) [@bala\_bk](https://community.cdata.com/u/bala_bk)\
**Post date:** [January 10, 2025, 2:24pm UTC](https://community.cdata.com/t/how-to-remove-the-last-line-in-flat-file-using-arc-script/994/1 "2025-01-10T14:24:57Z")

</div>

How to remove the last&nbsp;line&nbsp;in flat file using Arc script ??

The below script is to add the “END” on the last line but I need to remove the END which is present in the last line of a Flat-file??

\<arc:set attr=“file.file” value=“[filepath]” /\>

\<arc:call op=“fileRead” item=“file”\>  
&nbsp; \<arc:set attr=“tmp.originalBody”\>[file.file:data]\</arc:set\>  
\</arc:call\>

\<arc:unset attr=“output.data”\>[tmp.originalBody]END\</arc:set\>  
\<arc:unset attr=“output.filename” value=“940\_[now()]” /\>  
\<arc:push item=“output” /\>

---

<div class="post-metadata">

**Author:** ![James\_B](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/james_b/32/1688_2.png) [@James\_B](https://community.cdata.com/u/James_B)\
**Post date:** [January 10, 2025, 7:30pm UTC](https://community.cdata.com/t/how-to-remove-the-last-line-in-flat-file-using-arc-script/994/2 "2025-01-10T19:30:37Z")

</div>

It’s a little difficult to tell from your question, what it is you want to achieve, but likely you’d want to use a regular expression in a regex replacement.

It sounds like you want to take a flat file with a freestanding END line:

```auto
This is a test.Second line.END

```

And move END to the end of the previous line:

```auto
This is a test.Second line.END

```

Regular expressions are powerful ways to manage pattern matching, and you can use&nbsp;[Regexr](https://regexr.com/)&nbsp;to test out new patterns. That code, combined with your existing setup, would look something like:

```xml
<arc:set attr="file.file" value="[filepath]" /><arc:call op="fileRead" item="file"> <arc:set attr="tmp.originalBody">[file.file:data]</arc:set></arc:call><arc:set attr="output.data">[tmp.originalBody | regexreplace('\s+END$','END')]</arc:set><arc:set attr="output.filename" value="940_[now()]" /><arc:push item="output" />

```

---

<div class="post-metadata">

**Author:** ![Rohit](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/rohit/32/638_2.png) [@Rohit](https://community.cdata.com/u/Rohit)\
**Post date:** [January 16, 2025, 7:14am UTC](https://community.cdata.com/t/how-to-remove-the-last-line-in-flat-file-using-arc-script/994/3 "2025-01-16T07:14:15Z")

</div>

Hi ​ @bala_bk

If you only want to remove the last line from a flat file without shifting it to the second-to-last line, you can use the following script.

In this script:

1. first count the total number of lines using the `fileReadLine` operation. I initialize a `total (total lines)`&nbsp;variable to 0 and increment it by 1 for each line during iteration.
2. In the second part, I print the data. If the index is less than the total number of lines, the corresponding data is printed.

This approach ensures the last line is excluded without affecting the rest of the file.

Here is the script:

```auto
<arc:set attr="file.file" value="[filePath]" /><arc:set attr="file.total" value="0" /><!-- Counting total number of lines present in the file --> <arc:call op="fileReadLine" in="file" out="line"> <arc:set attr="file.total" value="[file.total | add(1)]" /> </arc:call><!-- printing the data less than the size of total lines variable, in order to remove the last line --><arc:set attr="output.data"> <arc:call op="fileReadLine" in="file" out="line"> <arc:if exp="[_index] < [file.total]">[line.file:data]\n</arc:if> </arc:call></arc:set><arc:push item="output" />

```

Do let me know if it works or if you have more questions.
