How to delete a line from a file containing sh extension?












0















I'm using Ubuntu. I have an fileName.sh file containing 1000 lines and I have to delete the 399 number line is this command is sufficient for the deleting the line.



Command:- sed -i '399d' test.sh



Is there any need to change in this command? And can you please give me more suggestions to delete the line from a file?










share|improve this question


















  • 3





    Have you tried running the command and verifying the result manually ? What was the result ?

    – Soren A
    Jan 14 at 7:21











  • yes I tried this command in result it will delete the line from the file @SorenA

    – catter
    Jan 14 at 7:23











  • So why the question ?

    – Soren A
    Jan 14 at 7:27






  • 1





    IMHO deleting by line number is dangerous, if there is any change in what produces the target file you'll be deleting the wrong line. Deleting by pattern woul be a bit safer (though still dangerous if done on a script).

    – xenoid
    Jan 14 at 7:34











  • @xenoid whatever method you do it all has the same risk. If 1 of the actual arguments is "is has to be line 399" I do not see anything wrong. I would assume someone would 1st verify it is indeed line 399.

    – Rinzwind
    Jan 14 at 7:37
















0















I'm using Ubuntu. I have an fileName.sh file containing 1000 lines and I have to delete the 399 number line is this command is sufficient for the deleting the line.



Command:- sed -i '399d' test.sh



Is there any need to change in this command? And can you please give me more suggestions to delete the line from a file?










share|improve this question


















  • 3





    Have you tried running the command and verifying the result manually ? What was the result ?

    – Soren A
    Jan 14 at 7:21











  • yes I tried this command in result it will delete the line from the file @SorenA

    – catter
    Jan 14 at 7:23











  • So why the question ?

    – Soren A
    Jan 14 at 7:27






  • 1





    IMHO deleting by line number is dangerous, if there is any change in what produces the target file you'll be deleting the wrong line. Deleting by pattern woul be a bit safer (though still dangerous if done on a script).

    – xenoid
    Jan 14 at 7:34











  • @xenoid whatever method you do it all has the same risk. If 1 of the actual arguments is "is has to be line 399" I do not see anything wrong. I would assume someone would 1st verify it is indeed line 399.

    – Rinzwind
    Jan 14 at 7:37














0












0








0








I'm using Ubuntu. I have an fileName.sh file containing 1000 lines and I have to delete the 399 number line is this command is sufficient for the deleting the line.



Command:- sed -i '399d' test.sh



Is there any need to change in this command? And can you please give me more suggestions to delete the line from a file?










share|improve this question














I'm using Ubuntu. I have an fileName.sh file containing 1000 lines and I have to delete the 399 number line is this command is sufficient for the deleting the line.



Command:- sed -i '399d' test.sh



Is there any need to change in this command? And can you please give me more suggestions to delete the line from a file?







command-line bash scripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 14 at 7:07









cattercatter

62




62








  • 3





    Have you tried running the command and verifying the result manually ? What was the result ?

    – Soren A
    Jan 14 at 7:21











  • yes I tried this command in result it will delete the line from the file @SorenA

    – catter
    Jan 14 at 7:23











  • So why the question ?

    – Soren A
    Jan 14 at 7:27






  • 1





    IMHO deleting by line number is dangerous, if there is any change in what produces the target file you'll be deleting the wrong line. Deleting by pattern woul be a bit safer (though still dangerous if done on a script).

    – xenoid
    Jan 14 at 7:34











  • @xenoid whatever method you do it all has the same risk. If 1 of the actual arguments is "is has to be line 399" I do not see anything wrong. I would assume someone would 1st verify it is indeed line 399.

    – Rinzwind
    Jan 14 at 7:37














  • 3





    Have you tried running the command and verifying the result manually ? What was the result ?

    – Soren A
    Jan 14 at 7:21











  • yes I tried this command in result it will delete the line from the file @SorenA

    – catter
    Jan 14 at 7:23











  • So why the question ?

    – Soren A
    Jan 14 at 7:27






  • 1





    IMHO deleting by line number is dangerous, if there is any change in what produces the target file you'll be deleting the wrong line. Deleting by pattern woul be a bit safer (though still dangerous if done on a script).

    – xenoid
    Jan 14 at 7:34











  • @xenoid whatever method you do it all has the same risk. If 1 of the actual arguments is "is has to be line 399" I do not see anything wrong. I would assume someone would 1st verify it is indeed line 399.

    – Rinzwind
    Jan 14 at 7:37








