0

Long story short, I'm trying to grep a value contained in the first column of a text file by using a variable.

Here's a sample of the script, with the grep command that doesn't work:

for ii in `cat list.txt`
do
    grep '^$ii' >outfile.txt
done

Contents of list.txt :

123,"first product",description,20.456789
456,"second product",description,30.123456
789,"third product",description,40.123456

If I perform grep '^123' list.txt, it produces the correct output... Just the first line of list.txt.

If I try to use the variable (ie grep '^ii' list.txt) I get a "^ii command not found" error. I tried to combine text with the variable to get it to work:

VAR1= "'^"$ii"'"

but the VAR1 variable contained a carriage return after the $ii variable:

'^123
'

I've tried a laundry list of things to remove the cr/lr (ie sed & awk), but to no avail. There has to be an easier way to perform the grep command using the variable. I would prefer to stay with the grep command because it works perfectly when performing it manually.

2
  • 3
    Also, show your desired output. Commented May 28, 2016 at 17:38
  • Thanks for the replies! The desired output is just the first line of the file in this case. Basically, I want the grep to search the first column and ignore the rest of the columns. If a grep is used with no flags, each line of the file will be displayed. Commented May 29, 2016 at 16:01

2 Answers 2

1

You have things mixed in the command grep '^ii' list.txt. The character ^ is for the beginning of the line and a $ is for the value of a variable. When you want to grep for 123 in the variable ii at the beginning of the line, use

ii="123"
grep "^$ii" list.txt

(You should use double quotes here)
Good moment for learning good habits: Continue in variable names in lowercase (well done) and use curly braces (don't harm and are needed in other cases) :

ii="123"
grep "^${ii}" list.txt

Now we both are forgetting something: Our grep will also match 1234,"4-digit product",description,11.1111. Include a , in the grep:

ii="123"
grep "^${ii}," list.txt

And how did you get the "^ii command not found" error ? I think you used backquotes (old way for nesting a command, better is echo "example: $(date)") and you wrote

grep `^ii` list.txt # wrong !
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Walter, that did the trick! in addition to your response, I found that I needed to perform a dos2unix on the list.txt file because it was generated in Windows. Thank you to everyone who replied!!!
0
#!/bin/sh
# Read every character before the first comma into the variable ii.
while IFS=, read ii rest; do
    # Echo the value of ii. If these values are what you want, you're done; no
    # need for grep.
    echo "ii = $ii"
    # If you want to find something associated with these values in another
    # file, however, you can grep the file for the values. Use double quotes so
    # that the value of $ii is substituted in the argument to grep.
    grep "^$ii" some_other_file.txt >outfile.txt
done <list.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.