0

I am pretty new to awk, and I am trying to modify an existing script, here is my requirement - from command line provide a string parameter like - "abcd|efgh|ijkl" (I am able to do this using -v) and the split it to have a array of strings like - {"abcd","efgh","ijkl"). Read values from a file and if a particular string matches one of the values in the split array(the value will be a key-val pair ex - abcd:789), then replace it and write it in the output file.(in output file i need to have "abcd 123") for other lines if the key doesn't matches any value in my array,(for example "xyz:456") I want to print the actual value ("xyz 456") here is how I am getting the split array -

awk -v mystring="$5" 'BEGIN {
  print mystring ; //prints abcd|efgh|ijkl
  split(mystring,mylistDictionary,"|"); //splitting
  for (i in mylistDictionary) uniqueKeys[mylistDictionary[i]] = "" //* currently unused
  print "1-",mylistDictionary[1]; // prints abcd
  print "2-",mylistDictionary[2]; // prints efgh
}
.
.
.
function writeLine(line) {
  split(line,lineTokens,"="); // the line will be something like abcd = ewiubqq
  for(x in mylistDictionary) {if(x==lineTokens[1]){ tmp="123"; print lineTokens[1],tmp; } };
  print(line)
 }

I am able to print the value of lineTokens[1] inside the for loop , and I am also able to print all values of the array (if i replace the if block with print x; but, when I am trying to compare the two values if(x==lineTokens[1]) , the condition is never true and I am unable to change the value of tmp .

i understand that awk arrays are associative, and so based on some answers tried to convert it into an array with the values as keys,which is the line with the * in the comment , but using that also doesn't works.

Sample input -

key1 = value1
key2 = value2
key3 = value3
key1 = value4

command line arg- "key1|key2|key8"

Expected output-

key1 = 123
key2 = 123
key3 = value3
key1 = 123
soumitra goswami
  • 818
  • 6
  • 29
  • 1
    The first `mylistDictionary[]` value is `'abcd`' (no trailing spaces). You've stated the input line looks like `abcd = ewiubqq` and that you're splitting said line based on `=` as the delimiter, which is going to place `'abcd'` in the first element of the `lineTokens[]` array; net result is that `'abcd' != 'abcd'`; also, in your test (`x==lineTokens[1]`) you're comparing a number (`x`) to a string (`lineTokens[1]`) when I'm assuming you really want to compare 2 strings, ie, `(mylistDictionary[x]==lineTokens[1])` ... ?? – markp-fuso Aug 13 '20 at 17:54
  • yes, but when I do print x ; inside the loop, it prints the values, so I assumed i can directly use x, the trailing spaces could be an issue though, will modify the input file to remove spaces and then test once,is there any way i can trim the spaces for lineTokens[1] ? – soumitra goswami Aug 13 '20 at 17:58
  • 1
    take a look at `awk/gsub()` for trimming whitespace; a google search on `awk trim whitespace` should generate a lot of hits (eg, [trim leading/trailing spaces in awk](https://stackoverflow.com/a/20601021/7366100)); I would also suggest updating the question to include a) a sample input file (so we can see the actual format of said data) and b) sample `awk` code that's calling the `writeLine()` function – markp-fuso Aug 13 '20 at 18:10
  • 1
    As above, but please also include the required output from your sample input. Also, are you bared from using the "match" operator, ie `'abcd' ~ 'abcd'` should return true, but the space is still there and may mess up other stuff for you. `sub(/ *$/,"",$1)` will trim trailing space chars from field `$1`. Good luck. – shellter Aug 13 '20 at 18:41
  • added sample input and output – soumitra goswami Aug 13 '20 at 19:03
  • @shellter I wasn't aware of the pattern match operator, so thanks for suggesting that, it is working fine now, as I have added the condition like -`if(lineTokens[1]~mylistDictionary[x]) and the space doesn't needs to be removed, so this is good enough for my use case. – soumitra goswami Aug 13 '20 at 19:36
  • 1
    Glad that helped and GOOD SHOW! for adding your inputs and expected output! Be sure to include that info on your next Q and hopefully the drive-by downvoter will leave you alone ;-)! . Good luck. – shellter Aug 13 '20 at 19:41

0 Answers0