0

I have a test file with following content:

k1:v1

I'd like to get v1 and concatenate with a string a using AWK command below:

cat test.txt | grep k1 | awk -F: '{print $2"a"}'

Expected the result is v1a, but I got a1. If I concatenate with aa, then I got aa. Seems like it overwrites the string v1 from beginning. I have been debugging for a long time, I cannot see any reason to cause this.

ZhengguanLi
  • 113
  • 1
  • 2
  • 8

1 Answers1

1

Based on your shown samples, could you please try following. We could do it in a single awk itself we need not to use grep and cat with it. Simply make field separator as : and check condition if line starts from K1 then print 2nd field along with string a then.

awk -F':' '/^k1/{print $2"a"}' Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Check with `cat -v Input_file` in case you have control M characters in your file, if yes; then try `awk -F':' '{gsub(/\r/,"")} /^k1/{print $2"a"}' Input_file` to remove them and then Process awk command further. – RavinderSingh13 Jan 19 '21 at 09:11