awk with $var as search pattern [closed] - variables

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am facing a problem where I want awk to search for a pattern that comes from a bash variable...
Now it looks like:
for i in ${CLASS_LIST[*]}; do
cat log.txt | awk -F ":" '$1 ~ /$i/ {print substr($0, index($0,$2))}'
done
But awk has no output here.
If I do it like:
cat log.txt | awk -F ":" '$1 ~ /LABEL/ {print substr($0, index($0,$2))}
it works well... what is wrong in the first example?
Thanks a lot for your help in advance.
regards,
Joerg

Thanks for the tip, Cyrus. I've already tried the "-v" option with the same result.
But I found my mistake...
when give a variable as search pattern, don't pack it in to "/"...
This is working for me:
for i in ${CLASS_LIST[*]}; do
cat log.txt | awk -v var="$i" -F ":" '$1 ~ var {print substr($0, index($0,$2))}'
done
Regards,
Joerg

Related

awk -OFS vs OFS gives different output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a sample file:
đŸ’»~/dev/test[1]â‘‚master*$ cat test.properties
startTime: 0515
stopTime: 2015
dataFiles: foo
fixVersion: 4.2
retry: 5
kafkaRelay.type: kafkaSink
kafkaRelay.producerId: blah
kafkaRelay.partitioningTag: 49
kafkaRelay.topic: topicname-pre-transform-{0,date,yyyyMMdd}
I have a particular awk command I want to run. It gives different output when I use -F vs {BEGIN FS = ...:
đŸ’»~/dev/test[4]â‘‚master*$ awk 'BEGIN{ FS = ": *"; OFS =": " } \
$1 ~ /(startTime|fixVersion)/ {print $2, $1}; \
$1 ~ /kafkaRelay.topic/ {$1="kafkaWriter.topic";print; $1="kafkaReader.topic"; print}; \
$1 ~ /stopTime/ { $2+=100; $2%=2400; printf("%s: %04d\n", $1, $2) }' test.properties
0515: startTime
stopTime: 2115
4.2: fixVersion
kafkaWriter.topic: topicname-pre-transform-{0,date,yyyyMMdd}
kafkaReader.topic: topicname-pre-transform-{0,date,yyyyMMdd}
đŸ’»~/dev/test[5]â‘‚master*$ awk -F': *' -OFS': ' \
'$1 ~ /(startTime|fixVersion)/ {print $2, $1}; \
$1 ~ /kafkaRelay.topic/ {$1="kafkaWriter.topic";print; $1="kafkaReader.topic"; print}; \
$1 ~ /stopTime/ { $2+=100; $2%=2400; printf("%s: %04d\n", $1, $2) }' test.properties
startTime: 0515
stopTime: 2015: 0100
fixVersion: 4.2
kafkaWriter.topic
kafkaReader.topic
The first version outputs exactly as I expect. The second version has a bunch of differences and I don't understand how they come about. I also tried 1-4 \ in front of the * in the second version, hoping it was something to do with escaping the *, but that had no effect.
Why does this happen? I followed awk's regexp field splitting, and the command line field separator doesn't have any special instructions for -F vs FS =. The only StackOverflow question I could find fails to use BEGIN, which isn't my problem.
For reference:
đŸ’»~/dev/test[6]â‘‚master*$ awk --version
GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)
The problem is the use of -OFS.
POSIX guidelines for command-line parsing indicate that after a single dash, flags are parsed character by character. Thus, this means -O, and -FS -- with the -FS overriding the -F ': *' with a value of just S.
If you want to set OFS, doing it in a BEGIN block is the Right Thing.

