bash script a line of code i am trying to use











up vote
0
down vote

favorite












I have a file that I store reservation into it and I am trying to remove the old reservation and put the new reservation if the same name appears before.
does this line of code do it for me in bash?



sed -i .bak "/^$name|/d" reservation.txt




#!/bin/bash


#start by building a zenity command
ZCMD='/bin/zenity/bin/zenity --title=PA5'
name=`$ZCMD --entry --text="Enter your Name"`

cities=("Portland" "Vancouver" "Seattle" "Riyadh" "Jeddah" ""Las Vegas"" ""New York"")
departCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you departing from?"`

arrivalCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you going to?"`

departDate=`$ZCMD --calendar --text="Choose your departure dates"`
arrivalDate=`$ZCMD --calendar --text="Choose your arrival dates"`

bags=`$ZCMD --entry --entry-text=0 1 2 3 4 5 --text="How many bags are you taking?"`

#Summary of the trip info
echo "Travelers name: $name"
echo "Departure City: $departCity"
echo "Arrival City: $arrivalCity"
echo "Departing Date: $departDate"
echo "Arrival Date: $arrivalDate"
echo "Number of Bags: $bags"

echo "$name|$departCity|$arrivalCity|$departDate|$arrivalDate|$bags" >> reservation.txt
sed -i .bak "/^$name|/d" reservation.txt









share|improve this question




















  • 1




    Welcome to StackOverflow! You could try to execute that yourself and see if it's doing it for you. Then if the result is not as what you expected, include both current and expected results, and sample data in your question.
    – Andreas
    Nov 22 at 4:29












  • That will remove all lines that begin with the name. It doesn't add a new reservation, and doesn't check whether the same name appears before it.
    – Barmar
    Nov 22 at 4:39










  • Okay, how I can replace the old name reservation with a new reservation? if you want to see my code I can post it here?
    – Rawad Bader
    Nov 22 at 4:46












  • s/$name|/new reservation here/ but then you have the opposite problem; if an old reservation doesn't exist, it won't create the new one either. Are you really married to sed for this task?
    – tripleee
    Nov 22 at 6:29










  • No, I can use different the "sed" but I do not know what to use?
    – Rawad Bader
    Nov 22 at 7:52















up vote
0
down vote

favorite












I have a file that I store reservation into it and I am trying to remove the old reservation and put the new reservation if the same name appears before.
does this line of code do it for me in bash?



sed -i .bak "/^$name|/d" reservation.txt




#!/bin/bash


#start by building a zenity command
ZCMD='/bin/zenity/bin/zenity --title=PA5'
name=`$ZCMD --entry --text="Enter your Name"`

cities=("Portland" "Vancouver" "Seattle" "Riyadh" "Jeddah" ""Las Vegas"" ""New York"")
departCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you departing from?"`

arrivalCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you going to?"`

departDate=`$ZCMD --calendar --text="Choose your departure dates"`
arrivalDate=`$ZCMD --calendar --text="Choose your arrival dates"`

bags=`$ZCMD --entry --entry-text=0 1 2 3 4 5 --text="How many bags are you taking?"`

#Summary of the trip info
echo "Travelers name: $name"
echo "Departure City: $departCity"
echo "Arrival City: $arrivalCity"
echo "Departing Date: $departDate"
echo "Arrival Date: $arrivalDate"
echo "Number of Bags: $bags"

echo "$name|$departCity|$arrivalCity|$departDate|$arrivalDate|$bags" >> reservation.txt
sed -i .bak "/^$name|/d" reservation.txt









share|improve this question




















  • 1




    Welcome to StackOverflow! You could try to execute that yourself and see if it's doing it for you. Then if the result is not as what you expected, include both current and expected results, and sample data in your question.
    – Andreas
    Nov 22 at 4:29












  • That will remove all lines that begin with the name. It doesn't add a new reservation, and doesn't check whether the same name appears before it.
    – Barmar
    Nov 22 at 4:39










  • Okay, how I can replace the old name reservation with a new reservation? if you want to see my code I can post it here?
    – Rawad Bader
    Nov 22 at 4:46












  • s/$name|/new reservation here/ but then you have the opposite problem; if an old reservation doesn't exist, it won't create the new one either. Are you really married to sed for this task?
    – tripleee
    Nov 22 at 6:29










  • No, I can use different the "sed" but I do not know what to use?
    – Rawad Bader
    Nov 22 at 7:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file that I store reservation into it and I am trying to remove the old reservation and put the new reservation if the same name appears before.
