Deano
Deano

Reputation: 12190

sed replace empty space with NULL

Using sed

  1. how can I replace \N with NULL

  2. How can I replace the empty space || with NULL.

    |200|0||0|\N||^A|0|\N||
    

desired output

    |200|0|NULL|0|NULL|NULL|^A|0|NULL|NULL|

Upvotes: 4

Views: 10730

Answers (5)

Thor
Thor

Reputation: 47099

You need a slightly modified version of this. Something like this works with GNU sed:

sed ':a; s:|\(\\N\)\?|:|NULL|:g; ta'

Or more portable:

sed -e ':a' -e 's:|\(\\N\)\?|:|NULL|:g' -e 'ta'

Upvotes: 4

Anew
Anew

Reputation: 5492

<file sed 's/\\N/NULL/g' | awk -F\| '{for(i=2;i<=NF;i++){ if($i==""){ printf "|NULL"} else{  printf "|%s", $i}}; printf "|\n"}'

Upvotes: 0

hany heggy
hany heggy

Reputation: 7

sed -i 's/ /NULL/g' temp.txt

This will replace any space character with NULL.

Upvotes: -2

Mirage
Mirage

Reputation: 31548

with awk

awk 'gsub("\\\\N","NULL");gsub("\\|\\|","|NULL|")' temp.txt

Upvotes: 0

mikyra
mikyra

Reputation: 10367

I think the command you are looking for might be called "tr". Try

tr \\n \\0

Upvotes: 1

Related Questions