WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Edit/write in file  (Read 6791 times)

Offline Aladin

  • Newbie
  • *
  • Posts: 40
Edit/write in file
« on: March 31, 2020, 07:04:31 PM »
how to edit,save, add in  file ?

Offline fangis

  • Newbie
  • *
  • Posts: 41
    • rafalis2
Re: Edit/write in file
« Reply #1 on: June 24, 2020, 08:55:27 PM »
type:
vi

Offline PDP-8

  • Hero Member
  • *****
  • Posts: 915
Re: Edit/write in file
« Reply #2 on: July 27, 2020, 02:51:20 AM »
What if you don't know vi?  And the gui editor is borked for some reason?
(um, so I have time on my hands like all of us....)

Get your inner OG on and not use a dedicated editor at all! 

Let's redirect the standard input (keyboard) to a file instead of the standard output (the screen):

Code: [Select]
cat > shoppingList
meat
cheese
milk
energy drink
pizza
yerba-mate
air freshener
espresso (ground)
tickets to prog-rock revival
<CTRL-D>

Use the keyboard as you would think for CTRL-D, rather than typing it in as seen in the last line.

This acts like a line-editor, so if you blow it, you'll have to start over and type it all in again.  But while you are on a line, you can backspace, and CTRL-U will erase the whole line before you hit return.

So yeah, you aren't going to write your phd thesis with this.

Oops, forgot - need to add birdseed to that list.  Let's just append it to the end with two redirection symbols:

Code: [Select]
cat >> shoppingList
birdseed
<CTRL-D>

If you ls you'll see the file there.

How about something more practical?

You just totally blew out your .Xdefaults file with a wrong parameter.  None of your normal editors work.

Man, cat as an editor is not interactive, and I'm not about to type all that in manually.

Let's say you need to urgently change the foreground character color in Aterm.  Fortunately, you can add duplicate parameters to .Xdefaults, and when it notices a duplicate, it just uses the last one found.

Code: [Select]
cat >> .Xdefaults
Aterm*foreground: green
<CTRL-D>

So we just appended the foreground paramater to the end of the .Xdefaults file, overriding the original line for it way up above.

If you play with this a little, you may find yourself reaching for cat as a simple line editor for tiny config files faster than any other editor.

That's a UNIX book! - cool  -- Garth

Offline PDP-8

  • Hero Member
  • *****
  • Posts: 915
Re: Edit/write in file
« Reply #3 on: July 27, 2020, 03:35:03 AM »
Q: you told me that the standard input is the keyboard, and the standard output is the screen.  And to make a file out of what I type, I have to redirect it with a > character, and stop the file with ctrl-d.

So why am I still seeing stuff on the screen?

A: So you aren't typing in the blind, characters are echoed to the screen but the file itself isn't.  Huh?

Try using no file redirection whatsoever:

Code: [Select]
cat
This is crazy  <--- you type
This is crazy  <--- computer responds
Wait, what the heck is
Wait, what the heck is
like a little kid repeating everything you say
like a little kid repeating everything you say
Jane, how do I stop this crazy thing?
Jane, how do I stop this crazy thing?
<CTRL-D>

Done.  Computer did exactly what you asked of it.  By not specifying any files, it used the defaults (keyboard and screen) and annoyed you instead.

CTRL-D "wrote the file", but since there is no file redirection specified, and the screen doesn't have memory of it's own, nada - zip.  It just closes.

Just got a little bit of typing experience.  I used to use this whole cat thing to get those interested in becoming one with the machine, rather than just blindly typing stuff in they saw online or in books.

That's a UNIX book! - cool  -- Garth

Offline fangis

  • Newbie
  • *
  • Posts: 41
    • rafalis2
Re: Edit/write in file
« Reply #4 on: October 03, 2020, 08:30:17 PM »
hi
It was a double surprise to see this reply, because
1. there was a reply at all
2. length of your reply.

I want to add that there is also this way

cat >> test << eof
>rice
>beans
>grape juice
>picanha
>eof

And the file gets saved without ctrl d. very useful if you dont have a ctrl key, or it got somehow broken. or in an incomplete, non interactive shell