does this line of code do it for me in bash?



sed -i .bak "/^$name|/d" reservation.txt




#!/bin/bash


#start by building a zenity command
ZCMD='/bin/zenity/bin/zenity --title=PA5'
name=`$ZCMD --entry --text="Enter your Name"`

cities=("Portland" "Vancouver" "Seattle" "Riyadh" "Jeddah" ""Las Vegas"" ""New York"")
departCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you departing from?"`

arrivalCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you going to?"`

departDate=`$ZCMD --calendar --text="Choose your departure dates"`
arrivalDate=`$ZCMD --calendar --text="Choose your arrival dates"`

bags=`$ZCMD --entry --entry-text=0 1 2 3 4 5 --text="How many bags are you taking?"`

#Summary of the trip info
echo "Travelers name: $name"
echo "Departure City: $departCity"
echo "Arrival City: $arrivalCity"
echo "Departing Date: $departDate"
echo "Arrival Date: $arrivalDate"
echo "Number of Bags: $bags"

echo "$name|$departCity|$arrivalCity|$departDate|$arrivalDate|$bags" >> reservation.txt
sed -i .bak "/^$name|/d" reservation.txt









share|improve this question















I have a file that I store reservation into it and I am trying to remove the old reservation and put the new reservation if the same name appears before.
does this line of code do it for me in bash?



sed -i .bak "/^$name|/d" reservation.txt




#!/bin/bash


#start by building a zenity command
ZCMD='/bin/zenity/bin/zenity --title=PA5'
name=`$ZCMD --entry --text="Enter your Name"`

cities=("Portland" "Vancouver" "Seattle" "Riyadh" "Jeddah" ""Las Vegas"" ""New York"")
departCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you departing from?"`

arrivalCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you going to?"`

departDate=`$ZCMD --calendar --text="Choose your departure dates"`
arrivalDate=`$ZCMD --calendar --text="Choose your arrival dates"`

bags=`$ZCMD --entry --entry-text=0 1 2 3 4 5 --text="How many bags are you taking?"`

#Summary of the trip info
echo "Travelers name: $name"
echo "Departure City: $departCity"
echo "Arrival City: $arrivalCity"
echo "Departing Date: $departDate"
echo "Arrival Date: $arrivalDate"
echo "Number of Bags: $bags"

echo "$name|$departCity|$arrivalCity|$departDate|$arrivalDate|$bags" >> reservation.txt
sed -i .bak "/^$name|/d" reservation.txt






bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 5:38

























asked Nov 22 at 3:59









Rawad Bader

42




42








  • 1




    Welcome to StackOverflow! You could try to execute that yourself and see if it's doing it for you. Then if the result is not as what you expected, include both current and expected results, and sample data in your question.
    – Andreas
    Nov 22 at 4:29












  • That will remove all lines that begin with the name. It doesn't add a new reservation, and doesn't check whether the same name appears before it.
    – Barmar
    Nov 22 at 4:39










  • Okay, how I can replace the old name reservation with a new reservation? if you want to see my code I can post it here?
    – Rawad Bader
    Nov 22 at 4:46












  • s/$name|/new reservation here/ but then you have the opposite problem; if an old reservation doesn't exist, it won't create the new one either. Are you really married to sed for this task?
    – tripleee
    Nov 22 at 6:29










  • No, I can use different the "sed" but I do not know what to use?
    – Rawad Bader
    Nov 22 at 7:52














  • 1




    Welcome to StackOverflow! You could try to execute that yourself and see if it's doing it for you. Then if the result is not as what you expected, include both current and expected results, and sample data in your question.
    – Andreas
    Nov 22 at 4:29












  • That will remove all lines that begin with the name. It doesn't add a new reservation, and doesn't check whether the same name appears before it.
    – Barmar
    Nov 22 at 4:39










  • Okay, how I can replace the old name reservation with a new reservation? if you want to see my code I can post it here?
    – Rawad Bader
    Nov 22 at 4:46












  • s/$name|/new reservation here/ but then you have the opposite problem; if an old reservation doesn't exist, it won't create the new one either. Are you really married to sed for this task?
    – tripleee
    Nov 22 at 6:29










  • No, I can use different the "sed" but I do not know what to use?
    – Rawad Bader
    Nov 22 at 7:52








