To add a pipe in a file

Hi All,

how to add a pipe "|" in name field in my .dat file, below is the sample file.
name field is not case sensitive, it is mixed with upper and lower characters.

EMPLID|NAME |STID |STATUS |HRID |OIN(COLUMN NAME)
001123456One,Test|2027062|A|HIR|NPS|
001123457Expat,Two|6027062|A|HIR|NPS|
001167812shute,Clifford W|9421622|R|RET|PEN|
001203156Edmondson,Gwendolyn E|4303928|R|RET|
001224571calder,Jeanne Y|4418327|R|RET|FAP|

Expeted output like below.

001123456|One,Test|2027062|A|HIR|NPS|
001123457|Expat,Two|6027062|A|HIR|NPS|
001167812|shute,Clifford W|9421622|R|RET|PEN|
001203156|Edmondson,Gwendolyn E|4303928|R|RET|
001224571|calder,Jeanne Y|4418327|R|RET|FAP|

could any on suggest me. how to do.

Thanks,
Krupa

Is the EMPLID a fixed size field? Always 9 characters?

yes, it is always 9 characters .

but it would be better, if put beginning of the NAME column.

Thanks,
Krupa

Try this:

awk -F\| '$1=substr($1,1,9) FS substr($1,10)' OFS=\| file

Or with sed:

sed 's/\(.........\)\(.*\)/\1|\2/' file
sed 's/\([0-9]*\)\(.*\)/\1|\2/' file

Assuming name field always starts with a character:

sed 's#[a-zA-Z]#|&#' file

Does your file have that header, or not?

This should work in either case, although it may require GNU sed:

sed -E 's/^[[:digit:]]+/&|/' file

Also, note that sed won't change the input file - you'll either need to specify inplace ( -i on GNU sed), or redirect the output to a temp file.

Thanks a lot
could you explain this command?

[a-zA-Z] matches first occurrence of any alphabet and & corresponds to the pattern found, so |& means replace with pipe followed by the pattern found.