how to replace null value(field) with word on CSV file - sed

I have a comma separated CSV file looks like:
customer1,customer2,,customer4,
,customer2,,customer4,
custome1,,customer3,,
I want replace null value inside (,)with word "unknown".
How can I do that?

sed -e 's/,,/,unknown,/g'
will work except if you want to add unknown at the beginning or end of lines too.
If you also want to add something if the first is missing (line starts with ,) or the last one is missing (line ends with ,), then you could do:
sed -e 's/^,/unknown,/' -e 's/,,/,unknown,/g' -e 's/,$/,unknown/'
I'm sure there is a more elegant way, but that works.

Related

Delete line from file if matches regular expression

I am attempting to delete a line from a text file if it matches a regular expression. To accomplish this I was using sed in an Ubuntu environment combined with regular expressions. I have tried/referenced the following solutions: Sol1, Sol2, Sol3.
My current command is: sed '/[^"]+},/d' test.json with this command I am attempting to match and remove lines like:
{"hello},
{"go penguins!},
{"someone help1),
I am NOT trying to match or remove lines like: "should not match regex"}, Any line that ends with "}, should not be deleted.
I am not tied to using sed so any acceptable answer would work so long as my text file would look something like:
...
{"omg this is amazing"},
{"thanks for your help"},
{"no problem"},
...
How about sed '/\"},/!d' test.json?
It should by sed '/\"},/d' test.json (without !)

Why won't the tab be inserted on the first added line?

I am trying to add multiple lines to a file, all with a leading a tab. The lines should be inserted on the first line after matching a string.
Assume a file with only one line, called "my-file.txt" as follows:
foo
I have tried the following sed command:
sed "/^foo\$/a \tinsert1\n\tinsert2" my-file.txt
This produces the following output:
foo
tinsert1
insert2
Notice how the the tab that should be on the first (inserted) line is omitted. Instead it prints an extra leading 't'.
Why? And how can I change my command to print the tab on the first line, as expected?
With GNU sed:
sed '/^foo$/a \\tinsert1\n\tinsert2' file
<---- single quotes! --->
Produces:
foo
insert1
insert2
From the manual:
a \
text Append text, which has each embedded newline preceded by a backslash.
Since the text to be append itself has to to be preceded by a backslash, it needs to be \\t at the beginning.
PS: If you need to use double quotes around the sed command because you want to inject shell variables, you need to escape the \ which precedes the text to be appended:
ins1="foo"
ins2="bar"
sed "/^foo\$/a \\\t${ins1}\n\t${ins2}" file
sed is for doing s/old/new on individual strings, that is all. Just use awk:
$ awk '{print} $0=="foo"{print "\tinsert1\n\tinsert2"}' file
foo
insert1
insert2
The above will work using any awk in any shell on every UNIX box and is trivial to modify to do anything else you might want to do in future.

how to find a specific character combination and add a newline

I have a large file that looks like this
(something,something1,something2),(something,something1,something2)
how do I use sed and find ),( and replace it with );( or add a newline between the parentheses that has a comma character.
I did try sed 's/),(/),\n(/g' filename.txt but for some reason it does not work
for those who come here and want to know how this work without getting a lot of stackoverflow "greetings"
since I was on Mac os x you need to replace your \n with \'$'\n''
so to find ),( and add a new line between the parentheses this is the command I used
sed 's/;/\'$'\n''/g' testdone.txt > testdone2.txt
ES
echo "(something,something1,something2),(something,something1,something2)" | sed "s|),(|);(|"
This prints the below for me.
(something,something1,something2);(something,something1,something2)
For new line
echo "(something,something1,something2),(something,something1,something2)" | sed "s|),(|)\n(|"
And the above prints the below.
(something,something1,something2)
(something,something1,something2)

How to insert sth with sed just once

I'm trying to substitute the first empty line in my input file with a multiline block, i. e. out of
one
two
three
four
five
six
I want to create
one
two
foo
three
four
five
six
For this I tried this sed script:
sed '/^$/i\
\
foo'
But it inserts at /each/ empty line.
How can I tweak this call to sed so that it inserts just at the first occurrence of an empty line? Is there a way to tell sed that now the rest of the input should just be copied from to the output?
I do not want to switch to awk or other shell tools like read in a loop or similar. I'm just interested in the use of sed for this task.
You can loop and print lines until the end of the file:
sed '/^$/{i\
\
foo
:a;n;ba}' file
I found a way by replacing the i with a s command:
sed '0,/^$/s//\
foo\
/'
But I would prefer a solution using the i command because not everything I could want to do after the search might be easily replaceable with an s.

how to find a pattern in file entry, remove leading hash and redirect all entries to file

Looking for the syntax to find a pattern in a file and remove the leading character from only that pattern.
For example, find -16 and remove the # and save it to file.
Tried grep 12345-16 testfile2 | sed -e "s/^#//g" which works but need to capture all entries into the input file.
Example:
From this:
something here 12345-14
something here 12345-15
# something here 12345-16
to this:
something here 12345-14
something here 12345-15
something here 12345-16
suggestions would be much appreciated.
You can do it with just sed alone.
sed '/12345-16/s/^# *//' file
You can use -i option of sed to make in-file changes. /../ in front of sed is a regex which only makes changes on lines that has that pattern. All remaining lines will not be touched and be printed out as is.
You don't need g for global here since you are only removing the leading #. I have added a pattern of ^# * which means # or # followed by spaces at the start of the line. You can create your own pattern based on the structure of your file.