Hosting & Domaining Forum

Hosting & Domaining development => Programming Discussion => Topic started by: willeachrank on Oct 18, 2024, 12:44 AM

Title: AWK and GREP Variable Conundrum
Post by: willeachrank on Oct 18, 2024, 12:44 AM
I'm struggling to pinpoint the issue with extracting specific data from a file.

For instance, I have a file containing multiple lines, and I want to display all lines that include the number 11. However, when I try to pass a variable into either awk or grep, it doesn't work as expected.

Here's the content of the file:
11 line - 1
111 line - 2
222 line - 3
1111 line - 4

Despite my efforts, it only returns the line with an exact match, which is line 1:
y='11'
awk -v c=y '\1 == c {print $0}' /test.txt

When I attempt this, it yields no results:
y='11'
awk -v c=y '\1 ~ c {print $0}' /test.txt

However, this command successfully retrieves all lines containing 11 (which is what I need, but I want to use a variable):
awk -v c=y '\1 ~ 11 {print $0}' /test.txt
I even considered using grep as an alternative, but that approach was even more convoluted. I struggled to format it properly to get the desired output, trying various methods:
y=11
cat /test.txt | grep -E "^${y}"

I've scoured the internet and browsed through forums, yet I still can't resolve this seemingly straightforward issue.
Title: Re: AWK and GREP Variable Conundrum
Post by: Brijesh on Oct 18, 2024, 02:57 AM
Your issue stems from how you're passing the variable in awk.
Instead of -v c=y, use -v c="$y" to properly reference the variable's value. Your awk command should be: awk -v c="$y" '\$0 ~ c {print \$0}' /test.txt. For grep, you can do:
grep -E "$y" /test.txt
It's frustrating that you didn't catch this sooner; these basics are crucial for effective scripting. If you're still struggling, maybe you should revisit your understanding of shell scripting fundamentals.
Title: Re: AWK and GREP Variable Conundrum
Post by: KneeseWaike on Oct 18, 2024, 05:58 AM
It's clear that you're not familiar with the basics of pattern matching in awk and grep. The syntax you're using is all wrong. Let me break it down for you: in awk, use $0 ~ c to match anywhere in the line, and in grep, use ${y} without the anchor to match anywhere in the line. And for goodness' sake, don't use backslashes before the 1 in awk.
Title: Re: AWK and GREP Variable Conundrum
Post by: vinamax2015 on Oct 18, 2024, 12:07 PM
In awk, when you use \1, it's not a literal "1", but rather a backreference to the first capture group, which doesn't exist in your case. To match the literal string "11", you should use c == "11" or c ~ /11/. However, since you're trying to use a variable, you should use c ~ c instead. But, there's a catch! When you use awk -v c=y, the value of c is not treated as a regular expression, but rather as a literal string. So, you should use awk -v c=y '$0 ~ c {print $0}' instead.