1




1




Welcome to StackOverflow! You could try to execute that yourself and see if it's doing it for you. Then if the result is not as what you expected, include both current and expected results, and sample data in your question.
– Andreas
Nov 22 at 4:29






Welcome to StackOverflow! You could try to execute that yourself and see if it's doing it for you. Then if the result is not as what you expected, include both current and expected results, and sample data in your question.
– Andreas
Nov 22 at 4:29














That will remove all lines that begin with the name. It doesn't add a new reservation, and doesn't check whether the same name appears before it.
– Barmar
Nov 22 at 4:39




That will remove all lines that begin with the name. It doesn't add a new reservation, and doesn't check whether the same name appears before it.
– Barmar
Nov 22 at 4:39












Okay, how I can replace the old name reservation with a new reservation? if you want to see my code I can post it here?
– Rawad Bader
Nov 22 at 4:46






Okay, how I can replace the old name reservation with a new reservation? if you want to see my code I can post it here?
– Rawad Bader
Nov 22 at 4:46














s/$name|/new reservation here/ but then you have the opposite problem; if an old reservation doesn't exist, it won't create the new one either. Are you really married to sed for this task?
– tripleee
Nov 22 at 6:29




s/$name|/new reservation here/ but then you have the opposite problem; if an old reservation doesn't exist, it won't create the new one either. Are you really married to sed for this task?
– tripleee
Nov 22 at 6:29












No, I can use different the "sed" but I do not know what to use?
– Rawad Bader
Nov 22 at 7:52




No, I can use different the "sed" but I do not know what to use?
– Rawad Bader
Nov 22 at 7:52












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The bug is that you add a reservation and then immediately remove it. You should reverse the order of the last two commands: First delete any existing reservation, then add the new one.



Maybe make sure that you use the -i option correctly -- on some platforms you cannot have a space between -i and .bak (while on others it is mandatory).






share|improve this answer























  • Okay, how can I do that?
    – Rawad Bader
    Nov 22 at 7:54










  • In your editor, delete the sed line and paste it above the echo line so that it comes last.
    – tripleee
    Nov 22 at 7:57












  • Okay, I did it but it still keeping the same name reservation. is not replacing the old reservation with the newer reservation for the same name
    – Rawad Bader
    Nov 22 at 8:09










  • I would like to overwrite the name if it appears in the text file already
    – Rawad Bader
    Nov 22 at 8:22










  • That's the net effect of deleting if it it exists and then adding it unconditionally. Adding and then deleting obviously removes the thing you just added.
    – tripleee
    Nov 22 at 8:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
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%2fstackoverflow.com%2fquestions%2f53423679%2fbash-script-a-line-of-code-i-am-trying-to-use%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








up vote
0
down vote













The bug is that you add a reservation and then immediately remove it. You should reverse the order of the last two commands: First delete any existing reservation, then add the new one.



Maybe make sure that you use the -i option correctly -- on some platforms you cannot have a space between -i and .bak (while on others it is mandatory).






share|improve this answer























  • Okay, how can I do that?
    – Rawad Bader
    Nov 22 at 7:54










  • In your editor, delete the sed line and paste it above the echo line so that it comes last.
    – tripleee
    Nov 22 at 7:57












  • Okay, I did it but it still keeping the same name reservation. is not replacing the old reservation with the newer reservation for the same name
    – Rawad Bader
    Nov 22 at 8:09










  • I would like to overwrite the name if it appears in the text file already
    – Rawad Bader
    Nov 22 at 8:22










  • That's the net effect of deleting if it it exists and then adding it unconditionally. Adding and then deleting obviously removes the thing you just added.
    – tripleee
    Nov 22 at 8:26















up vote
0
down vote













The bug is that you add a reservation and then immediately remove it. You should reverse the order of the last two commands: First delete any existing reservation, then add the new one.



