-1

I have the file like this. ex.(test.txt)

$$BATCHCTRL=TEST-012017
$$STATE=CA AZ
$$FROM_DATE=01/10/2017
$$TO_DATE=01/30/2017

All I need to do is replace this $$STATE=CA AZ with first TWO bytes of this value. i.e(CA).

The output file should be

$$BATCHCTRL=TEST-012017
$$STATE=CA
$$FROM_DATE=01/10/2017
$$TO_DATE=01/30/2017
steeldriver
  • 81,074
  • 2
    Hi! What have you tried this far, and where in that process are you stuck? –  Jan 19 '19 at 15:50

2 Answers2

3

I am assuming that these couples of character after STATE are in capital. If not then you should replace [A-Z] [A-Za-z].

You can use this simple command:

sed -Ei 's/^\$\$STATE=([A-Z]{2}) ([A-Z]{2})/\$\$STATE=\1/g' sed_file

It will match with lines starting with $$ like $$STATE=AB CD and will replace them with $$STATE and first subexpression \1.

Edit: If you want an extra single quote then use:

sed -Ei 's/^\$\$STATE=([A-Z]{2}) ([A-Z]{2})/\$\$STATE='"'"'\1'"'"'/g' sed_file
Prvt_Yadav
  • 5,882
1
awk 'BEGIN { FS=OFS="=" } $1 == "$$STATE" { split($2,a," "); $2 = "'"'"'" a[1] "'"'"'" } 1' file

This uses awk to parse the file as a collection of =-delimited fields. If the first field is the exact string $$STATE, then the second field is split on spaces and assigned the value of its first space-delimited bit, with single quotes around it.

The funky looking "'"'"'" is a double quoted single quote character within a single quoted awk script.

The result would be

$$BATCHCTRL=TEST-012017
$$STATE='CA'
$$FROM_DATE=01/10/2017
$$TO_DATE=01/30/2017

Alternatively, with sed:

sed -E '/^\$\$STATE=/s/=([^ ]*).*/='"'"'\1'"'"'/' file

This would locate any line that starts with the string $$STATE= and then replace anything after the = with the first bit before a space, again with single quotes inserted.

Kusalananda
  • 333,661