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
bash
|
show 1 more comment
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
bash
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 tosed
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
|
show 1 more comment
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
bash
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
bash
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 tosed
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
|
show 1 more comment
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 tosed
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
|
show 1 more comment
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).
Okay, how can I do that?
– Rawad Bader
Nov 22 at 7:54
In your editor, delete thesed
line and paste it above theecho
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
|
show 5 more comments
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).
Okay, how can I do that?
– Rawad Bader
Nov 22 at 7:54
In your editor, delete thesed
line and paste it above theecho
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
|
show 5 more comments
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).
Okay, how can I do that?
– Rawad Bader
Nov 22 at 7:54
In your editor, delete thesed
line and paste it above theecho
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
|
show 5 more comments
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).
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).
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 thesed
line and paste it above theecho
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
|
show 5 more comments
Okay, how can I do that?
– Rawad Bader
Nov 22 at 7:54
In your editor, delete thesed
line and paste it above theecho
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
|
show 5 more comments
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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 tosed
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