Maybe make sure that you use the -i option correctly -- on some platforms you cannot have a space between -i and .bak (while on others it is mandatory).






share|improve this answer























  • Okay, how can I do that?
    – Rawad Bader
    Nov 22 at 7:54










  • In your editor, delete the sed line and paste it above the echo line so that it comes last.
    – tripleee
    Nov 22 at 7:57












  • Okay, I did it but it still keeping the same name reservation. is not replacing the old reservation with the newer reservation for the same name
    – Rawad Bader
    Nov 22 at 8:09










  • I would like to overwrite the name if it appears in the text file already
    – Rawad Bader
    Nov 22 at 8:22










  • That's the net effect of deleting if it it exists and then adding it unconditionally. Adding and then deleting obviously removes the thing you just added.
    – tripleee
    Nov 22 at 8:26













up vote
0
down vote










up vote
0
down vote









The bug is that you add a reservation and then immediately remove it. You should reverse the order of the last two commands: First delete any existing reservation, then add the new one.



Maybe make sure that you use the -i option correctly -- on some platforms you cannot have a space between -i and .bak (while on others it is mandatory).






share|improve this answer














The bug is that you add a reservation and then immediately remove it. You should reverse the order of the last two commands: First delete any existing reservation, then add the new one.



Maybe make sure that you use the -i option correctly -- on some platforms you cannot have a space between -i and .bak (while on others it is mandatory).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 8:52

























answered Nov 22 at 6:30









tripleee

87.1k12122177




87.1k12122177












  • Okay, how can I do that?
    – Rawad Bader
    Nov 22 at 7:54










  • In your editor, delete the sed line and paste it above the echo line so that it comes last.
    – tripleee
    Nov 22 at 7:57












  • Okay, I did it but it still keeping the same name reservation. is not replacing the old reservation with the newer reservation for the same name
    – Rawad Bader
    Nov 22 at 8:09










  • I would like to overwrite the name if it appears in the text file already
    – Rawad Bader
    Nov 22 at 8:22










  • That's the net effect of deleting if it it exists and then adding it unconditionally. Adding and then deleting obviously removes the thing you just added.
    – tripleee
    Nov 22 at 8:26


















  • Okay, how can I do that?
    – Rawad Bader
    Nov 22 at 7:54










  • In your editor, delete the sed line and paste it above the echo line so that it comes last.
    – tripleee
    Nov 22 at 7:57












  • Okay, I did it but it still keeping the same name reservation. is not replacing the old reservation with the newer reservation for the same name
    – Rawad Bader
    Nov 22 at 8:09










  • I would like to overwrite the name if it appears in the text file already
    – Rawad Bader
    Nov 22 at 8:22










  • That's the net effect of deleting if it it exists and then adding it unconditionally. Adding and then deleting obviously removes the thing you just added.
    – tripleee
    Nov 22 at 8:26
















Okay, how can I do that?
– Rawad Bader
Nov 22 at 7:54




Okay, how can I do that?
– Rawad Bader
Nov 22 at 7:54












In your editor, delete the sed line and paste it above the echo line so that it comes last.
– tripleee
Nov 22 at 7:57






In your editor, delete the sed line and paste it above the echo line so that it comes last.
– tripleee
Nov 22 at 7:57














Okay, I did it but it still keeping the same name reservation. is not replacing the old reservation with the newer reservation for the same name
– Rawad Bader
Nov 22 at 8:09




Okay, I did it but it still keeping the same name reservation. is not replacing the old reservation with the newer reservation for the same name
– Rawad Bader
Nov 22 at 8:09












I would like to overwrite the name if it appears in the text file already
– Rawad Bader
Nov 22 at 8:22




I would like to overwrite the name if it appears in the text file already
– Rawad Bader
Nov 22 at 8:22












That's the net effect of deleting if it it exists and then adding it unconditionally. Adding and then deleting obviously removes the thing you just added.
– tripleee
Nov 22 at 8:26




That's the net effect of deleting if it it exists and then adding it unconditionally. Adding and then deleting obviously removes the thing you just added.
– tripleee
Nov 22 at 8:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2fstackoverflow.com%2fquestions%2f53423679%2fbash-script-a-line-of-code-i-am-trying-to-use%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

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu