Im trying to make a script that simply adds a word to the last available line in a txt file without overwriting any previous lines.
Ive googled this and there are great examples but no one explain what each function does, and i dont entirely understand how it works.
Basically Im looking for someone to show me the correct format, and tell me how it works. Im going to use this as a basis to write more complex shell scripts in the future.
It would be helpful to know what you found, and what you intend to use. Very difficult to explain how something works if you don't post it.
Assuming you are writing Kshell or bash based scripts, this snipit of code will append the word 'foo' to the last line in the file. It assumes the name of the file is passed in on the command line:
The sed programme reads the file ($1). For each line that is not the last line ($!b) it branches to the end of the programme which by default prints the line. When the last line is encountered, the end of the line is substituted with the word "foo" preceded by a space. The line is then printed.
Depending on your version of sed, you will need to have a hard newline after the b. If you don't it will interpret the next token as the target of the branch. There might be a work around for this, but I am not aware of it.
The output of the sed command is written to a temporary file. If the sed is successful ($? contains the exit code; 0 is good) then the temp file is moved onto (replaces) the original file. If the sed errors, then the temp file is removed and an error is written.
Would it be possible instead of replacing it with a pre defined name, have it choose a random name from a txt file or just specified in the script that it puts onto the last line.
Would it be possible instead of replacing it with a pre defined name, have it choose a random name from a txt file or just specified in the script that it puts onto the last line.
For example instead of adding the word foo to the last line of the txt fiel which is what i initiially wanted i want to have a list of words that it can choose from.
foo
hello
cat
cow
pineapple
The script chooses a random word from the above and adds it to the last line of the text file instead of just having it add a one flat word every time. I dont know where the words would be stored, i suppose it would read them from a txt file o the random list would be in the script itself.
This line will add the value of var variable to the end of last line in file
For the rest of your assignment, you should work yourself using your favorite shell howto read a file to array and pick a "random" array element for substitution.
Hi everyone, first time visitor to these forums here.
Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a... (4 Replies)
Hello,
This is basic (i think). I am trying to run a shell script which would go into each folder (folder names defined in the list) and after entering would run some commands, once done, come out of the folder and continue until the list ends.
Pretty basic and there are bunch of example online. ... (9 Replies)
I need to have a script read a file that has a list of words in a single column like below:Black
Blue
Brown
Orange
Red
Yellow
Green
White
Purple
Silver
Grey
Tan
Then print to another file just all of the two-word possible combinations. Example:
Black,Blue
Anyone want to take a... (4 Replies)
Hey everyone, just now joined because I didn't want to go onto Ubuntu forums and start asking about how to write shell scripts. Seems like this is a pretty active forum for exactly what I need.
I'm trying to modify a shell script I found online, the end goal is to have it find all files in the... (9 Replies)
Hi,
A basic sed question.
I have a set of files. In each file there is a number that I want replaced. For example, if I run sed I should get the following:
% cat test2.txt
#goofy//171.00
goofy 171.00
% sed -i 's/171/xxx/g' test2.txt
% cat test2.txt
#goofy//xxx.00
goofy xxx.00
... (2 Replies)
Lets say I wanted to create a script that would show what people are doing on my machine using the w command and refresh in about 6 seconds. What would be the easiest way to do this? I pretty much want the script to loop until I stop it.
I'm using the BASH shell by the way.
Help is appreciated.... (1 Reply)
hi,
I am new script learner,
so my basic doubt is ,
how to store value of any command in a variable example
$ ls | wc -l
i want to stote the output of this in a variable c.
so that i can use c in if else loop.
and when do we use " ` " symbol in script..
can anyone also tell for... (5 Replies)
Hi All,
I am new to shell scripting.
I have a variable which holds a numeric value.I have to check whether this variable holds a value between(0- 8),(8-17)(17-24).How do i write this syntax using if in shell scripting.
Thanks
Vignesh (2 Replies)
im trying to run the below if command
ifconfig -a |grep 10.100.120.21
gives me below output
inet addr:10.100.120.21 Bcast:10.100.120.255 Mask:255.255.255.0
i just want a basic shell which says if above exists then continue
how would i do this? (6 Replies)