Find and delete a line that starts with a certain string in a file












2















My file currently has



Name.      Address.   Phone number 
Jack. L.A 1435672
John L.A. 1465432
Nick. F.l. 1489756


When the user wants to delete a contact from the list he enters the name of the contact and the program deletes the whole line. When the user wants to modify a contact he enters the name and find the contact and then change it with a new name, address and phone number. I am trying to achieve this using two functions delete and modify.










share|improve this question









New contributor




A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    Show us some samples of how it is and how it should be!

    – George Udosen
    2 days ago











  • That’s fine, but what is your question?

    – dessert
    2 days ago











  • For example the file has a line. Jack L.A. 1456233. So when the user enters Jack the code should delete the whole line

    – A6du 2
    2 days ago













  • And another function to modify the name address and phone number

    – A6du 2
    2 days ago











  • I think this is done easily with a spreadsheet program, for example Libreoffice Calc (unless the database is too huge). For the sake of learning you can make a bash shellscript with or without using awk or sed. Anyway, please edit your original question to show us a few lines of the data file with names, addresses and phone numbers and your current version of 'bash code', the shellscript.

    – sudodus
    2 days ago
















2















My file currently has



Name.      Address.   Phone number 
Jack. L.A 1435672
John L.A. 1465432
Nick. F.l. 1489756


When the user wants to delete a contact from the list he enters the name of the contact and the program deletes the whole line. When the user wants to modify a contact he enters the name and find the contact and then change it with a new name, address and phone number. I am trying to achieve this using two functions delete and modify.










share|improve this question









New contributor




A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    Show us some samples of how it is and how it should be!

    – George Udosen
    2 days ago











  • That’s fine, but what is your question?

    – dessert
    2 days ago











  • For example the file has a line. Jack L.A. 1456233. So when the user enters Jack the code should delete the whole line

    – A6du 2
    2 days ago













  • And another function to modify the name address and phone number

    – A6du 2
    2 days ago











  • I think this is done easily with a spreadsheet program, for example Libreoffice Calc (unless the database is too huge). For the sake of learning you can make a bash shellscript with or without using awk or sed. Anyway, please edit your original question to show us a few lines of the data file with names, addresses and phone numbers and your current version of 'bash code', the shellscript.

    – sudodus
    2 days ago














2












2








2








My file currently has



Name.      Address.   Phone number 
Jack. L.A 1435672
John L.A. 1465432
Nick. F.l. 1489756


When the user wants to delete a contact from the list he enters the name of the contact and the program deletes the whole line. When the user wants to modify a contact he enters the name and find the contact and then change it with a new name, address and phone number. I am trying to achieve this using two functions delete and modify.










share|improve this question









New contributor




A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












My file currently has



Name.      Address.   Phone number 
Jack. L.A 1435672
John L.A. 1465432
Nick. F.l. 1489756


When the user wants to delete a contact from the list he enters the name of the contact and the program deletes the whole line. When the user wants to modify a contact he enters the name and find the contact and then change it with a new name, address and phone number. I am trying to achieve this using two functions delete and modify.







bash text-processing






share|improve this question









New contributor




A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









pa4080

14k52564




14k52564






New contributor




A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









A6du 2A6du 2

133




133




New contributor




A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






A6du 2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2





    Show us some samples of how it is and how it should be!

    – George Udosen
    2 days ago











  • That’s fine, but what is your question?

    – dessert
    2 days ago











  • For example the file has a line. Jack L.A. 1456233. So when the user enters Jack the code should delete the whole line

    – A6du 2
    2 days ago













  • And another function to modify the name address and phone number

    – A6du 2
    2 days ago











  • I think this is done easily with a spreadsheet program, for example Libreoffice Calc (unless the database is too huge). For the sake of learning you can make a bash shellscript with or without using awk or sed. Anyway, please edit your original question to show us a few lines of the data file with names, addresses and phone numbers and your current version of 'bash code', the shellscript.

    – sudodus
    2 days ago














  • 2





    Show us some samples of how it is and how it should be!

    – George Udosen
    2 days ago











  • That’s fine, but what is your question?

    – dessert
    2 days ago











  • For example the file has a line. Jack L.A. 1456233. So when the user enters Jack the code should delete the whole line

    – A6du 2
    2 days ago













  • And another function to modify the name address and phone number

    – A6du 2
    2 days ago











  • I think this is done easily with a spreadsheet program, for example Libreoffice Calc (unless the database is too huge). For the sake of learning you can make a bash shellscript with or without using awk or sed. Anyway, please edit your original question to show us a few lines of the data file with names, addresses and phone numbers and your current version of 'bash code', the shellscript.

    – sudodus
    2 days ago