3




3





Have you tried running the command and verifying the result manually ? What was the result ?

– Soren A
Jan 14 at 7:21





Have you tried running the command and verifying the result manually ? What was the result ?

– Soren A
Jan 14 at 7:21













yes I tried this command in result it will delete the line from the file @SorenA

– catter
Jan 14 at 7:23





yes I tried this command in result it will delete the line from the file @SorenA

– catter
Jan 14 at 7:23













So why the question ?

– Soren A
Jan 14 at 7:27





So why the question ?

– Soren A
Jan 14 at 7:27




1




1





IMHO deleting by line number is dangerous, if there is any change in what produces the target file you'll be deleting the wrong line. Deleting by pattern woul be a bit safer (though still dangerous if done on a script).

– xenoid
Jan 14 at 7:34





IMHO deleting by line number is dangerous, if there is any change in what produces the target file you'll be deleting the wrong line. Deleting by pattern woul be a bit safer (though still dangerous if done on a script).

– xenoid
Jan 14 at 7:34













@xenoid whatever method you do it all has the same risk. If 1 of the actual arguments is "is has to be line 399" I do not see anything wrong. I would assume someone would 1st verify it is indeed line 399.

– Rinzwind
Jan 14 at 7:37





@xenoid whatever method you do it all has the same risk. If 1 of the actual arguments is "is has to be line 399" I do not see anything wrong. I would assume someone would 1st verify it is indeed line 399.

– Rinzwind
Jan 14 at 7:37










1 Answer
1






active

oldest

votes


















0














Better (but stiull unsafe) to remove the line using its contents than using a line number. If you want to delete a line whose contents are known:



sed -i /{contents}/d in.sh


Another solution is to use grep -v:



grep -v '{contents}' <in.sh >out.sh


In both cases {contents} is a regexp that matches the contents you want to remove, knowing that strings with only letters and numbers and a few select special characters are regular expressions for themselves, so:



sed -i /somefile/d in.sh
grep -v 'somefile' <in.sh >out.sh


will remove any line containing somefile from the input. But keep in mind that they could also match more things (for instance somefile2 would also be matched) unless you start using more complete regexps, for instance using ^{contents}$ to match lines that are exactly {contents}, excluding those that contain {contents} and additional things.






share|improve this answer
























  • Thanks for your answer :)

    – catter
    Jan 14 at 13:11











  • @catter, please also read What should I do when someone answers my question?. No need to hurry, though. Just take your time.

    – PerlDuck
    Jan 14 at 13:38











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1109548%2fhow-to-delete-a-line-from-a-file-containing-sh-extension%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Better (but stiull unsafe) to remove the line using its contents than using a line number. If you want to delete a line whose contents are known:



sed -i /{contents}/d in.sh


Another solution is to use grep -v:



grep -v '{contents}' <in.sh >out.sh


In both cases {contents} is a regexp that matches the contents you want to remove, knowing that strings with only letters and numbers and a few select special characters are regular expressions for themselves, so:



sed -i /somefile/d in.sh
grep -v 'somefile' <in.sh >out.sh


will remove any line containing somefile from the input. But keep in mind that they could also match more things (for instance somefile2 would also be matched) unless you start using more complete regexps, for instance using ^{contents}$ to match lines that are exactly {contents}, excluding those that contain {contents} and additional things.






