Production Log Analysis or File Search or Replace Commands in Linux
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What are the usual and best commands to be used for the following in linux production machine?
#1 - Find last 5 occurrences of a given string
#2 - Find a last occurrence only of a given string
#3 - Find the 1st occurrence only of a given string
#4 - Find the nth occurrence of a given string. ex: 10th or 12th or any specific occurrence of a given string
#5 - How to replace the last 5 occurrences of a given search string and replaceable string
#6 - How to replace the last occurrence only of a given search string and replaceable string
#7 - How to replace the first occurrence only of a given search string and replaceable string
#8 - Find the nth occurrence of a given search string and replaceable string. ex: 10th or 12th or any specific occurrence of a given search string and replaceable string
#9 - How to search for a file in the linux environment
#10 - How to search for a string in all the files in the linux environment
#11 - Any alternative of executing commands when we don't have sudo access
#12 - Commands to navigate the log files quickly - up/down/scroll the text or log files
#13 - Commands to save the file and to take a backup of a file
If any item has been missed in the above, please add and let me know the command to be used
Note: all commands will be monitored in the production environment and we need to use the best practice
Thanks
This is surely not a "Beginning Java" question so I have moved it out of that forum.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
find for finding files
sed for replacing things in files
less, vim, emacs, etc for viewing file contents
All of those things will most likely already be available on your remote system so you can get started right away with experimenting and learning how they work.
Tim Driven Development | Test until the fear goes away
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can you please help/share me the commands with sed to solve the above replace need of #5 to #8?
-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can you please help/share me the commands with sed to solve the above replace need of #5 to #8?
What have you tried that didn't work? You need to show some effort instead of expecting people to do everything for you. You'll learn better by experimenting than by reading how something is done.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Moores wrote:You'll learn better by experimenting than by reading how something is done.
I totally agree - spend the time to understand how to use the tools rather than being dependent on recipes provided by others.
Everything you are wanting to do and much more can be solved using a combination of grep, sed, awk, and cut (find can be very useful as well).
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Ron McLeod wrote:Everything you are wanting to do and much more can be solved using a combination of grep, sed, awk, and cut (find can be very useful as well).
Add head, tail, cat, tac, and sort to the list. They are all pretty simple to use and shouldn't take long to master.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Chris Mary wrote:Tim Cooke,
Can you please help/share me the commands with sed to solve the above replace need of #5 to #8?
We've all given you some tools and general guidance that you can use as a starting point on your journey of learning. Why don't you take it as a challenge to answer each of your questions yourself by learning just enough of each tool to achieve it? That's pretty much how I've learned most things over time.
Tim Driven Development | Test until the fear goes away
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Driven Development | Test until the fear goes away
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
#1 - Find last 5 occurrences of a given string
grep -m NUM pattern file
tac file| grep -m5 someword
(or)
sudo grep someword file | tail -5
#2 - Find a last occurrence only of a given string
sudo grep someword file | tail -1
awk '/x/{flag = 1}; flag' file (x is the someword)
#3 - Find the 1st occurrence only of a given string
awk '/someword/{print NR;exit}' file
grep -n -m1 someword file | cut -d':' -f1
#4 - Find the nth occurrence of a given string. ex: 10th or 12th or any specific occurrence of a given string
grep -m NUM pattern file
tac file| grep -mNUM someword
(or)
sudo grep someword file | tail -NUM
#5 - How to replace the last 5 occurrences of a given search string and replaceable string
#6 - How to replace the last occurrence only of a given search string and replaceable string
sed '$ s/January/July/' Sales.txt
`sed` command will search the word ‘January‘ in the file and replace the last occurrence of this word with the word, ‘July‘.
#7 - How to replace the first occurrence only of a given search string and replaceable string
sed '0,/Apple/{s/Apple/Banana/}' input_filename
The first two parameters 0 and /Apple/ are the range specifier. The s/Apple/Banana/ is what is executed within that range. So in this case "within the range of the beginning (0) up to the first instance of Apple, replace Apple with Banana. Only the first Apple will be replaced.
#8 - Find the nth occurrence of a given search string and replaceable string. ex: 10th or 12th or any specific occurrence of a given search string and replaceable string
Second Occurence - sed -z 's/line/LINUX/2' mytextfile
Nth Occurence - sed -z 's/line/LINUX/NUM' mytextfile
========================================================
How to replace last 3 occurrences of a given string in a file ??
I couldn't find answer for this
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Driven Development | Test until the fear goes away
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Replace last 3 occurences is not found out yet
| Replace the word "snake" with "danger noodle" in all tiny ads. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