2




2





Show us some samples of how it is and how it should be!

– George Udosen
2 days ago





Show us some samples of how it is and how it should be!

– George Udosen
2 days ago













That’s fine, but what is your question?

– dessert
2 days ago





That’s fine, but what is your question?

– dessert
2 days ago













For example the file has a line. Jack L.A. 1456233. So when the user enters Jack the code should delete the whole line

– A6du 2
2 days ago







For example the file has a line. Jack L.A. 1456233. So when the user enters Jack the code should delete the whole line

– A6du 2
2 days ago















And another function to modify the name address and phone number

– A6du 2
2 days ago





And another function to modify the name address and phone number

– A6du 2
2 days ago













I think this is done easily with a spreadsheet program, for example Libreoffice Calc (unless the database is too huge). For the sake of learning you can make a bash shellscript with or without using awk or sed. Anyway, please edit your original question to show us a few lines of the data file with names, addresses and phone numbers and your current version of 'bash code', the shellscript.

– sudodus
2 days ago





I think this is done easily with a spreadsheet program, for example Libreoffice Calc (unless the database is too huge). For the sake of learning you can make a bash shellscript with or without using awk or sed. Anyway, please edit your original question to show us a few lines of the data file with names, addresses and phone numbers and your current version of 'bash code', the shellscript.

– sudodus
2 days ago










1 Answer
1






active

oldest

votes


















2














You can use the command sed with syntax like as:



sed "/^t$name/d" in-file


Where:




  • we must use double quotes when inside the regular expression need to pass a shell variable - $name in this case.


  • ^ will match to the beginning of the line.


  • t will match to a single tab.


  • the command d at the end will delete each line that match to our regexp /^t$name/.



You can add -i (or -i.bak) to create the changes in place of the file (or and create a backup copy). Or you can Blueirect the output of the command to anotger file, etc.:



sed "/^t$name/d" in-file -i.bak




sed "/^t$name/d" in-file > out-file


More examples:



$ name='Blue'                       # assign a value to the shell variable $name

$ cat in-file # output the content of the input file
first line
second line
Blue
fourth line
Blue

$ sed "/^t*$name/d" in-file # remove the lines that begin ^ with 0 or more tabs followed by the value of $name
first line
second line
fourth line

$ sed -r "/^t+$name/d" in-file # remove the lines that begin ^ with 1 or more tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line
Blue

$ sed -r "/^t{0,1}$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r "/^t?$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r -e "/^(t|s|st|ts)?$name/d" -e 's/^t//' in-file # remove the lines that begin ^ with 0 or 1 tabs, or spaces, or spaces and tabs, or tabs and spaces`; remove the tabs in the beginning of the rest lines
first line
second line
fourth line




Edit: Here is how to substitute a whole line from the example provided in the updated question. Here is used the sed's substitution command s/regexp/replacement/.



First let's assume we have defined the following sets variables:



old_name='Jack.' old_address='L.A.' old_phone='1435672'
new_name='Tom.' new_address='F.l.' new_phone='875632'


If we need exact match of the line and want to keep the exact format, we can use the following command, that uses capture groups option: (...) -> 1, etc.; additionally the option -r (use extended regular expressions) is applied to simply the syntax (check this question as reference):



sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file


In this way we capturing the field separators (that in this case cold tabs and/or spaces) and output them in their place within the replacement string.



If we do not need to be so accurate we can use something more simple as the follow (where in the place of the capture groups our regex will expect 0 or more * characters of any type .):



