2

I'm trying to match the string in specific position using substr an running it in a loop

For example my cardfile.txt contains 01 02

My input file is 02AAA45678 04BBB04673 01CCC63848 05DDD07494

I want in the output lines having either 01 or 02 in the first 2 characters as below 02AAA45678 01CCC63848

for i in `cat cardfile.txt` do awk 'substr($0,1,2) == $i {print} ' input.txt > output.txt done

If I'm trying to run by giving the string in substr directly like "02" it is working properly but if I am giving $i it is not matching the string

Thanks in advance

  • 1
    Please format your code correctly and give an example with the input and output you expect. – Bitwise Jun 20 '18 at 18:19
  • Click `edit` under your question, then select your code with the mouse and click `{}` in the formatting toolbar. Don't add details in comments - edit your question instead. – Mark Setchell Jun 20 '18 at 18:21
  • 1
    Possible duplicate of [How do I use shell variables in an awk script?](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) – Will Barnwell Jun 20 '18 at 18:34
  • Apart from shell variable usage in awk scripts, also have a look at the best practice for looping over a file line-by-line: [BashFAQ/001](https://mywiki.wooledge.org/BashFAQ/001) – Benjamin W. Jun 20 '18 at 18:47

2 Answers2

0

The issue is that your variable $i is surrounded in 'single quotes' which is preventing bash's variable expansion.

This question asks about the same issue and has some good solutions for using bash variables such as your $i inside an awk script.

How do I use shell variables in an awk script?

Will Barnwell
  • 4,049
  • 21
  • 34
0

I am assuming your cardfile.txt is supposed to broken up like this:

02AAA45678
04BBB04673
01CCC63848
05DDD07494

Do you have a requirement to use awk in a loop? How about using grep for it:

grep -E '^(01|02)' cardfile.txt

if you must use awk as you've done, try like this:

for i in `cat cardfile.txt `; do awk 'substr($0,1,2) == "'"$i"'" {print} ' input.txt >> output.txt; done

you are running into trouble because of the single quotes and the way your shell does (or does not, in this case) expand $i.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42