0

This might look simple, But I could not get it work. I have a property file and contains like below.

AVAIL_COLS=col1,col2,col3,col4,col5
SC_AVAIL_COLS=col1,col2,col3

In my bash script, I do some processing and modifying available cols property and trying to replace it in the file. Like below.

propfile=<my property file>
line_avail_cols="AVAIL_COLS="
line_sc_avail_cols="SC_AVAIL_COLS="
available_cols=$(grep $line_avail_cols $propfile | grep -v $line_sc_avail_cols)

# Doing some operations to add few more values to same row.
available_cols="$available_cols,col6,col7,col8"

#Replace the line with new content
sed -i '/'"$line_avail_cols"'/c\'"$available_cols" $propfile

The above script works fine if my property file has only AVAIL_COLS property and not SC_AVAIL_COLS. But if the property file contains SC_AVAIL_COLS property, then instead of replacing the line, it adds as a new line in the file making duplicate entry in the file.

How to replace the AVAIL_COLS property line directly? The line number or the order of properties in file may vary. So I was looking for a generic way.

Expected OUTPUT

AVAIL_COLS=col1,col2,col3,col4,col5,col6,col7,col8
SC_AVAIL_COLS=col1,col2,col3
  • Your grep pipe looks odd to me. How does the complete line you are interested in, look like? Wouldn't a single `grep "^ *$line_avail_cols" "$propfile"` be sufficient? Or maybe a `grep -wF "$line_avail_cols" "$propfile"`? – user1934428 Dec 14 '21 at 08:12
  • Alternatively, you may consider `awk` as solution, i.e. something like `awk -F = '"AVAIL_COLS" { print($0 ",col6,col7,col8") }' "$profile" > "new.$propfile"` and thereby replace `grep` and `sed`. – user1934428 Dec 14 '21 at 08:20
  • @user1934428 Initially I was doing only `$(grep $line_avail_cols $propfile)` and got both lines due to same matching. And google gave me this command :D not much of script guy –  Dec 14 '21 at 08:28
  • This can't work of course. You forgot the `^`, or the `-w` (depending on your preferences). See the suggestions I gave in my comment. – user1934428 Dec 14 '21 at 08:49
  • @user1934428 that grep worked for me anyway. Got from this answer https://stackoverflow.com/a/6064412/2353403 –  Dec 14 '21 at 09:07
  • It works only if you follow it by a `grep -v`, but this is unnecessarily complicated. – user1934428 Dec 14 '21 at 09:12

2 Answers2

2

You can fix your script like this:

propfile="$1"
line_avail_cols="AVAIL_COLS="
line_sc_avail_cols="SC_AVAIL_COLS="
available_cols="$(grep "^$line_avail_cols" $propfile)"

# Doing some operations to add few more values to same row.
available_cols="$available_cols,col6,col7,col8"

#Replace the line with new content
sed -i "s/^$line_avail_cols.*/$available_cols/" "$propfile"

However all this can be done in a single awk command also like this:

awk -v val=',col6,col7,col8' -F= '$1 == "AVAIL_COLS" {
$0 = $0 val} 1' file

AVAIL_COLS=col1,col2,col3,col4,col5,col6,col7,col8
SC_AVAIL_COLS=col1,col2,col3
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Worked. Did not try awk as I didn't want to modify my other lines. –  Dec 14 '21 at 09:08
0

You need to differentiate between the two lines, because the second line matches too in your condition.

Try including ^ for beginning of line like this:

sed -i '/^'"$line_avail_cols"'/c\'"$available_cols" $propfile
D-FENS
  • 1,438
  • 8
  • 21