sed -r "s/^.*$old_name.*$old_address.*$old_phone.*$/$new_namet$new_addresst$new_phone/" in-file


Or even more simple:



sed -r "s/^.*$old_name.*$/$new_namet$new_addresst$new_phone/" in-file


Example:



$ cat in-file 
Name. Address. Phone number
Jack. L.A. 1435672
John. L.A. 1465432
Nick. F.l. 1489756

$ old_name='Jack.' old_address='L.A.' old_phone='1435672' new_name='Tom.' new_address='F.l.' new_phone='875632'

$ sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file
Name. Address. Phone number
Tom. F.l. 875632
John. L.A. 1465432
Nick. F.l. 1489756





share|improve this answer


























  • Works perfectly Thanks appreciate it

    – A6du 2
    2 days ago













  • Is there any way I can replace the whole line with other user inputs for example in place of jack- tom, l.a - f.l and 1435267- 875632. Assuming that tom, l.a and 875632 are stored in $newname,$newaddress and$newphone respectively

    – A6du 2
    2 days ago











  • @A6du2, I've updated the answer.

    – pa4080
    2 days ago











  • I can’t accept the oldaddress and oldphone from the user so is there any way I can read those from a file given the old name

    – A6du 2
    2 days ago











  • @A6du2, yes, you can in several ways, for example you can use the bash builtin read in a way like this: read -r old_name_captured old_address old_phone < <(sed -n "/^.*$old_name.*$/p" in-file), where the first < feeds the std-in of the read command with the content of the "pseudo file" <(...). The default value of IFS= is tabs and spaces so read will assign the three monolithic strings to the tree newly created variables old_name_captured, old_address and old_phone. Here is a clue abut this usage of sed: askubuntu.com/a/1113064/566421

    – pa4080
    2 days ago













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
});


}
});






A6du 2 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1112995%2ffind-and-delete-a-line-that-starts-with-a-certain-string-in-a-file%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









2














You can use the command sed with syntax like as:



sed "/^t$name/d" in-file


Where:




  • we must use double quotes when inside the regular expression need to pass a shell variable - $name in this case.


  • ^ will match to the beginning of the line.


  • t will match to a single tab.


  • the command d at the end will delete each line that match to our regexp /^t$name/.



You can add -i (or -i.bak) to create the changes in place of the file (or and create a backup copy). Or you can Blueirect the output of the command to anotger file, etc.:



sed "/^t$name/d" in-file -i.bak




sed "/^t$name/d" in-file > out-file


More examples:



$ name='Blue'                       # assign a value to the shell variable $name

$ cat in-file # output the content of the input file
first line
second line
Blue
fourth line
Blue

$ sed "/^t*$name/d" in-file # remove the lines that begin ^ with 0 or more tabs followed by the value of $name
first line
second line
fourth line

$ sed -r "/^t+$name/d" in-file # remove the lines that begin ^ with 1 or more tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line
Blue

$ sed -r "/^t{0,1}$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r "/^t?$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r -e "/^(t|s|st|ts)?$name/d" -e 's/^t//' in-file # remove the lines that begin ^ with 0 or 1 tabs, or spaces, or spaces and tabs, or tabs and spaces`; remove the tabs in the beginning of the rest lines
first line
second line
fourth line




Edit: Here is how to substitute a whole line from the example provided in the updated question. Here is used the sed's substitution command s/regexp/replacement/.



First let's assume we have defined the following sets variables:



old_name='Jack.' old_address='L.A.' old_phone='1435672'
new_name='Tom.' new_address='F.l.' new_phone='875632'


If we need exact match of the line and want to keep the exact format, we can use the following command, that uses capture groups option: (...) -> 1, etc.; additionally the option -r (use extended regular expressions) is applied to simply the syntax (check this question as reference):



sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file


In this way we capturing the field separators (that in this case cold tabs and/or spaces) and output them in their place within the replacement string.



If we do not need to be so accurate we can use something more simple as the follow (where in the place of the capture groups our regex will expect 0 or more * characters of any type .):



