Append herestring data to a file, if it doesn't already exist in that file (all in one line)

Multi tool use
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