• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Production Log Analysis or File Search or Replace Commands in Linux

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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
Staff note (Paul Clapham) :

This is surely not a "Beginning Java" question so I have moved it out of that forum.

 
Saloon Keeper
Posts: 27485
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like you answered most of those questions yourself in your other post: https://coderanch.com/t/755086/os/Log-editor-unix-hard-drag
 
Chris Mary
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Tim, The above one is without editor.....All commands is required to do that without opening the files ???
 
Tim Holloway
Saloon Keeper
Posts: 27485
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most of the above can be done with the "grep" utility. Finding files based on name, change date or other external properties can be done with the "find" command. Backing up a file is up to you. A simple file copy can he used or a full-scale backup utility.
 
Sheriff
Posts: 5552
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
grep for finding things in files
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.
 
Chris Mary
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim Cooke,

Can you please help/share me the commands with sed to solve the above replace need of #5 to #8?
 
Saloon Keeper
Posts: 7548
176
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 4399
567
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ron McLeod
Marshal
Posts: 4399
567
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Tim Cooke
Sheriff
Posts: 5552
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 Cooke
Sheriff
Posts: 5552
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course if you are stuck on a specific problem then please come and ask for help making sure you've described clearly what you've done, what you are expecting to happen, and what is actually happening.
 
Tim Holloway
Saloon Keeper
Posts: 27485
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But please post them in the "Other Unix" forum. This stiff isn't even remotely related to Beginning Java.
 
Chris Mary
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone validates the following?

#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
 
Tim Cooke
Sheriff
Posts: 5552
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need us to validate them. You have a computer the same as we do to test them with.
 
Chris Mary
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant anything wrong...It's working..Just a review if anything is wrong...

Replace last 3 occurences is not found out yet
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic