Jump to content

Append text to a file without starting a new line?


Recommended Posts

I notice some posts were discussing similar topics but which didn’t end up with a solution satisfying my need.

 

I need to add new words into a file and later import the file into Anki. I think it would be best that, when the new words are added into a list, the sentence from which I read it can be added as well as the context, so that the word can be momerised most easily.

 

But „append text to a file“ that I used would list every added word in a new line. Except for adding the sentence manually, I am not aware of a way that I could add the word and the sentence in the same line with the seperation of a delimiter between them.

 

I tried using {clipboard:1} {query} in „Copy to Clipboard“ but which would make confusion when I don’t add a sentence occasionally, in which case the last wod in clipboard history would usurp the place of the current word as the Word, and the current word would be the Sentence.

 

So my question is if there is a way to „append a word into a file without starting a new line“, so that I can use two sets of Hotkey: 1) add a word and leave a Tab without starting a new line, 2) add the sentence after that Tab and starting a new line for the next word.

 

I would really appreciate if someone could help!

 

Attached are the current setting.

Bildschirmfoto 2022-01-16 um 00.40.34.png

Bildschirmfoto 2022-01-16 um 00.40.45.png

Bildschirmfoto 2022-01-16 um 00.40.56.png

Link to comment
4 hours ago, asphoa said:

So my question is if there is a way to „append a word into a file without starting a new line“,

 

Use echo -n "this is the text" >> /path/to/the/file.txt in bash or zsh.

 

-n tells echo not to add a newline, and >> means “add to the end of the file” (> means “replace contents of file”).

 

4 hours ago, asphoa said:

But „append text to a file“ that I used would list every added word in a new line.

 

Yes. It is absolutely standard behaviour to add a newline in such situations. Pretty much everything works that way by default. The idea is that the next task can just go right ahead and add to the file without having to check its contents first.

 

You also want to end every line with a newline; you just don't want to insert the whole line at once.

 

Personally, I'd consider re-working your workflow so it does insert the whole line at once. That way you won't be able to accidentally insert "word<tab>word<tab>" and screw up the file.

 

4 hours ago, asphoa said:

Attached are the current setting.

 

Please don't use screenshots as a way to show us a workflow. Upload the workflow somewhere (iCloud?) and post a link to it instead.

Link to comment

Thank you very much indeed for your help and detailed explanation, @deanishe!

 

I tried echo -n "this is the text" >> /path/to/the/file.txt in Terminal of Monterey, as follows:

 

echo -n “example” >> /Users/MYUSERNAME/Library/Mobile Documents/com~apple~CloudDocs/Wordlist/EN.txt

 

but nothing really happens in the designated file. Anyway, for registing huge amount of words & sentences, it doesn’t seem very feasible to use Command-line every time.

 

In fact, I’m quite ignorant with codes and such knowlege🥵.

 

The workflow has been uploaded to dropbox. For the simplicity, I deleted all other languages (for they are similar) and leave only two. The commands in “copy to Clipboard” respectively in EN and DE are different.

 

I just want two ways: 1) append something into the file without changing into a new line by one hotkey, and 2) append something and change into a new line by another hotkey.

 

So that I can choose to add a word first, and then add its sentence in the same line, or simply add a word and later on add another word in a line beneath it. Anki could in this case recognise the words with sentences and the words without sentences.

 

Would it be possible that you could have a look at the workflow I uploaded? ☺️

Link to comment
2 hours ago, asphoa said:

but nothing really happens in the designated file.

 

There’s a space in the path, which means you have to quote it. You have probably been writing to /Users/MYUSERNAME/Library/Mobile

 

The correct command would be:

 

echo -n "example" >> "/Users/MYUSERNAME/Library/Mobile Documents/com~apple~CloudDocs/Wordlist/EN.txt"

 

Note that those are regular quotes, not smart quotes, like in your post. You can't quote with smart quotes in a shell.

 

2 hours ago, asphoa said:

I just want two ways: 1) append something into the file without changing into a new line by one hotkey, and 2) append something and change into a new line by another hotkey.

 

The workflow's kinda broken. Can you describe in more detail what you want to happen?

 

You want to add what to a file? Current selection? Clipboard contents?

 

And you want a Hotkey to add the text followed by a tab, and another Hotkey to add the text followed by a newline?

Edited by deanishe
Link to comment

Indeed, it works now with regular quotes. Thanks for explaining!

 

What I want it to happen, ideally, is to add the currently selected text to a file in two ways:

 

1) when I select a word, by a Hotkey, append the selected word into a file; in the meantime, a trailing Tab is added after the word, but no new line is created, in other word, the position remains at the end of Tab;

 

2) and then, when I select a sentence, by another Hotkey, append the sentence after the above-mentioned Tab, so that the word and the sentence are in the same line only separated by that Tab. But now, the position goes to a new line, so next time when I append a new word, it will appear in a new line.

 

The file would look like this:

mellifluous[a Tab here]By the sauour of tho mellyfluous flowres alle the cytee was puryfyed.

Docetist[a Tab here]These Docetists..had a whole series of successors in the early church.

 

The first Hotkey append the words mellifluous & Docetist, the second Hotkey append the following sentences respectively.

 

Would this be possible at all, dear Deanishe?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...