sed -r "s/^.*$old_name.*$old_address.*$old_phone.*$/$new_namet$new_addresst$new_phone/" in-file


Or even more simple:



sed -r "s/^.*$old_name.*$/$new_namet$new_addresst$new_phone/" in-file


Example:



$ cat in-file 
Name. Address. Phone number
Jack. L.A. 1435672
John. L.A. 1465432
Nick. F.l. 1489756

$ old_name='Jack.' old_address='L.A.' old_phone='1435672' new_name='Tom.' new_address='F.l.' new_phone='875632'

$ sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file
Name. Address. Phone number
Tom. F.l. 875632
John. L.A. 1465432
Nick. F.l. 1489756





share|improve this answer


























  • Works perfectly Thanks appreciate it

    – A6du 2
    2 days ago













  • Is there any way I can replace the whole line with other user inputs for example in place of jack- tom, l.a - f.l and 1435267- 875632. Assuming that tom, l.a and 875632 are stored in $newname,$newaddress and$newphone respectively

    – A6du 2
    2 days ago











  • @A6du2, I've updated the answer.

    – pa4080
    2 days ago











  • I can’t accept the oldaddress and oldphone from the user so is there any way I can read those from a file given the old name

    – A6du 2
    2 days ago











  • @A6du2, yes, you can in several ways, for example you can use the bash builtin read in a way like this: read -r old_name_captured old_address old_phone < <(sed -n "/^.*$old_name.*$/p" in-file), where the first < feeds the std-in of the read command with the content of the "pseudo file" <(...). The default value of IFS= is tabs and spaces so read will assign the three monolithic strings to the tree newly created variables old_name_captured, old_address and old_phone. Here is a clue abut this usage of sed: askubuntu.com/a/1113064/566421

    – pa4080
    2 days ago


















2














You can use the command sed with syntax like as:



sed "/^t$name/d" in-file


Where:




  • we must use double quotes when inside the regular expression need to pass a shell variable - $name in this case.


  • ^ will match to the beginning of the line.


  • t will match to a single tab.


  • the command d at the end will delete each line that match to our regexp /^t$name/.



You can add -i (or -i.bak) to create the changes in place of the file (or and create a backup copy). Or you can Blueirect the output of the command to anotger file, etc.:



sed "/^t$name/d" in-file -i.bak




sed "/^t$name/d" in-file > out-file


More examples:



$ name='Blue'                       # assign a value to the shell variable $name

$ cat in-file # output the content of the input file
first line
second line
Blue
fourth line
Blue

$ sed "/^t*$name/d" in-file # remove the lines that begin ^ with 0 or more tabs followed by the value of $name
first line
second line
fourth line

$ sed -r "/^t+$name/d" in-file # remove the lines that begin ^ with 1 or more tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line
Blue

$ sed -r "/^t{0,1}$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r "/^t?$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r -e "/^(t|s|st|ts)?$name/d" -e 's/^t//' in-file # remove the lines that begin ^ with 0 or 1 tabs, or spaces, or spaces and tabs, or tabs and spaces`; remove the tabs in the beginning of the rest lines
first line
second line
fourth line




Edit: Here is how to substitute a whole line from the example provided in the updated question. Here is used the sed's substitution command s/regexp/replacement/.



First let's assume we have defined the following sets variables:



old_name='Jack.' old_address='L.A.' old_phone='1435672'
new_name='Tom.' new_address='F.l.' new_phone='875632'


If we need exact match of the line and want to keep the exact format, we can use the following command, that uses capture groups option: (...) -> 1, etc.; additionally the option -r (use extended regular expressions) is applied to simply the syntax (check this question as reference):



sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file


In this way we capturing the field separators (that in this case cold tabs and/or spaces) and output them in their place within the replacement string.



If we do not need to be so accurate we can use something more simple as the follow (where in the place of the capture groups our regex will expect 0 or more * characters of any type .):



sed -r "s/^.*$old_name.*$old_address.*$old_phone.*$/$new_namet$new_addresst$new_phone/" in-file


Or even more simple:



sed -r "s/^.*$old_name.*$/$new_namet$new_addresst$new_phone/" in-file


