0

Trying to print array element and other stuff but awk doesnt print array element if something going after that


awk -F, 'FNR==NR{tag[$1]=$2; next} ($1 in tag) {print tag[$1], $0}' OFS=, tags.csv entries.csv

,638000,1670886539191,3,50,30

If i ask to print element in the end it prints well


awk -F, 'FNR==NR{tag[$1]=$2; next} ($1 in tag) {print $0,tag[$1]}' OFS=, tags.csv entries.csv

result:

830000,1670255312883,3,49,31,5

Also tried printf but no differ. Using GNU awk P.S. I need to put it in one line, not two separate

Ivan
  • 31
  • 4
  • please update the question to include samples from both input files (`tags.csv`, `entries.csv`) as well as the (correct) expected output, making sure the output corresponds to the sample inputs – markp-fuso Dec 24 '22 at 18:15
  • you've provided 2 different sets of output that (to me) don't appear to be from the same set of inputs; eg, `638000 / 50 / 30 / 167088...` are in 1st set of outputs while 2nd set has a different set of numbers - `830000 / 49 / 31 / 167025...`; please confirm both sets of output – markp-fuso Dec 24 '22 at 18:19
  • the fact the 1st set of output starts with a comma would seem to indicate that one (or both) of your input files may contain windows/dos line endings (`\r\n`); you can verify this with `head -2 filename | od -c` and look for a line ending of `\r \n`; if you see the `\r` character you have a couple options ... `dos2unix filename` to permanently remove the `\r` characters from the file ... or have `awk` strip off the `\r` (eg, `awk -F, '{gsub(/\r/,""} FNR==NR ... rest_of_current_script`) – markp-fuso Dec 24 '22 at 18:39
  • Thanks, you are right, problem was \r in tags.csv, some time ago i trapped into this problem, solved with sed that time. File is the output from sqlite3, why it adds \r\n but not \n?.. – Ivan Dec 24 '22 at 18:41
  • sure, additional options for removing `\r` ... `sed` ... `tr` .... `perl` ... whatever you're comfortable with :-) – markp-fuso Dec 24 '22 at 18:43
  • I can't speak to why `sqlite3` generates the `\r` unless (of course?) it's based on the OS your `sqlite3` was compiled for ... ? from the [download page](https://www.sqlite.org/download.html) it looks like there are different versions of `sqlite3` based on the underlying OS ... could it be some versions (eg, Windows) generate the additional `\r` while other versions (eg, Linux) don't generate the `\r`? ... *shrug* – markp-fuso Dec 24 '22 at 18:48
  • I use preinstalled sqlite3 on Android AOSP rom, i ll try to get it from termux – Ivan Dec 24 '22 at 19:11

0 Answers0