i have a file with below content in linux:
servername: tesing1001
os: Unix
bu:
aid: 132
location: anywhere
environment: dev-stg
application: ABCD testing space
city: kk
I want to replace content in same file with below data:
servername: tesing1001
os: Unix
**bu: BLANK** **>>>>here since value is empty i will set it as BLANK hardcoded**
aid: 132
location: anywhere
environment: dev-stg
**application: ABCD_testing_space** **>>>>>here we will replace string space with "_"**
city: kk
all this thing we will do in same file . So, far i am trying to acheive above output with below logic
#!/bin/bash
cp -p /opsunix/dyfacter.txt /tmp/customized.txt.tmp
awk -F ":" '{
if ($2 == "")
{
print $0 " blank"
} else {
print $0
}
}' /opsunix/dyfacter.txt > /tmp/customized.txt.tmp && mv /tmp/customized.txt.tmp /opsunix/dyfacter.txt
with the help of above code, am able to identify null value and replace it with "blank" string.
servername: tesing1001
os: Unix
**bu: blank** **>>>>done**
aid: 132
location: anywhere
environment: dev-stg
**application: ABCD testing space** **>>>>>still need to correct**
city: kk
however 2nd last line i.e: application: ABCD testing space am not able to convert it as application: ABCD_testing_space.
appliying sed -i 's/ /_/g' /opsunix/dyfacter.txt
is replacing all spaces in file after : .
purpose is to replace space in string .
Please help!
CodePudding user response:
Using sed
, this will change all null values to BLANK
and strings with consecutive spaces between the values to change the space to _
after the delimiter :
$ sed -E ':a;s/^([^ ]*: )([^ ]*) /\1\2_/;ta;s/: ?$/: BLANK/' input_file
servername: tesing1001
os: Unix
bu: BLANK
aid: 132
location: anywhere
environment: dev-stg
application: ABCD_testing_space
city: kk
CodePudding user response:
Maybe this will help you. I replace all spaces with an _ After that i replace all ":_" with ": "
foo=${str// /_}
echo $foo
echo ${foo/:_/: }
CodePudding user response:
Using awk (gawk in particular):
awk -F ': ' '/^application/ { gsub(" ","_",$2) } /^bu/ { gsub("","BLANK",$2)}1' file
Set the field delimiter to ": " and then where the line starts with "application", use awks, gsub function to replace all instances of " " with "_" in the second field. With lines starting with bu, replace "" with "BLANK" in the second field
This will output the amendments to the screen, to write the data back to the file, use a temporary file and so:
awk -F ': ' '/^application/ { gsub(" ","_",$2) } /^bu/ { gsub("","BLANK",$2)}1' file > file.tmp && cp -f file.tmp file
CodePudding user response:
Using awk you could set the FS (Field Separator) to a colon followed by optional spaces, and the OFS (Ouput Field Separator) to :
Using a pattern, you could match if the line starts with non whitespace chars followed by :
and then optional spaces only.
If that is the case, print out the first field with the OFS and "BLANK" and move to the next line as there is nothing to substitute on this line.
For all the other lines, replace " "
with _
awk '
BEGIN{
FS=":[[:space:]]*";
OFS=": "
}
/^[^[:space:]]*:[[:space:]]*$/{
print $1 OFS "BLANK"
next
}
{
gsub(" ","_", $2)
}1
' file
Output
servername: tesing1001
os: Unix
bu: BLANK
aid: 132
location: anywhere
environment: dev-stg
application: ABCD_testing_space
city: kk
With gnu awk you might also match the whole line using a pattern and 2 capture groups, and do the replacement based on the value of capture group 2.
awk '
match($0, /^([^[:blank:]] :[[:blank:]]*)(. )?$/, a) {
if (a[2] != "") {
print a[1] gensub(" ", "_", "g", a[2])
next
}
print a[1] "BLANK"
}
' file