The code below gives the string "test1.txt" even though "tessdsdt" does not match "test1.txt". I would like to return "" if there is no match and return some kind of error that I can capture and decide what to do.
echo test1.txt | awk -v src="tessdsdt" -v dst="test" '{sub(src,dst); print}'
echo test1.txt |
awk -v src="tessdsdt" -v dst="test" '{if(sub(src,dst)){print} else {print "No match"}}'
I also have another problem. I want to check if $f contains $fsrc, but $match is empty.
set f = "test1.txt"
set fsrc = "test"
set match = `echo $f | awk '/$fsrc/'`
echo $match
Try this
#!/bin/ksh
f="test1.txt"
fsrc="test"
match=`grep $fsrc $f`
echo $match
you should use -v and pass arguments to awk if you are using it.
regards,
Ahamed
I have tried the code below to no avail.
set match = `echo $f | awk -v src=$fsrc '/src/'`
---------- Post updated at 06:04 AM ---------- Previous update was at 05:58 AM ----------
The grep thing is not working.
Did you mean ... ?
match=`echo $f | grep $fsrc`
kristinu:
I have tried the code below to no avail.
set match = `echo $f | awk -v src=$fsrc '/src/'`
---------- Post updated at 06:04 AM ---------- Previous update was at 05:58 AM ----------
The grep thing is not working.
You sure you have the string "test" in the file "test1.txt?
regards,
Ahamed
This is not working. I get the empty string.
set f = "test1.txt"
set fsrc = "test"
set match = `echo $f | awk -v src=$fsrc '/src/'`
Both of these work for me ...
f="test1.txt"
fsrc="test"
match=`echo $f | awk -v src=$fsrc 'src'`
or better with grep, as stated previously in this thread ...
match=`echo $f | grep $fsrc`
This is ok
---------- Post updated at 06:41 AM ---------- Previous update was at 06:35 AM ----------
I have tried the code below
set f = "test1.txt"
set fsrc = "test"
set match = `echo $f | awk -v src=$fsrc '/src/'`
echo "match = .$match. $f $fsrc"
However I am getting
match = .. test1.txt test
Examine my example more carefully. Remove the slashes from your awk command.
Regards,
Mark.
Yep, it works. How does it work without the slashes I cannot understand !!!
---------- Post updated at 06:52 AM ---------- Previous update was at 06:47 AM ----------
Now I do the below and still get "match = test1.txt" even though f does not contain fchk
set f = "test1.txt"
set fchk = "tst"
set match = `echo $f | awk -v chk=$fchk 'chk'`
echo "match = .$match. $f $fsrc"
Sorry, my fault:
match=`echo $f | awk -v chk=$fchk '$0 ~ chk'`
I have a big file, let's call it "test.txt". I want to check if it finds $lsrc in it. However, the result can get quite long if there are lot of matches, how can I just check if I get a match or not?
set result = `grep $lsrc $f`
result=`awk -v var=$lsrc '$0 ~ var{print; exit}'`
kristinu:
I have a big file, let's call it "test.txt". I want to check if it finds $lsrc in it. However, the result can get quite long if there are lot of matches, how can I just check if I get a match or not?
set result = `grep $lsrc $f`
set result = `grep -c $lsrc $f`
...might be better