A more efficient approach.
It's more efficient in several ways.
First, fewer subshells need to be forked.
Second, since grep's output isn't required, there's no need to pipe data from grep to a command substitution parent shell.
Third, suppose that there are very many matches in the ls output. We only need to find the first one. Once a single match is found, with -q, grep can exit immediately since the result is known to be true.
On an unrelated note, unless you intend to allow some sort of expansion between them, or unless you are trying to avoid a collision with another set of quotes, it's a good habit to use single quotes instead of double quotes. Also, while a trivial difference, it's also more efficient to parse single-quoted text.
Regards,
Alister
---------- Post updated at 03:43 PM ---------- Previous update was at 03:39 PM ----------
Quote:
Originally Posted by ahamed101
Something similar...
You can probably also redirect stdout to /dev/null and test the exit status directly.
Regards,
Alister
---------- Post updated at 03:46 PM ---------- Previous update was at 03:43 PM ----------
Quote:
Originally Posted by neutronscott
With the usual shell defaults, the if-condition will always succeed, since if nothing matches the pattern the pattern remains unchanged and is assigned as the sole member of the array. For this code to work, nullglob must be enabled.
Regards,
Alister
---------- Post updated at 03:59 PM ---------- Previous update was at 03:46 PM ----------
In spirit, the following is akin to Scrutinizer's approach, but instead of using a function parameter to isolate the first of potentially many matches, it uses the for-loop's list assignment and an unconditional break.
Regards,
Alister
Hi folks,
I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex.
I have a bunch of update statements with all columns in a file which I need to convert into insert statements.
UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Hi,
I have a file with data listed below:
clm_id double,
cnl_id double,
cnl_amt double
I want output to be
clm_id string,
cnl_id string,
cnl_amt double
So replace double by string if the field contains the word _id
I tried
sed -e 's/_id/& string/' filename
The above will not... (2 Replies)
Hello Guys,
I am new here and this is my first post, hope someone can help me
I am writing a script that is supposed to go in 9 different directories and edit a given file in each of the directories. I am using sed to edit the file as
sed -i 'line# s/#to be changed/#to be replaced with/... (5 Replies)
Hi
#Testing for file existence
if ; then
echo 'SCHOOL data is available for processing'
else
echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING'
:
i wrote a script, where it begins by checking if file exists or not.
If it exists, it truncates the database... (2 Replies)
I need to check if an entry input by the user is in a file. If so, I need to run a command, and if it does not exist then it should output entry does not exist.
So I have so far...
echo "Enter record:"
read record
//command || //command
Can I use an if statement to do this? (3 Replies)
Hi All,
If i run below copy command, it works absolutely fine,
/opt/csw/bin/scp axetlxyz01:/opt/data/test/QURIES* ./input
I want to make the above line better, by adding an IF statement, want to check if there is any file exists with name QURIES*.* then i need to copy that.
if
... (7 Replies)
Hi,
I'm trying to write a bit of code that will check if a file exists and then archives the file
Im trying to use the following if statement without success..
if
then
mv filename archive/filename
else
echo "no filename exists"
fi
Should the file name be... (3 Replies)
I am just wondering if it's possible to refer to variables within a sed statement as follows:-
cat $file | sed -e 1's/$oldtext/$newtext/' > $file
as when I run the script, the variables are not recognised and nothing happens..??
Thanks (5 Replies)
this is my output for my crawler.
/about.html
/ads/
/advanced_search?hl=en
froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8
groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8
/imghp?hl=en&tab=wi&ie=UTF-8
/intl/en/options/
/language_tools?hl=en
/maphp?hl=en&tab=wl&ie=UTF-8... (3 Replies)