Example:



$ cat in-file 
Name. Address. Phone number
Jack. L.A. 1435672
John. L.A. 1465432
Nick. F.l. 1489756

$ old_name='Jack.' old_address='L.A.' old_phone='1435672' new_name='Tom.' new_address='F.l.' new_phone='875632'

$ sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file
Name. Address. Phone number
Tom. F.l. 875632
John. L.A. 1465432
Nick. F.l. 1489756





share|improve this answer


























  • Works perfectly Thanks appreciate it

    – A6du 2
    2 days ago













  • Is there any way I can replace the whole line with other user inputs for example in place of jack- tom, l.a - f.l and 1435267- 875632. Assuming that tom, l.a and 875632 are stored in $newname,$newaddress and$newphone respectively

    – A6du 2
    2 days ago











  • @A6du2, I've updated the answer.

    – pa4080
    2 days ago











  • I can’t accept the oldaddress and oldphone from the user so is there any way I can read those from a file given the old name

    – A6du 2
    2 days ago











  • @A6du2, yes, you can in several ways, for example you can use the bash builtin read in a way like this: read -r old_name_captured old_address old_phone < <(sed -n "/^.*$old_name.*$/p" in-file), where the first < feeds the std-in of the read command with the content of the "pseudo file" <(...). The default value of IFS= is tabs and spaces so read will assign the three monolithic strings to the tree newly created variables old_name_captured, old_address and old_phone. Here is a clue abut this usage of sed: askubuntu.com/a/1113064/566421

    – pa4080
    2 days ago
















2












2








2







You can use the command sed with syntax like as:



sed "/^t$name/d" in-file


Where:




  • we must use double quotes when inside the regular expression need to pass a shell variable - $name in this case.


  • ^ will match to the beginning of the line.


  • t will match to a single tab.


  • the command d at the end will delete each line that match to our regexp /^t$name/.



You can add -i (or -i.bak) to create the changes in place of the file (or and create a backup copy). Or you can Blueirect the output of the command to anotger file, etc.:



sed "/^t$name/d" in-file -i.bak




sed "/^t$name/d" in-file > out-file


More examples:



$ name='Blue'                       # assign a value to the shell variable $name

$ cat in-file # output the content of the input file
first line
second line
Blue
fourth line
Blue

$ sed "/^t*$name/d" in-file # remove the lines that begin ^ with 0 or more tabs followed by the value of $name
first line
second line
fourth line

$ sed -r "/^t+$name/d" in-file # remove the lines that begin ^ with 1 or more tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line
Blue

$ sed -r "/^t{0,1}$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r "/^t?$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r -e "/^(t|s|st|ts)?$name/d" -e 's/^t//' in-file # remove the lines that begin ^ with 0 or 1 tabs, or spaces, or spaces and tabs, or tabs and spaces`; remove the tabs in the beginning of the rest lines
first line
second line
fourth line




Edit: Here is how to substitute a whole line from the example provided in the updated question. Here is used the sed's substitution command s/regexp/replacement/.



First let's assume we have defined the following sets variables:



old_name='Jack.' old_address='L.A.' old_phone='1435672'
new_name='Tom.' new_address='F.l.' new_phone='875632'


If we need exact match of the line and want to keep the exact format, we can use the following command, that uses capture groups option: (...) -> 1, etc.; additionally the option -r (use extended regular expressions) is applied to simply the syntax (check this question as reference):



sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file


In this way we capturing the field separators (that in this case cold tabs and/or spaces) and output them in their place within the replacement string.



If we do not need to be so accurate we can use something more simple as the follow (where in the place of the capture groups our regex will expect 0 or more * characters of any type .):



sed -r "s/^.*$old_name.*$old_address.*$old_phone.*$/$new_namet$new_addresst$new_phone/" in-file


Or even more simple:



sed -r "s/^.*$old_name.*$/$new_namet$new_addresst$new_phone/" in-file


Example:



$ cat in-file 
Name. Address. Phone number
Jack. L.A. 1435672
John. L.A. 1465432
Nick. F.l. 1489756

