Script to edit a text file on a Linux Redhat Server - linux

The task I am trying to achieve is to write a script which accesses a Red Hat Server, navigates to a certain directory, and adds things to a text file. How would I go about this task? what scripting language do I use? etc.
I don't have any experience in scripting languages, I'm only really an expert in Java applications and occasional C#.
Hope somebody can help. This would be extremely useful to me.

If you're just trying to append a line, you can use SSH (for the connection) and just concatenate to the end of the file like so:
echo "New line to text file" | ssh myserver.com 'cat >> /var/myfile.txt'
If you're trying to change the contents, then you'll need to download the file before running it through a utility such as sed or awk and then uploading it back to the server. scp can be used to securely download and upload the file, but describing sed or awk here is beyond the scope of a brief answer.

Related

Is there a Linux command line utility for getting random data to work with from the web?

I am a Linux newbie and I often find myself working with a bunch of random data.
For example: I would like to work on a sample text file to try out some regular expressions or read some data into gnuplot from some sample data in a csv file or something.
I normally do this by copying and pasting passages from the internet but I was wondering if there exists some combination of commands that would allow me to do this without having to leave the terminal. I was thinking about using something like the curl command but I dont exactly know how it works...
To my knowledge there are websites that host content. I would simply like to access them and store them in my computer.
In conclusion and as a concrete example, how would i copy and paste a random passage off the internet from a website and store it in a file in my system using only the command line? Maybe you can point me in the right direction. Thanks.
You could redirect the output of a curl command into a file e.g.
curl https://run.mocky.io/v3/5f03b1ef-783f-439d-b8c5-bc5ad906cb14 > data-output
Note that I've mocked data in Mocky which is a nice website for quickly mocking an API.
I normally use "Project Gutenberg" which has 60,000+ books freely downloadable online.
So, if I want the full text of "Peter Pan and Wendy" by J.M. Barrie, I'd do:
curl "http://www.gutenberg.org/files/16/16-0.txt" > PeterPan.txt
If you look at the page for that book, you can see how to get it as HTML, plain text, ePUB or UTF-8.

How to edit a text file over a network with netcat plus some $EDITOR

(no alternate suggestions like vim --servername, or vim over ssh, or "just use ssh", or nfs, sshfs please. Also I prefer to avoid more elaborate incarnations of netcat like ncat)
I'd like to edit my text files that exist on another computer over a network, and know that it's not so straightforward using netcat. But I'm sure it's possible given a deeper understanding of netcat. I'd like to achieve that deeper understanding.
I know how to create my own REST service with netcat using pipes and fifo's. I also know you can expose your entire bash shell over netcat (yes it's dangerous), but it won't display the prompt. Yet it does display the output of a bash command back on the client.
What must be done to get a text editor (like vim, pico, nano, emacs) to work over a network for both reading and writing?
From vim doc:
Editing files over a network *new-network-files*
----------------------------
Files starting with scp://, rcp://, ftp:// and http:// are recognized as
remote files. An attempt is made to access these files with the indicated
method. For http:// only reading is possible, for the others writing is also
supported. Uses the netrw.vim script as a standard "plugin". |netrw|
Another tutorial: http://vim.wikia.com/wiki/Edit_remote_files_locally_via_SCP/RCP/FTP
# open a remote file
vim scp://remote-computer:22//tmp/file.txt
# ... EDIT ...
# write it back (run in vim)
:Nwrite

Associate one File to another File in linux

We have application which is written in Python in linux enviromet which is legacy code.I have task to document which help other in wiki page. I thought, Can I add one file into Existing code. So I can add documentation into another File. So if user open file it will pop us with code detail. if user does not want to see that file they can close this File. for Example
sample.py ( A File which contain code)
demo.txt ( which contain documentation file)
So if I open sample.py using vi like vi sample.py than demo.txt open by default like pop us or simple way which contain documentation part. I think it is like windows tool when we open tool it pop us infront of user. it is my idea I am not sure really it is valid or not. I have searched but could not find any useful information.
What you want to do will most likely confuse the user. If one types vi sample.py one expects to read this file. Now what you can do as #HAL said in his comment is add a comment in your code saying that the documentation for this code is in the demo.txt accessible at /path/to/demo.txt.
I think this is the common way to do it, and you will probably frustrate the user if you don't do it this way. (at least I would be ;) )
I am not aware of any Linux specific include functionality and I do not think that it exists, because the program you are executing is vi and not the Linux kernel. But editors support editor specific functions for hyper links. Here is an example for vim. vim: Add clickable label
Another way would be to replace vi with a shell script which does what you want. This is an example:
#! /bin/bash
if [ "$1" = "sample.py" ] ; then
vim demo.txt "$1"
fi
This will open the documentation together with the program in two vim buffers.

Trying to extract field from browser page

I'm trying to extract one field from Firefox to my local Ubuntu 12.04 PC and Mac OS 19.7.4 from an online form
I can manually save the page locally as a text document and then search for the text using Unix script but this seems rather cumbersome & I require it to be automated. Is there another more efficient method?
My background is on Macs but the company is trialling Linux PC's, so please be tolerant of my relevant Ubuntu ignorance.
If you mean to program something try
WWW:Mechanize library, it have python and perl bindings,
several mousescripting engines in lunux, (actionaz)
test automation tool which works with firefox (Selenium)
You can do it by simple BASH script.
Take a look at some useful stuff like:
wget
sed
grep
and then nothing will by cumbersome and everything can go automatic.
If you want to go with the method that you mentioned, you can use curl to automate the saving of the form. Your BASH script would then look something like this:
curl http://locationofonlineform.com -o tempfile
valueOfField=$(grep patternToFindField tempfile)
// Do stuff
echo $valueOfField
If you want to get rid of the temporary file, you can directly feed the result of curl into the grep command.

Command Shell for a mobile phone

I am working on a project for builiding a shell for a java enabled mobile phone(using J2ME).I have to demonstarte 3 commands 1) ls 2) top 3)cat .How to implement them?Plese give me some ideas.Thanking you.
You can use JSR 75 [File Connection API] to list out the files, which can be used to implement 'ls' command and using the same, you can implement the 'cat' command also.
However beware of the permissions, your app needs to be signed to be effective, otherwise for each subsequent requests to the file-system, the OS will be asking the user for permission, destroying the concept of a shell!
'top' can't be implemented in J2ME
Sample for File Connection API: File Sample
Download Coreutils (it's the package which contains all of these commands): http://ftp.gnu.org/gnu/coreutils/coreutils-8.9.tar.gz.
Rip it open, and find the source code for each one. I think the tarball is organized quite nicely.
And some implementations of the commands in other languages (I like Python, as it's readable):
ls: http://www.pixelbeat.org/talks/python/ls.py.html.
cat: http://www.unix.com/302420159-post2.html.
top is complicated. Good luck implementing it!

Resources