Search and delete a row which is delimited by |^

I have a sample text file like this.

CampaignId|^CampaignCd|^InsertionOrderCd|^OwningAdvertiserCd|^CampaignName
998201|^T15-06|^T15|^|^GTA 160x160
998277|^T15-07|^T15|^TEST|^GTA 160x160
998297|^T15-07|^T15|^TEST2|^GTA 160x160

I want to delete the line only when the 4th field is empty. Here the delimiter is |^. Here 2nd line is to be deleted. First line is the heading. One condition here but, if the 3rd column is empty it shouldn't delete that line. Deletion should happen only if the 4th column is empty. I tried this which is not solving the real purpose.

sed -i '/|^|^/d' <your file>

--- Post updated at 01:34 AM ---

tried this myself. To an extent its working. Any other methods?

awk -F "^"  '$4!="|"' test.txt

Hello Tuxidow,

Could you please try following.

awk -F'\\|\\^' '$3==""{next} $4'  Input_file

NOTE: After seeing Rudi sir's comment edited this solution now.

Thanks,
R. Singh

1 Like

None of the solutions so far covers your other condition "if the 3rd column is empty it shouldn't delete that line". Adapt your own solution like

awk -F "^" '$3=="|" || $4!="|"' file

Note that "ignoring" the real field separator may lead to false results (e.g. a "^" within one of the fields).

1 Like