$ old_name='Jack.' old_address='L.A.' old_phone='1435672' new_name='Tom.' new_address='F.l.' new_phone='875632'

$ sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file
Name. Address. Phone number
Tom. F.l. 875632
John. L.A. 1465432
Nick. F.l. 1489756





share|improve this answer















You can use the command sed with syntax like as:



sed "/^t$name/d" in-file


Where:




  • we must use double quotes when inside the regular expression need to pass a shell variable - $name in this case.


  • ^ will match to the beginning of the line.


  • t will match to a single tab.


  • the command d at the end will delete each line that match to our regexp /^t$name/.



You can add -i (or -i.bak) to create the changes in place of the file (or and create a backup copy). Or you can Blueirect the output of the command to anotger file, etc.:



sed "/^t$name/d" in-file -i.bak




sed "/^t$name/d" in-file > out-file


More examples:



$ name='Blue'                       # assign a value to the shell variable $name

$ cat in-file # output the content of the input file
first line
second line
Blue
fourth line
Blue

$ sed "/^t*$name/d" in-file # remove the lines that begin ^ with 0 or more tabs followed by the value of $name
first line
second line
fourth line

$ sed -r "/^t+$name/d" in-file # remove the lines that begin ^ with 1 or more tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line
Blue

$ sed -r "/^t{0,1}$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r "/^t?$name/d" in-file # remove the lines that begin ^ with 0 or 1 tabs followed by the value of $name; enable extended regexp -r
first line
second line
fourth line

$ sed -r -e "/^(t|s|st|ts)?$name/d" -e 's/^t//' in-file # remove the lines that begin ^ with 0 or 1 tabs, or spaces, or spaces and tabs, or tabs and spaces`; remove the tabs in the beginning of the rest lines
first line
second line
fourth line




Edit: Here is how to substitute a whole line from the example provided in the updated question. Here is used the sed's substitution command s/regexp/replacement/.



First let's assume we have defined the following sets variables:



old_name='Jack.' old_address='L.A.' old_phone='1435672'
new_name='Tom.' new_address='F.l.' new_phone='875632'


If we need exact match of the line and want to keep the exact format, we can use the following command, that uses capture groups option: (...) -> 1, etc.; additionally the option -r (use extended regular expressions) is applied to simply the syntax (check this question as reference):



sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file


In this way we capturing the field separators (that in this case cold tabs and/or spaces) and output them in their place within the replacement string.



If we do not need to be so accurate we can use something more simple as the follow (where in the place of the capture groups our regex will expect 0 or more * characters of any type .):



sed -r "s/^.*$old_name.*$old_address.*$old_phone.*$/$new_namet$new_addresst$new_phone/" in-file


Or even more simple:



sed -r "s/^.*$old_name.*$/$new_namet$new_addresst$new_phone/" in-file


Example:



$ cat in-file 
Name. Address. Phone number
Jack. L.A. 1435672
John. L.A. 1465432
Nick. F.l. 1489756

$ old_name='Jack.' old_address='L.A.' old_phone='1435672' new_name='Tom.' new_address='F.l.' new_phone='875632'

$ sed -r "s/^(t*|s*)$old_name(t*|s*)$old_address(t*|s*)$old_phone(t*|s*)$/1$new_name2$new_address3$new_phone4/" in-file
Name. Address. Phone number
Tom. F.l. 875632
John. L.A. 1465432
Nick. F.l. 1489756






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









pa4080pa4080

14k52564




