Replacing a column in a pipe delimited file

Hi,

I have a pipe delimited file as below and I need to replace the 2nd column of each line with null values.

1|10/15/2011|fname1|lname1
2|10/15/2012|fname2|lname2
3|10/15/2013|fname3|lname3

Output file:

1||fname1|lname1
2||fname2|lname2
3||fname3|lname3

I tried this

awk -F"|" -v OFS="|" '{ $2=""}' filename.txt

Can anyone please help me with this urgent requirement?

Thanks in advance.

---------- Post updated at 10:36 AM ---------- Previous update was at 10:31 AM ----------

Was a syntax error. This worked.

awk -F"|" -v OFS="|" '$2 { $2=""} 1' filename.txt > newfilename.txt

Use man sed (linux):

sed -e 's/|[^|]*|/|/'

---------- Post updated at 02:12 PM ---------- Previous update was at 01:58 PM ----------

Or man cut (linux):

cut -d\| -f1,3-
1 Like

Thank you Derek. This is helpful.