0

So I want to grab a string from a file, file contains data:

----------------------------------------------------
Id   Name      CreationDate         Comment
----------------------------------------------------
1    testing    19.10.11             created by jag

2    develop    19.10.12             created by jag

3    array      19.10.12             created by jaguuuu

4    start123   19.10.12             created by akj

I want to grep both start123 but using only start because following number changes from time to time. So it could be start456, start567. But it will start with start****.

This is what I tried so far:

awk '$0 ~ arr{print NR-1 FS b}{b=$0}' arr="start" /filepath
echo "string found : $arr"

Updating: Also I want to extract only start123 from second column which could be in any row from 1-4 or 1-whatever number. Once I got the string "start123", want to store it in an variable. Sorry for not being clear initially.

So if I try to sort it via comment = created by akj and print out start123 still. I think it will be just an && statement. Will something like this work:

arr=$(awk -v str=start '$2 ~ "^" str "[0-9]*" { print $2 }' /filepath)
 if [ -z "$arr" ]
 then echo "string not found"
 else echo "String found: $arr"
 fi

It is not working for some reason. Any help would be appreciated.

Thanks

Kyle

2
  • "grep" is the name of a program that will do what you want. Is there a reason you are using awk? (which will also do what you want, but it's not as easy as using grep) Commented Sep 9, 2014 at 20:58
  • The example that I referred was using awk, so I have being trying to get this to work. But I don't know what I am doing wrong. I want to know where I am going wrong. Commented Sep 9, 2014 at 21:00

2 Answers 2

2

You can use the -o option of grep to print just the part of the file that matches the regular expression. In this case, it's start followed by any number of digits.

str=start
if arr=$(grep -o "$str[0-9]*" /filepath)
then echo "string found: $arr"
else echo "string not found"
fi

If you only want to find it in column 2, you can use awk:

arr=$(awk -v str=start '$2 ~ "^" str "[0-9]*" { print $2; exit; }' /filepath)
if [ -z "$arr" ]
then echo "string not found"
else echo "String found: $arr"
fi
Sign up to request clarification or add additional context in comments.

10 Comments

I want to print start123 and store that in a variable.
Maybe you should say something like that in the question? There's nothing about printing just the second column or assigning to a variable in there.
actually my bad again. As I mentioned in the code there could be string such as start123, start456, Can I sort it via comment? I tried as I updated the code. Also once found should stop, It just continues to run. Wonder why???
I tried my code and it worked. It matches start followed by any sequence of digits.
yes, it prints everything that contains start***. I am trying to tweak it where lets say it has start123, start456, start566. Once it hits the first one. for example: start123. it should store it in an variable and stop from going to other one.
|
1

If you have GNU awk:

gawk 'match($0, /(start[0-9]+)/, m) {print m[1]; exit}'

documentation: http://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#String-Functions

2 Comments

Instead of file path can I have variable?? arr=$(awk -v str=start '$2 ~ "^" str "[0-9]*" { print $2; exit; }' /filepath)
sure: gawk -v str=start 'match($0, "(" str "[0-9]+", m) {print m[1]; exit}' /filepath

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.