14k52564













  • Works perfectly Thanks appreciate it

    – A6du 2
    2 days ago













  • Is there any way I can replace the whole line with other user inputs for example in place of jack- tom, l.a - f.l and 1435267- 875632. Assuming that tom, l.a and 875632 are stored in $newname,$newaddress and$newphone respectively

    – A6du 2
    2 days ago











  • @A6du2, I've updated the answer.

    – pa4080
    2 days ago











  • I can’t accept the oldaddress and oldphone from the user so is there any way I can read those from a file given the old name

    – A6du 2
    2 days ago











  • @A6du2, yes, you can in several ways, for example you can use the bash builtin read in a way like this: read -r old_name_captured old_address old_phone < <(sed -n "/^.*$old_name.*$/p" in-file), where the first < feeds the std-in of the read command with the content of the "pseudo file" <(...). The default value of IFS= is tabs and spaces so read will assign the three monolithic strings to the tree newly created variables old_name_captured, old_address and old_phone. Here is a clue abut this usage of sed: askubuntu.com/a/1113064/566421

    – pa4080
    2 days ago





















  • Works perfectly Thanks appreciate it

    – A6du 2
    2 days ago













  • Is there any way I can replace the whole line with other user inputs for example in place of jack- tom, l.a - f.l and 1435267- 875632. Assuming that tom, l.a and 875632 are stored in $newname,$newaddress and$newphone respectively

    – A6du 2
    2 days ago











  • @A6du2, I've updated the answer.

    – pa4080
    2 days ago











  • I can’t accept the oldaddress and oldphone from the user so is there any way I can read those from a file given the old name

    – A6du 2
    2 days ago











  • @A6du2, yes, you can in several ways, for example you can use the bash builtin read in a way like this: read -r old_name_captured old_address old_phone < <(sed -n "/^.*$old_name.*$/p" in-file), where the first < feeds the std-in of the read command with the content of the "pseudo file" <(...). The default value of IFS= is tabs and spaces so read will assign the three monolithic strings to the tree newly created variables old_name_captured, old_address and old_phone. Here is a clue abut this usage of sed: askubuntu.com/a/1113064/566421

    – pa4080
    2 days ago



















Works perfectly Thanks appreciate it

– A6du 2
2 days ago







Works perfectly Thanks appreciate it

– A6du 2
2 days ago















Is there any way I can replace the whole line with other user inputs for example in place of jack- tom, l.a - f.l and 1435267- 875632. Assuming that tom, l.a and 875632 are stored in $newname,$newaddress and$newphone respectively

– A6du 2
2 days ago





Is there any way I can replace the whole line with other user inputs for example in place of jack- tom, l.a - f.l and 1435267- 875632. Assuming that tom, l.a and 875632 are stored in $newname,$newaddress and$newphone respectively

– A6du 2
2 days ago













@A6du2, I've updated the answer.

– pa4080
2 days ago





@A6du2, I've updated the answer.

– pa4080
2 days ago













I can’t accept the oldaddress and oldphone from the user so is there any way I can read those from a file given the old name

– A6du 2
2 days ago





I can’t accept the oldaddress and oldphone from the user so is there any way I can read those from a file given the old name

– A6du 2
2 days ago













@A6du2, yes, you can in several ways, for example you can use the bash builtin read in a way like this: read -r old_name_captured old_address old_phone < <(sed -n "/^.*$old_name.*$/p" in-file), where the first < feeds the std-in of the read command with the content of the "pseudo file" <(...). The default value of IFS= is tabs and spaces so read will assign the three monolithic strings to the tree newly created variables old_name_captured, old_address and old_phone. Here is a clue abut this usage of sed: askubuntu.com/a/1113064/566421

– pa4080
2 days ago







@A6du2, yes, you can in several ways, for example you can use the bash builtin read in a way like this: read -r old_name_captured old_address old_phone < <(sed -n "/^.*$old_name.*$/p" in-file), where the first < feeds the std-in of the read command with the content of the "pseudo file" <(...). The default value of IFS= is tabs and spaces so read will assign the three monolithic strings to the tree newly created variables old_name_captured, old_address and old_phone. Here is a clue abut this usage of sed: askubuntu.com/a/1113064/566421

– pa4080
2 days ago












A6du 2 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















A6du 2 is a new contributor. Be nice, and check out our Code of Conduct.













A6du 2 is a new contributor. Be nice, and check out our Code of Conduct.












A6du 2 is a new contributor. Be nice, and check out our Code of Conduct.
















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%2f1112995%2ffind-and-delete-a-line-that-starts-with-a-certain-string-in-a-file%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

數位音樂下載

When can things happen in Etherscan, such as the picture below?

格利澤436b