Append herestring data to a file, if it doesn't already exist in that file (all in one line)
Clash Royale CLAN TAG#URR8PPP
up vote
-3
down vote
favorite
What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file?
This is my herestring append pattern:
cat >> /var/www/html/myFile <<< "myOutput"
shell-script text-processing control-flow here-string
add a comment |Â
up vote
-3
down vote
favorite
What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file?
This is my herestring append pattern:
cat >> /var/www/html/myFile <<< "myOutput"
shell-script text-processing control-flow here-string
See also Edit file based on existence of a string
â don_crissti
Jan 18 at 0:06
add a comment |Â
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file?
This is my herestring append pattern:
cat >> /var/www/html/myFile <<< "myOutput"
shell-script text-processing control-flow here-string
What will be an "elegant" one-line way to append a single line of data into the end of a file, with herestring, if this exact data isn't already in that file?
This is my herestring append pattern:
cat >> /var/www/html/myFile <<< "myOutput"
shell-script text-processing control-flow here-string
edited Jan 15 at 9:01
asked Jan 14 at 21:35
Arcticooling
83123
83123
See also Edit file based on existence of a string
â don_crissti
Jan 18 at 0:06
add a comment |Â
See also Edit file based on existence of a string
â don_crissti
Jan 18 at 0:06
See also Edit file based on existence of a string
â don_crissti
Jan 18 at 0:06
See also Edit file based on existence of a string
â don_crissti
Jan 18 at 0:06
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Assuming that you want "myOutput" to be a full line
at the last line of the file:
s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file
s="myOutput"
Set pattern to matchsed
use sed$ â¦' file
On the last line of the file/^'"$s"'$/b
Match the pattern as a whole line and branch to end if matched.s/$/n'"$s"'/
If no match, append the pattern at the end line.
If the file already contains the "myOutput" data
somewhere other than the last line,
this will add another copy of it at the end of the file.
(1)â¯Your answer doesnâÂÂt actually append the new line of text to the file (as the question requests and demonstrates) â you would need to usesedâ¯-i
.â (2)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:48
add a comment |Â
up vote
3
down vote
s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"
add a comment |Â
up vote
-1
down vote
In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.
As tested it worked fine
#!bin/bash
i="praveen"
f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
if [[ -z $f ]]
then
sed "$s/.*/&n$i/g" p.txt
else
echo "output exsists"
fi
(1)â¯The question says, âÂÂall in one lineâÂÂ.â YouâÂÂll notice that the earlier answers are one line long.â Yours is nine lines long (counting everything).â (2)â¯You tested this?â It doesnâÂÂt work AT ALL because the she-bang is wrong: there needs to be a/
at the beginning of the interpreter name (after the#!
).â (3)â¯s/$/nfoo/
(which isaacâÂÂs answer uses) is a somewhat awkward way to add a line of text (containing âÂÂfooâÂÂ) to a file insed
.âÂÂ(As IâÂÂm sure you understand,âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â a$
at the beginning of that command (before thes
) represents the last line of the file, and says âÂÂdo this at the end of the fileâÂÂ.)âÂÂs/.*/&nfoo/
(which your answer uses) is an unnecessarily complicated version of the same command.â But isaacâÂÂs answer uses that awkward technique because heâÂÂs trying to solve the problem economically (i.e., in one line) and heâÂÂs usingsed
already.âÂÂYour use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you donâÂÂt need to be usingsed
.âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â It would be much more natural to do what the OP is already doing (in the question):cat >>â¯"$file" <<<â¯"$i"
â or, better yet, something like StéphaneâÂÂs version:printf "%sn" "$i" >>â¯"$file"
.âÂÂ(4)â¯The question asks how to append a line of text to a file.âÂÂYour answer doesnâÂÂt even accomplish that (although, to be fair, isaacâÂÂs doesnâÂÂt either).âÂÂTo append a line of text to a file using your command, you would need to usesedâ¯-i
.âÂÂ(5)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:44
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Assuming that you want "myOutput" to be a full line
at the last line of the file:
s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file
s="myOutput"
Set pattern to matchsed
use sed$ â¦' file
On the last line of the file/^'"$s"'$/b
Match the pattern as a whole line and branch to end if matched.s/$/n'"$s"'/
If no match, append the pattern at the end line.
If the file already contains the "myOutput" data
somewhere other than the last line,
this will add another copy of it at the end of the file.
(1)â¯Your answer doesnâÂÂt actually append the new line of text to the file (as the question requests and demonstrates) â you would need to usesedâ¯-i
.â (2)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:48
add a comment |Â
up vote
1
down vote
accepted
Assuming that you want "myOutput" to be a full line
at the last line of the file:
s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file
s="myOutput"
Set pattern to matchsed
use sed$ â¦' file
On the last line of the file/^'"$s"'$/b
Match the pattern as a whole line and branch to end if matched.s/$/n'"$s"'/
If no match, append the pattern at the end line.
If the file already contains the "myOutput" data
somewhere other than the last line,
this will add another copy of it at the end of the file.
(1)â¯Your answer doesnâÂÂt actually append the new line of text to the file (as the question requests and demonstrates) â you would need to usesedâ¯-i
.â (2)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:48
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Assuming that you want "myOutput" to be a full line
at the last line of the file:
s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file
s="myOutput"
Set pattern to matchsed
use sed$ â¦' file
On the last line of the file/^'"$s"'$/b
Match the pattern as a whole line and branch to end if matched.s/$/n'"$s"'/
If no match, append the pattern at the end line.
If the file already contains the "myOutput" data
somewhere other than the last line,
this will add another copy of it at the end of the file.
Assuming that you want "myOutput" to be a full line
at the last line of the file:
s="myOutput"; sed '$ /^'"$s"'$/b; s/$/n'"$s"'/' file
s="myOutput"
Set pattern to matchsed
use sed$ â¦' file
On the last line of the file/^'"$s"'$/b
Match the pattern as a whole line and branch to end if matched.s/$/n'"$s"'/
If no match, append the pattern at the end line.
If the file already contains the "myOutput" data
somewhere other than the last line,
this will add another copy of it at the end of the file.
edited Jan 17 at 23:47
Scott
6,27132347
6,27132347
answered Jan 15 at 1:38
Isaac
6,7611834
6,7611834
(1)â¯Your answer doesnâÂÂt actually append the new line of text to the file (as the question requests and demonstrates) â you would need to usesedâ¯-i
.â (2)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:48
add a comment |Â
(1)â¯Your answer doesnâÂÂt actually append the new line of text to the file (as the question requests and demonstrates) â you would need to usesedâ¯-i
.â (2)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:48
(1)â¯Your answer doesnâÂÂt actually append the new line of text to the file (as the question requests and demonstrates) â you would need to use
sedâ¯-i
.â (2)â¯Your command doesnâÂÂt work when the text string contains a slash.â Scott
Jan 17 at 23:48
(1)â¯Your answer doesnâÂÂt actually append the new line of text to the file (as the question requests and demonstrates) â you would need to use
sedâ¯-i
.â (2)â¯Your command doesnâÂÂt work when the text string contains a slash.â Scott
Jan 17 at 23:48
add a comment |Â
up vote
3
down vote
s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"
add a comment |Â
up vote
3
down vote
s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"
add a comment |Â
up vote
3
down vote
up vote
3
down vote
s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"
s="myOutput"; grep -Fxqe "$s" < "$file" || printf "%sn" "$s" >> "$file"
answered Jan 14 at 22:54
Stéphane Chazelas
281k53518849
281k53518849
add a comment |Â
add a comment |Â
up vote
-1
down vote
In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.
As tested it worked fine
#!bin/bash
i="praveen"
f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
if [[ -z $f ]]
then
sed "$s/.*/&n$i/g" p.txt
else
echo "output exsists"
fi
(1)â¯The question says, âÂÂall in one lineâÂÂ.â YouâÂÂll notice that the earlier answers are one line long.â Yours is nine lines long (counting everything).â (2)â¯You tested this?â It doesnâÂÂt work AT ALL because the she-bang is wrong: there needs to be a/
at the beginning of the interpreter name (after the#!
).â (3)â¯s/$/nfoo/
(which isaacâÂÂs answer uses) is a somewhat awkward way to add a line of text (containing âÂÂfooâÂÂ) to a file insed
.âÂÂ(As IâÂÂm sure you understand,âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â a$
at the beginning of that command (before thes
) represents the last line of the file, and says âÂÂdo this at the end of the fileâÂÂ.)âÂÂs/.*/&nfoo/
(which your answer uses) is an unnecessarily complicated version of the same command.â But isaacâÂÂs answer uses that awkward technique because heâÂÂs trying to solve the problem economically (i.e., in one line) and heâÂÂs usingsed
already.âÂÂYour use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you donâÂÂt need to be usingsed
.âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â It would be much more natural to do what the OP is already doing (in the question):cat >>â¯"$file" <<<â¯"$i"
â or, better yet, something like StéphaneâÂÂs version:printf "%sn" "$i" >>â¯"$file"
.âÂÂ(4)â¯The question asks how to append a line of text to a file.âÂÂYour answer doesnâÂÂt even accomplish that (although, to be fair, isaacâÂÂs doesnâÂÂt either).âÂÂTo append a line of text to a file using your command, you would need to usesedâ¯-i
.âÂÂ(5)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:44
add a comment |Â
up vote
-1
down vote
In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.
As tested it worked fine
#!bin/bash
i="praveen"
f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
if [[ -z $f ]]
then
sed "$s/.*/&n$i/g" p.txt
else
echo "output exsists"
fi
(1)â¯The question says, âÂÂall in one lineâÂÂ.â YouâÂÂll notice that the earlier answers are one line long.â Yours is nine lines long (counting everything).â (2)â¯You tested this?â It doesnâÂÂt work AT ALL because the she-bang is wrong: there needs to be a/
at the beginning of the interpreter name (after the#!
).â (3)â¯s/$/nfoo/
(which isaacâÂÂs answer uses) is a somewhat awkward way to add a line of text (containing âÂÂfooâÂÂ) to a file insed
.âÂÂ(As IâÂÂm sure you understand,âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â a$
at the beginning of that command (before thes
) represents the last line of the file, and says âÂÂdo this at the end of the fileâÂÂ.)âÂÂs/.*/&nfoo/
(which your answer uses) is an unnecessarily complicated version of the same command.â But isaacâÂÂs answer uses that awkward technique because heâÂÂs trying to solve the problem economically (i.e., in one line) and heâÂÂs usingsed
already.âÂÂYour use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you donâÂÂt need to be usingsed
.âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â It would be much more natural to do what the OP is already doing (in the question):cat >>â¯"$file" <<<â¯"$i"
â or, better yet, something like StéphaneâÂÂs version:printf "%sn" "$i" >>â¯"$file"
.âÂÂ(4)â¯The question asks how to append a line of text to a file.âÂÂYour answer doesnâÂÂt even accomplish that (although, to be fair, isaacâÂÂs doesnâÂÂt either).âÂÂTo append a line of text to a file using your command, you would need to usesedâ¯-i
.âÂÂ(5)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:44
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.
As tested it worked fine
#!bin/bash
i="praveen"
f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
if [[ -z $f ]]
then
sed "$s/.*/&n$i/g" p.txt
else
echo "output exsists"
fi
In below script will check in complete last line for whether variable i present or not if not it inserts variable i in last line of file.
As tested it worked fine
#!bin/bash
i="praveen"
f=$(sed -n '$p' p.txt | sed -n "/^$i$/p")
if [[ -z $f ]]
then
sed "$s/.*/&n$i/g" p.txt
else
echo "output exsists"
fi
answered Jan 15 at 10:03
Praveen Kumar BS
1,010128
1,010128
(1)â¯The question says, âÂÂall in one lineâÂÂ.â YouâÂÂll notice that the earlier answers are one line long.â Yours is nine lines long (counting everything).â (2)â¯You tested this?â It doesnâÂÂt work AT ALL because the she-bang is wrong: there needs to be a/
at the beginning of the interpreter name (after the#!
).â (3)â¯s/$/nfoo/
(which isaacâÂÂs answer uses) is a somewhat awkward way to add a line of text (containing âÂÂfooâÂÂ) to a file insed
.âÂÂ(As IâÂÂm sure you understand,âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â a$
at the beginning of that command (before thes
) represents the last line of the file, and says âÂÂdo this at the end of the fileâÂÂ.)âÂÂs/.*/&nfoo/
(which your answer uses) is an unnecessarily complicated version of the same command.â But isaacâÂÂs answer uses that awkward technique because heâÂÂs trying to solve the problem economically (i.e., in one line) and heâÂÂs usingsed
already.âÂÂYour use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you donâÂÂt need to be usingsed
.âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â It would be much more natural to do what the OP is already doing (in the question):cat >>â¯"$file" <<<â¯"$i"
â or, better yet, something like StéphaneâÂÂs version:printf "%sn" "$i" >>â¯"$file"
.âÂÂ(4)â¯The question asks how to append a line of text to a file.âÂÂYour answer doesnâÂÂt even accomplish that (although, to be fair, isaacâÂÂs doesnâÂÂt either).âÂÂTo append a line of text to a file using your command, you would need to usesedâ¯-i
.âÂÂ(5)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:44
add a comment |Â
(1)â¯The question says, âÂÂall in one lineâÂÂ.â YouâÂÂll notice that the earlier answers are one line long.â Yours is nine lines long (counting everything).â (2)â¯You tested this?â It doesnâÂÂt work AT ALL because the she-bang is wrong: there needs to be a/
at the beginning of the interpreter name (after the#!
).â (3)â¯s/$/nfoo/
(which isaacâÂÂs answer uses) is a somewhat awkward way to add a line of text (containing âÂÂfooâÂÂ) to a file insed
.âÂÂ(As IâÂÂm sure you understand,âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â a$
at the beginning of that command (before thes
) represents the last line of the file, and says âÂÂdo this at the end of the fileâÂÂ.)âÂÂs/.*/&nfoo/
(which your answer uses) is an unnecessarily complicated version of the same command.â But isaacâÂÂs answer uses that awkward technique because heâÂÂs trying to solve the problem economically (i.e., in one line) and heâÂÂs usingsed
already.âÂÂYour use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you donâÂÂt need to be usingsed
.âÂÂâ¦â¯(ContâÂÂd)
â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â It would be much more natural to do what the OP is already doing (in the question):cat >>â¯"$file" <<<â¯"$i"
â or, better yet, something like StéphaneâÂÂs version:printf "%sn" "$i" >>â¯"$file"
.âÂÂ(4)â¯The question asks how to append a line of text to a file.âÂÂYour answer doesnâÂÂt even accomplish that (although, to be fair, isaacâÂÂs doesnâÂÂt either).âÂÂTo append a line of text to a file using your command, you would need to usesedâ¯-i
.âÂÂ(5)â¯Your command doesnâÂÂt work when the text string contains a slash.
â Scott
Jan 17 at 23:44
(1)â¯The question says, âÂÂall in one lineâÂÂ.â YouâÂÂll notice that the earlier answers are one line long.â Yours is nine lines long (counting everything).â (2)â¯You tested this?â It doesnâÂÂt work AT ALL because the she-bang is wrong: there needs to be a
/
at the beginning of the interpreter name (after the #!
).â (3)â¯s/$/nfoo/
(which isaacâÂÂs answer uses) is a somewhat awkward way to add a line of text (containing âÂÂfooâÂÂ) to a file in sed
.âÂÂ(As IâÂÂm sure you understand,âÂÂâ¦â¯(ContâÂÂd)â Scott
Jan 17 at 23:44
(1)â¯The question says, âÂÂall in one lineâÂÂ.â YouâÂÂll notice that the earlier answers are one line long.â Yours is nine lines long (counting everything).â (2)â¯You tested this?â It doesnâÂÂt work AT ALL because the she-bang is wrong: there needs to be a
/
at the beginning of the interpreter name (after the #!
).â (3)â¯s/$/nfoo/
(which isaacâÂÂs answer uses) is a somewhat awkward way to add a line of text (containing âÂÂfooâÂÂ) to a file in sed
.âÂÂ(As IâÂÂm sure you understand,âÂÂâ¦â¯(ContâÂÂd)â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â a
$
at the beginning of that command (before the s
) represents the last line of the file, and says âÂÂdo this at the end of the fileâÂÂ.)â s/.*/&nfoo/
(which your answer uses) is an unnecessarily complicated version of the same command.â But isaacâÂÂs answer uses that awkward technique because heâÂÂs trying to solve the problem economically (i.e., in one line) and heâÂÂs using sed
already.âÂÂYour use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you donâÂÂt need to be using sed
.âÂÂâ¦â¯(ContâÂÂd)â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â a
$
at the beginning of that command (before the s
) represents the last line of the file, and says âÂÂdo this at the end of the fileâÂÂ.)â s/.*/&nfoo/
(which your answer uses) is an unnecessarily complicated version of the same command.â But isaacâÂÂs answer uses that awkward technique because heâÂÂs trying to solve the problem economically (i.e., in one line) and heâÂÂs using sed
already.âÂÂYour use of it in a standalone command (the sixth line of your script) is doubly unnecessarily complicated and unintuitive, because, in that context, you donâÂÂt need to be using sed
.âÂÂâ¦â¯(ContâÂÂd)â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â It would be much more natural to do what the OP is already doing (in the question):
cat >>â¯"$file" <<<â¯"$i"
â or, better yet, something like StéphaneâÂÂs version: printf "%sn" "$i" >>â¯"$file"
.âÂÂ(4)â¯The question asks how to append a line of text to a file.âÂÂYour answer doesnâÂÂt even accomplish that (although, to be fair, isaacâÂÂs doesnâÂÂt either).âÂÂTo append a line of text to a file using your command, you would need to use sedâ¯-i
.âÂÂ(5)â¯Your command doesnâÂÂt work when the text string contains a slash.â Scott
Jan 17 at 23:44
(ContâÂÂd) â¦â It would be much more natural to do what the OP is already doing (in the question):
cat >>â¯"$file" <<<â¯"$i"
â or, better yet, something like StéphaneâÂÂs version: printf "%sn" "$i" >>â¯"$file"
.âÂÂ(4)â¯The question asks how to append a line of text to a file.âÂÂYour answer doesnâÂÂt even accomplish that (although, to be fair, isaacâÂÂs doesnâÂÂt either).âÂÂTo append a line of text to a file using your command, you would need to use sedâ¯-i
.âÂÂ(5)â¯Your command doesnâÂÂt work when the text string contains a slash.â Scott
Jan 17 at 23:44
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417117%2fappend-herestring-data-to-a-file-if-it-doesnt-already-exist-in-that-file-all%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
See also Edit file based on existence of a string
â don_crissti
Jan 18 at 0:06