Support Questions

Find answers, ask questions, and share your expertise

ReplaceText Processor edit only the header of csv file

avatar
Contributor

Hello,

The header of my csv file contains some special character. Example

Name,T&C_Test,Date_of_(Transformation)

And I want to use the processor Replace Text to edit only the header. When I use the Append Stratesy for the entire text, it dosen't work.

Help plz. Thank you

6 REPLIES 6

avatar
Super Collaborator

You can use a regular expression to isolate the header line (for example search the entire content, start at the beginning using "^", and stop on the first newline) and replace it with what you'd like to have. You'l have to test out different regex strings to see what works for you but this should get you started.

avatar
Contributor

I tried with ^ at the beginning of the expression. No results 😕

avatar
Super Collaborator

Please take a look at the following HCC thread:
https://community.hortonworks.com/questions/35203/hi-where-can-i-find-the-rules-of-nifis-regex-langu...

That thread also links to this website, which you can use to create your expressions and directly copy them to NiFi. You may not be using the correct syntax if it isn't working, so please verify with this website:

https://regexr.com/

avatar
Master Guru
@Amira khalifa

As suggested by @anarasimham With Start of the string(^) in Replace text processor should match only the first line.

Make sure you are having matching regex that exclude special character.

Example:

77827-replacetext.png

in the above configs i'm matching only the first line in the flowfile and adding new to the first line only and all the other contents will be untouched.

input:

hi
hello

Output:

newhi
hello

(or)

You can use one of the way that i have suggested in this link, please refer to the shared link and choose the method that will best fit for your case.

If you are still having issues please share some sample data with header and the expected output?

avatar
Contributor

@Shu Thank you for your response when I tried this regex expression :

^[!@#$%^&*(),.?":{}|<>]

It changed only the first occurance of a special character.

I have in input this csv file :

Name,T&C_Test,Date_of_(Transformation),date_/_of _deliverance

1,1457,26/12/2016,14/12/2017

2,4789,18/04/2017,03/09/2018

I expect an output file like this one:

Name,TC_Test,Date_of_Transformation,date_of _deliverance

1,1457,26/12/2016,14/12/2017

2,4789,18/04/2017,03/09/2018

avatar
Master Guru
@Amira khalifa

Use one of the way from the above shared link to take out only the header from the csv file then in replace text keep the

78567-replacetext.png

then search for (&|\(|\)|\/_|\s) and in Replacement value keep as empty string, now we are searching for all the special characters in the header flowfile then replacing with empty string.Now add this header flowfile with the other non header flowfile. all the explanation and template.xml are shared in this link.