This question already has answers here :
Could you please try following if ok with awk
.
awk '{for(i=1;i<=NF;i++){if($i==0){$i="Not Exist"}}}{$1=$1} 1' OFS="\t" Input_file
Adding a non-one liner form of solution too now.
awk '
{
for(i=1;i<=NF;i++){
if($i==0){
$i="Not Exist"
}
}
}
{
$1=$1
}
1
' OFS="\t" Input_file
Explanation: Adding explanation for above code too now.
awk '
{
for(i=1;i<=NF;i++){ ##Starting for loop from variable i=1 to value of NF(number of field) increment with 1 each time.
if($i==0){ ##Checking condition if value of field is 0 then do following.
$i="Not Exist" ##Re-making value of that field to string Not Exist now.
} ##Closing if condition block now.
} ##Closing for loop block here.
}
{
$1=$1 ##re-setting first field on current line(to make sure TAB is being made output field separator to edited lines).
}
1 ##Mentioning 1 means awk works on method on pattern and action. So making condition/pattern as TRUE and not mentioning any action so by default print of current line will happen.
' OFS="\t" Input_file ##Setting OFS as TAB and mentioning Input_file name here.
sed, How do you replace a variable in a file using sed? You can also use regular expressions. For example to search all 3 digit numbers and replace them with the string number you would use: sed -i 's/\b[0-9]\{3\}\b/number/g' file.txt number Foo foo foo foo /bin/bash demo foobar number Another useful feature of sed is that you can use the ampersand character & which corresponds to the matched pattern. The character can be used multiple times.
Here's why your three attempts so far don't work:
sed 's/[0]/Not Exist/g' data.txt > out.txt
This asks sed to replace any zero character with the replacement string, including those that are part of a larger number.
sed 's/[^0]/Not Exist/g' data.txt > out.txt
This asks sed to replace any character which is NOT zero with the replacement string. The ^
"negates" the regex bracket expression.
sed 's/^[0]/Not Exist/g' data.txt > out.txt
This asks sed to replace any zero that is at the beginning of the line, since the ^
means "the null at the beginning of the line" in this context.
What you're looking for is might be expressed as follows:
sed 's/\([[:space:]]\)0\([[:space:]]\)/\1Not exist\2/g; s/\([[:space:]]\)0$/\1Not exist/' data.txt > out.txt
In this solution I'm using the space
character class since I don't know whether your input file is tab or space separated. The class works with both, and retains whatever was there before.
Note that there are two sed commands here -- the first processes zeros that are have text after them, and the second processes zeros that at are the end of the line. This does make the script a bit awkward, so if you're on a more modern operating system with a sed
that includes a -E
option, the following might be easier to read:
sed -E 's/([[:space:]])0([[:space:]]|$)/\1Not exist\2/g' data.txt > out.txt
This takes advantage of the fact that in ERE, an "atom" can have multiple "branches", separated by an or bar (|
). For more on this, man re_format
.
Note that sed is probably not the best tool for this. Processing fields is usually best done with awk. I can't improve on @RavinderSingh13's awk solution, so you should use that if awk is an option.
Of course, your formatting is going to be wonky with almost any option.
replace with value of variable using SED, How do I find and replace multiple files in Linux? I am trying to find out how to replace a pattern a certain number of times using sed. I found that sed with g flag replaces all occurences, w/o 1 time, with specified number replace this occurence, g3 greater/equal 3.
I assume the columns are separated by white-space characters, then:
When using sed, you need to search for a lonely zero, that is zero "enclosed" in spaces. So you need to check the char after and before zero if it is equal to space. Also you need to handle the first zero and the last zero on the line separately.
sed '
# replace 0 beeing the first character on the line
s/^0\([[:space:]]\)/Not Exists\1/
# replace zeros separated by spaces
s/\([[:space:]]\)0\([[:space:]]\)/\1Not Exists\2/g
# replace the last 0
s/\([[:space:]]\)0&/\1Not Exists/ ' data.txt > out.txt
Live example at tutorialpoint .
Sed Command in Linux/Unix with examples, How do you replace a string in Unix without opening the file? This asks sed to replace any zero character with the replacement string, including those that are part of a larger number. sed 's/[^0]/Not Exist/g' data.txt > out.txt This asks sed to replace any character which is NOT zero with the replacement string. The ^ "negates" the regex bracket expression. sed 's/^[0]/Not Exist/g' data.txt > out.txt
Using sed:
sed 's/\<0\>/NotExist/g' file | column -t
\<...\>
matches a word.
column -t
display in column nicely.
How to use sed to replace numbers with parenthese?, How do you like this one? I hope it is what you needed. sed -e 's/\([0-9]\+\)/(\1, 0)/g'. Test echo "hello:123: world hello:783: world hello:479: world" | sed -e Find and Replace the Specific Occurrence of the Pattern on a Line By default, the sed command replaces only the first occurrence of a pattern in each line. You can also replace the first, second and Nth occurrence of a pattern in each line.
How to Use sed to Find and Replace String in Files, With sed you can search, find and replace, insert, and delete strings and lines. all 3 digit numbers and replace them with the string number you would use: If you want to search and replace text only on files with specific Example 8. Interactive Find and Replace in Vim Editor. You can perform interactive find and replace using the ‘c’ flag in the substitute, which will ask for confirmation to do substitution or to skip it as explained below. In this example, Vim editor will do a global find the word ‘awesome’ and replace it with ‘wonderful’.
Using sed to replace numbers, In "modern" regexes, + has a special meaning — it means "one or more" (just like how * means "zero or more") — so to match an actual plus
Replace a block of numbers in sed, [0-9] will already match all digits, and replace every single one with # . Since + is extended syntax, you could also do: echo "fdsafdsa 32432
Comments Thank you Ravinder. This is what what I expected. Can you please let me know what last 1 stands for ? @buddhima87, glad that it helped you, try to up-vote helpful answers. Try to select an answer as correct too out of all see this stackoverflow.com/help/someone-answers Dang. We typed the same awk code, almost exactly. :) Thank Ravinder. You explained it well. Thank you @ghoti. This helped me to shape up my final output. This will also match 0
in 0.234
(if any). Whitespaces are the boundaries here. Thank you Oliv. This is working. Can you elaborate regex for me to understand. @WiktorStribiżew I didn't see any float number input data... If you think what you suggest is a solution, you should refrain from posting duplicates as it has already been answered at stackoverflow.com/questions/1032023, stackoverflow.com/questions/46676657.... Just mark as a dupe.