what operations are performing in the given coding [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am having a shell script code like below:
awk 'NR==FNR{a[$0]; next} !($0 in a){print "Fail: "$0 " is not found"}' <(cat file3 <(grep -r names file2)) <(grep -r present file1)
Can someone explain in the above code what the awk is doing here..?
This is the kind of question where you can take it apart piece by piece:
do grep -r present file1 on it's own and see what it outputs
although if "file1" is truly a file and not a directory, then the -r option is useless
<(...) is a Process Substitution -- it takes the output of the script and lets you handle that as a file
Similarly, <(cat file3 <(grep -r names file2)) concatenates the contents
of "file3" and the output of the grep command.-
now, the awk script
awk 'NR==FNR {do something; next} some more awk code' fileA fileB is a very common awk idiom
NR == FNR means "the current record number (out of all files processed so far) is equal to the record number of the current file being processed" -- this can only happen for the first file in the list[1]
so, do something only for the first file, because next won't allow the "some more awk code" to be reached.
Without showing us the contents of the files, there's not much more to say. If you were to show the inputs and output, we can help you understand exactly why you see the results you see.

How to update 0.1.88 to 1.0.88 using awk? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How do I change 0.1.88 to 1.0.88 using awk functionality?
I tried below:
ori_value=0.1.88
value=echo $ori_value | awk -F'.' -v OFS=. '++$(NF-1)'
this just updates to 0.2.88
but I want 1.0.88
Please help me with this
Thank you in advance
Could you please try following, written and tested with shown samples in GNU awk.
echo "0.1.88" | awk 'BEGIN{FS=OFS="."} {print $2,$1,$3}'
OR as per OP's shown variable:
echo "$ori_value" | awk 'BEGIN{FS=OFS="."} {print $2,$1,$3}'
2nd solution: With sed could you please try following in sed with shown samples.
echo "0.1.88" | sed 's/\([^.]*\)\.\([^.]*\)\.\([^.]*\)/\2.\1.\3/'
You could re-assign the fields, like so:
awk -F'.' -v OFS=. '{$1=$2; $2=0; print $0}'

How to use variable in awk substr [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
n=4;
echo "abcd" awk '{print substr($0,$n,1);}'
I want to get the substring by the usage of variable but I am not getting please help
$ n=4; echo "abcd" | awk -v n="$n" '{print substr($0,n,1);}'
d
Possibly, it is clearer to have two different variable names:
$ n=4; echo "abcd" | awk -v m="$n" '{print substr($0,m,1);}'
d
Here, n is a shell variable and m is an awk variable. The -v option is used to assign the awk variable m to have the value of the shell variable n.

Query on Awk Script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
780745,0,3,0,sto_un_os_fast,daily,lonrb08390,lonrs08841,1355250030,0000015139,1355265169
783333,0,1,,sto_un_os_fast,daily,lonrb08390,lonrs08842,1355336488,0000032370,0000000000
778143,0,3,0,sto_un_os_fast,daily,lonrb08390,lonrs08841,1355163652,0000003967,1355167619
784864,0,3,0,sto_un_os_fast,daily,lonrb07366,lonrs08842,1355351424,0000001475,1355352899
In the above file, I would like to print lonrb... & 3rd column after that.
Can someone please help me with this?
The problem I face with awk '{print $6, $9}' is that the columns change for 783333,0,1,, lines.
Umm - you should be able to do this by specifying "," as the field separator.
awk -F, '{print $7, $10}
Apart from the fact that it looks like columns 7 and 10, I'm not sure what the issue is:
awk -F, '{ print $7, $10 }'
In the 783333,0,1,, line, there is an empty field after the 1 and before the sto_un_os_fast, but empty fields still count as a field.
lonrb08390 0000015139
lonrb08390 0000032370
lonrb08390 0000003967
lonrb07366 0000001475
If you want a comma separating the output fields, then you can either use OFS=, or simply use printf instead of print.
awk -F, 'BEGIN { OFS = "," } { print $7, $10 }'
awk -F, '{ printf "%s,%s\n", $7, $10 }'
Both those produce the output:
lonrb08390,0000015139
lonrb08390,0000032370
lonrb08390,0000003967
lonrb07366,0000001475