0

I ask this question after trying all codes shared in the site about this without success,

  1. I have a file that contains a very long text, all the text is in the first line only

  2. I want to append long line with symbols to the end of this file, and my line look like this:

    [{\n"code":"final .net.wifi.WifiManager wifi \u003d\n "typeName":"","palette":"9","type":" ","spec":"Enable WiFi"}]'
    

I want to append this line that contains symbols such as \n and [{]".... to an existing file in the end of first line.

5
  • do you want special characters insert exactly as those are, or those should interpret to actual character like \n to actual newline not literal backslash-n \n characters? or same for unicode characters like \u003d Commented Sep 27, 2020 at 9:52
  • the \n is inside the line that i want append the file, this my problem, the \n always make my line separatly added in other next lines Commented Sep 27, 2020 at 9:55
  • i want to append the whole above code as its look, to the and of text in a file Commented Sep 27, 2020 at 9:57
  • Does your line contain single quotes '? There is one at the end of your example but it might by a typo. Commented Sep 27, 2020 at 10:16
  • not me who added the single quote at last, in my line there's no single quotes but the double quotes and many others symboles are there even \n and this refer to other line and break the operation Commented Sep 27, 2020 at 10:20

2 Answers 2

0

As your line is very complex and contains many escape characters I'd suggest using printf "%q" that some shells such as Bash support. You can use the following short script:

#!/usr/bin/env bash

read -r
printf "%q\n" "$REPLY"

Run it and paste your line:

$ ./printf-q.sh
[{\n"code":"final .net.wifi.WifiManager wifi \u003d\n "typeName":"","palette":"9","type":" ","spec":"Enable WiFi"}]'
\[\{\\n\"code\":\"final\ .net.wifi.WifiManager\ wifi\ \\u003d\\n\ \"typeName\":\"\"\,\"palette\":\"9\"\,\"type\":\"\ \"\,\"spec\":\"Enable\ WiFi\"\}\]\'

The last line is showed in the form you can use in shell without adding extra escape characters like that:

$ echo \[\{\\n\"code\":\"final\ .net.wifi.WifiManager\ wifi\ \\u003d\\n\ \"typeName\":\"\"\,\"palette\":\"9\"\,\"type\":\"\ \"\,\"spec\":\"Enable\ WiFi\"\}\]\' >> FILE
$ cat FILE
[{\n"code":"final .net.wifi.WifiManager wifi \u003d\n "typeName":"","palette":"9","type":" ","spec":"Enable WiFi"}]'
16
  • thanks so much bro for your hel and replying, the code look like complicated or hard for me as beginner can you tell me just what's the basic for using the last of your code for example where should i put this \ in my code i see it anywhere i want to know the basic of using this for i cant deal with another codes Commented Sep 27, 2020 at 10:39
  • Sorry but I cannot understand what you mean at all. Write your posts more carefully.. Commented Sep 27, 2020 at 10:42
  • sorry ! i mean, in your last example you have used echo command and in my line you use the \ \ \ \ how can i use that in another line, what is the secret of using this \ \ in the line Commented Sep 27, 2020 at 10:58
  • i mean where should i put the backslash, what i follow Commented Sep 27, 2020 at 11:00
  • As I said, you need to run printf-q.sh first and paste the line literally as shown in the example. The script will output the line that is properly escaped and can be used within shell. You don't have to add any extra backslashes. Commented Sep 27, 2020 at 12:13
0

Use a quoted here-document:

cat >>file <<'END'
[{\n"code":"final .net.wifi.WifiManager wifi \u003d\n "typeName":"","palette":"9","type":" ","spec":"Enable WiFi"}]'
END

This appends the contents of the here-document to the end of the file called file. Since the here-document is quoted, as is evident from the single quotes around the initial END, no expansions or filename globbing or other interpretation of the data will be done by the shell.

1
  • brother im using the command shell in android, so when i tried this code i got error can't create temporary file. because i don't have access to file system there's no other way to use this code avoiding create temp ? Commented Sep 27, 2020 at 11:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.