remove string ends with certain extension in a file
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Need to remove .sql entry in file
I have file which contains strings ends with both .class and .sql and I need to remove only string which ends with .sql in the file
file.txt
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
test4.sql
test5.sql
Output file which i expect is
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
Using below script but not able to achieve
for i in `cat file.txt` ; do
fname=`echo $i | cut -d "." -f1`
echo $fname
if file=$fname.sql
then
sed "/$fname.sql/d" file.txt > output_file.txt
else
exit 0
fi
done
with above script getting below output
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
shell-script shell sed
add a comment |Â
up vote
0
down vote
favorite
Need to remove .sql entry in file
I have file which contains strings ends with both .class and .sql and I need to remove only string which ends with .sql in the file
file.txt
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
test4.sql
test5.sql
Output file which i expect is
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
Using below script but not able to achieve
for i in `cat file.txt` ; do
fname=`echo $i | cut -d "." -f1`
echo $fname
if file=$fname.sql
then
sed "/$fname.sql/d" file.txt > output_file.txt
else
exit 0
fi
done
with above script getting below output
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
shell-script shell sed
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Need to remove .sql entry in file
I have file which contains strings ends with both .class and .sql and I need to remove only string which ends with .sql in the file
file.txt
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
test4.sql
test5.sql
Output file which i expect is
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
Using below script but not able to achieve
for i in `cat file.txt` ; do
fname=`echo $i | cut -d "." -f1`
echo $fname
if file=$fname.sql
then
sed "/$fname.sql/d" file.txt > output_file.txt
else
exit 0
fi
done
with above script getting below output
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
shell-script shell sed
Need to remove .sql entry in file
I have file which contains strings ends with both .class and .sql and I need to remove only string which ends with .sql in the file
file.txt
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
test4.sql
test5.sql
Output file which i expect is
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
Using below script but not able to achieve
for i in `cat file.txt` ; do
fname=`echo $i | cut -d "." -f1`
echo $fname
if file=$fname.sql
then
sed "/$fname.sql/d" file.txt > output_file.txt
else
exit 0
fi
done
with above script getting below output
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
shell-script shell sed
edited Dec 6 '17 at 10:45
Rahul
8,52612841
8,52612841
asked Dec 6 '17 at 10:27
Bindhu
1
1
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
grep
solution:
grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt
The 2nd grep
statement/command will be executed only if the 1st one returned zero exit status 0
if any match against pattern ..sql$
is found
The final output_file.txt
content:
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
add a comment |Â
up vote
1
down vote
Using
sed
sed '/.sql$/d' test.txt
For bash (3.2+)
while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt
Using perl
perl -ni.bak -e "print unless /.sql/" test.txt
1
Anchoring the pattern to $ end-of-line would be an improvement
â Jeff Schaller
Dec 6 '17 at 11:10
add a comment |Â
up vote
0
down vote
I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.
for i in $(cat ...)
is not good as it seperates your input by spaces by default as well, not just by newline.- The if statement doesn't make sense as
file
is not defined. For a typicalif
statement you would need to invoketest
, usually by putting things inside a pair of square brackets. For example,if [ $f = "abc" ]; then
. Note that all spaces here are necessary. exit 0
stops the script from handling all the lines after the given line, I believe that is not intended.
I hope these can help you write better shell scripts.
add a comment |Â
up vote
0
down vote
Another grep, just invert the match
grep -v ".sql$" filelist
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
grep
solution:
grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt
The 2nd grep
statement/command will be executed only if the 1st one returned zero exit status 0
if any match against pattern ..sql$
is found
The final output_file.txt
content:
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
add a comment |Â
up vote
2
down vote
grep
solution:
grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt
The 2nd grep
statement/command will be executed only if the 1st one returned zero exit status 0
if any match against pattern ..sql$
is found
The final output_file.txt
content:
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
add a comment |Â
up vote
2
down vote
up vote
2
down vote
grep
solution:
grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt
The 2nd grep
statement/command will be executed only if the 1st one returned zero exit status 0
if any match against pattern ..sql$
is found
The final output_file.txt
content:
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
grep
solution:
grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt
The 2nd grep
statement/command will be executed only if the 1st one returned zero exit status 0
if any match against pattern ..sql$
is found
The final output_file.txt
content:
actual.class
actual1.class
actual2.class
actual3.class
actual4.class
answered Dec 6 '17 at 10:36
RomanPerekhrest
22.4k12145
22.4k12145
add a comment |Â
add a comment |Â
up vote
1
down vote
Using
sed
sed '/.sql$/d' test.txt
For bash (3.2+)
while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt
Using perl
perl -ni.bak -e "print unless /.sql/" test.txt
1
Anchoring the pattern to $ end-of-line would be an improvement
â Jeff Schaller
Dec 6 '17 at 11:10
add a comment |Â
up vote
1
down vote
Using
sed
sed '/.sql$/d' test.txt
For bash (3.2+)
while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt
Using perl
perl -ni.bak -e "print unless /.sql/" test.txt
1
Anchoring the pattern to $ end-of-line would be an improvement
â Jeff Schaller
Dec 6 '17 at 11:10
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Using
sed
sed '/.sql$/d' test.txt
For bash (3.2+)
while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt
Using perl
perl -ni.bak -e "print unless /.sql/" test.txt
Using
sed
sed '/.sql$/d' test.txt
For bash (3.2+)
while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt
Using perl
perl -ni.bak -e "print unless /.sql/" test.txt
edited Dec 6 '17 at 11:21
Philippos
5,91211546
5,91211546
answered Dec 6 '17 at 10:39
Rahul
8,52612841
8,52612841
1
Anchoring the pattern to $ end-of-line would be an improvement
â Jeff Schaller
Dec 6 '17 at 11:10
add a comment |Â
1
Anchoring the pattern to $ end-of-line would be an improvement
â Jeff Schaller
Dec 6 '17 at 11:10
1
1
Anchoring the pattern to $ end-of-line would be an improvement
â Jeff Schaller
Dec 6 '17 at 11:10
Anchoring the pattern to $ end-of-line would be an improvement
â Jeff Schaller
Dec 6 '17 at 11:10
add a comment |Â
up vote
0
down vote
I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.
for i in $(cat ...)
is not good as it seperates your input by spaces by default as well, not just by newline.- The if statement doesn't make sense as
file
is not defined. For a typicalif
statement you would need to invoketest
, usually by putting things inside a pair of square brackets. For example,if [ $f = "abc" ]; then
. Note that all spaces here are necessary. exit 0
stops the script from handling all the lines after the given line, I believe that is not intended.
I hope these can help you write better shell scripts.
add a comment |Â
up vote
0
down vote
I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.
for i in $(cat ...)
is not good as it seperates your input by spaces by default as well, not just by newline.- The if statement doesn't make sense as
file
is not defined. For a typicalif
statement you would need to invoketest
, usually by putting things inside a pair of square brackets. For example,if [ $f = "abc" ]; then
. Note that all spaces here are necessary. exit 0
stops the script from handling all the lines after the given line, I believe that is not intended.
I hope these can help you write better shell scripts.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.
for i in $(cat ...)
is not good as it seperates your input by spaces by default as well, not just by newline.- The if statement doesn't make sense as
file
is not defined. For a typicalif
statement you would need to invoketest
, usually by putting things inside a pair of square brackets. For example,if [ $f = "abc" ]; then
. Note that all spaces here are necessary. exit 0
stops the script from handling all the lines after the given line, I believe that is not intended.
I hope these can help you write better shell scripts.
I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.
for i in $(cat ...)
is not good as it seperates your input by spaces by default as well, not just by newline.- The if statement doesn't make sense as
file
is not defined. For a typicalif
statement you would need to invoketest
, usually by putting things inside a pair of square brackets. For example,if [ $f = "abc" ]; then
. Note that all spaces here are necessary. exit 0
stops the script from handling all the lines after the given line, I believe that is not intended.
I hope these can help you write better shell scripts.
answered Dec 6 '17 at 14:17
Weijun Zhou
1,434119
1,434119
add a comment |Â
add a comment |Â
up vote
0
down vote
Another grep, just invert the match
grep -v ".sql$" filelist
add a comment |Â
up vote
0
down vote
Another grep, just invert the match
grep -v ".sql$" filelist
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Another grep, just invert the match
grep -v ".sql$" filelist
Another grep, just invert the match
grep -v ".sql$" filelist
answered Dec 6 '17 at 16:30
bu5hman
1,164214
1,164214
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409154%2fremove-string-ends-with-certain-extension-in-a-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password