share|improve this answer
























  • Thanks for your answer :)

    – catter
    Jan 14 at 13:11











  • @catter, please also read What should I do when someone answers my question?. No need to hurry, though. Just take your time.

    – PerlDuck
    Jan 14 at 13:38
















0














Better (but stiull unsafe) to remove the line using its contents than using a line number. If you want to delete a line whose contents are known:



sed -i /{contents}/d in.sh


Another solution is to use grep -v:



grep -v '{contents}' <in.sh >out.sh


In both cases {contents} is a regexp that matches the contents you want to remove, knowing that strings with only letters and numbers and a few select special characters are regular expressions for themselves, so:



sed -i /somefile/d in.sh
grep -v 'somefile' <in.sh >out.sh


will remove any line containing somefile from the input. But keep in mind that they could also match more things (for instance somefile2 would also be matched) unless you start using more complete regexps, for instance using ^{contents}$ to match lines that are exactly {contents}, excluding those that contain {contents} and additional things.






share|improve this answer
























  • Thanks for your answer :)

    – catter
    Jan 14 at 13:11











  • @catter, please also read What should I do when someone answers my question?. No need to hurry, though. Just take your time.

    – PerlDuck
    Jan 14 at 13:38














0












0








0







Better (but stiull unsafe) to remove the line using its contents than using a line number. If you want to delete a line whose contents are known:



sed -i /{contents}/d in.sh


Another solution is to use grep -v:



grep -v '{contents}' <in.sh >out.sh


In both cases {contents} is a regexp that matches the contents you want to remove, knowing that strings with only letters and numbers and a few select special characters are regular expressions for themselves, so:



sed -i /somefile/d in.sh
grep -v 'somefile' <in.sh >out.sh


will remove any line containing somefile from the input. But keep in mind that they could also match more things (for instance somefile2 would also be matched) unless you start using more complete regexps, for instance using ^{contents}$ to match lines that are exactly {contents}, excluding those that contain {contents} and additional things.






share|improve this answer













Better (but stiull unsafe) to remove the line using its contents than using a line number. If you want to delete a line whose contents are known:



sed -i /{contents}/d in.sh


Another solution is to use grep -v:



grep -v '{contents}' <in.sh >out.sh


In both cases {contents} is a regexp that matches the contents you want to remove, knowing that strings with only letters and numbers and a few select special characters are regular expressions for themselves, so:



sed -i /somefile/d in.sh
grep -v 'somefile' <in.sh >out.sh


will remove any line containing somefile from the input. But keep in mind that they could also match more things (for instance somefile2 would also be matched) unless you start using more complete regexps, for instance using ^{contents}$ to match lines that are exactly {contents}, excluding those that contain {contents} and additional things.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 14 at 12:58









xenoidxenoid

1,5781416




1,5781416













  • Thanks for your answer :)

    – catter
    Jan 14 at 13:11











  • @catter, please also read What should I do when someone answers my question?. No need to hurry, though. Just take your time.

    – PerlDuck
    Jan 14 at 13:38



















  • Thanks for your answer :)

    – catter
    Jan 14 at 13:11











  • @catter, please also read What should I do when someone answers my question?. No need to hurry, though. Just take your time.

    – PerlDuck
    Jan 14 at 13:38

















Thanks for your answer :)

– catter
Jan 14 at 13:11





Thanks for your answer :)

– catter
Jan 14 at 13:11













@catter, please also read What should I do when someone answers my question?. No need to hurry, though. Just take your time.

– PerlDuck
Jan 14 at 13:38





@catter, please also read What should I do when someone answers my question?. No need to hurry, though. Just take your time.

– PerlDuck
Jan 14 at 13:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1109548%2fhow-to-delete-a-line-from-a-file-containing-sh-extension%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Questions related to Moebius Transform of Characteristic Function of the Primes

List of scandals in India

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?