Skip to main content

Posts

Showing posts with the label Sed linux

Bash slice string in Linux Shell

Strings are quite basic in any scripting or programming languages.If a journey to a thousand miles starts with a single step, in the programming world the journey starts with a string called “Hello World” and beyond “Hello World” pseudocode and algorithm will keep the journey going. Strings in scripting or programming are just group of characters.So, if string is a group of characters then it can be slice by character or a sub-string can be taken from the string.To slice a string in Bash, a syntax which is part of the Bash library can be used.Syntax is: ${string_to_be_sliced:slice_start_position:slice_end_position}Example string: slicethestring=“Hello Algorithm World”The string is stored in a variable: slicethestringTo get the sub-string “Algorithm World”.Code will be:echo ${slicethestring:6:23} #start the slice at position 6 till position 23Another way to slice the string from a specified start position till the end; is to tell Bash the start position and just omit the end position.O…

Linux find accessed and modified files

Finding accessed and modified files might be necessary at times to check or for audit purposes.
If files kept in a folder or directory has been accessed or modified but should not be the case then something dubious is going on. 
In Linux finding accessed and modified files can be done in a one liner command.
find /home -type f -amin -60 || -mmin -60 -print
Above command will find or show any files accessed within the last 60 minutes with the option "-amin" and it will show also the files modified within the last 60 minutes with the option "-mmin".
A shell script can be created and further processing can be done when files are detected.
The time can be adjusted if there's a need, but a more robust solution to check any accessed or modified files should be a file system watcher, but above command is quite helpful to check any activity that should not be occurring.

Cheers..till next time!================================Free Android Apps:
Click  links below to find out mor…

Linux sed remove duplicates and get unique values

Removing duplicates and getting unique values is quite easy provided that the input data follows a specific format, for example the string or raw data has spaces in between.
But a dilemma can occur if the data has no spaces in between the characters of the string, instead of spaces it is separated by dashes.
So, how to remove duplicates, get the unique values and still retain the format of the raw data?
Like this raw data: (just a sample string) the-quick-brown-fox-jumps-over-the-lazy-dog-jumps-over-the-lazy-cat-jumps-over-the-rabbit
When removing duplicates and getting unique values via this command:
sort duplicates.txt | uniq (this will work if the data is separated by spaces)
duplicates.txt assumes that it has the string as illustrated above.
Sample output:

The output will be the exactly be the same with the input. Why? It is because the whole string is treated as literal one string, because the dashes connect between the character eliminating the space delimiter.
Example, if the requiremen…