Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Saturday, 12 February 2022

Get Current Location in Windows PowerShell and Linux

 When you are working on a Windows machine or Linux machine, you run your commands on a specific location. It is the location where you are presently inside, namely the current folder or directory.


How do you get the current location in Windows PowerShell?

Powershell uses the noun "Location"  to get the current directory or location. 

The command is "Get-Location"


Command:

 Get-Location


Output:

Path

----

C:\Users\chinm




How do you get the current location in Linux?

The command to get current location is : pwd  (present working directory)






Tuesday, 9 February 2021

Shell Script that accepts a positive integer and returns comma separated list of integers in descending order

Description: 
Write a  shell script that accepts exactly 1 argument which must be a positive integer. 
The script will print a comma-separated list of integers, all on the same line in descending order

Script: decreaseNum.sh

 

#!/bin/bash
#-----------------------------------------------
#user input
num=$1
temp=''
         
        #continue while input is greater than 0
 while [ $num -gt 0 ]
 do
                #append the numbers in decreasing order with comma-separated
  temp+="$num, "
  num=$(( $num - 1 ))
    done
 temp=$(echo $temp | sed 's/,$//')
 echo $temp


Output:








Thursday, 29 October 2020

Shell Script: find the 5 most frequent words in an article

Shell Script to find the 5 most frequent words in an article.


Article:

The company's rapid growth since incorporation has triggered a chain of products, acquisitions, and partnerships beyond Google's core search engine (Google Search). It offers services designed for work and productivity (Google Docs, Google Sheets, and Google Slides), email (Gmail), scheduling and time management (Google Calendar), cloud storage (Google Drive), instant messaging and video chat (Duo, Hangouts, Chat, and Meet), language translation (Google Translate), mapping and navigation (Google Maps, Waze, Google Earth, and Street View), podcast hosting (Google Podcasts), video sharing (YouTube), blog publishing (Blogger), note-taking (Google Keep and Google Jamboard), and photo organizing and editing (Google Photos). The company leads the development of the Android mobile operating system, the Google Chrome web browser, and Chrome OS, a lightweight operating system based on the Chrome browser. Google has moved increasingly into hardware; from 2010 to 2015, it partnered with major electronics manufacturers in the production of its Nexus devices, and it released multiple hardware products in October 2016, including the Google Pixel smartphone, Google Home smart speaker, Google Wifi mesh wireless router, and Google Daydream virtual reality headset. Google has also experimented with becoming an Internet carrier (Google Fiber, Google Fi, and Google Station)


let's store the text in a file named 'google.txt'


Command:

cat google.txt | tr '[ :upper: ]' '[ :lower: ]' | grep -oE '\w+' | grep -vE "and|has|of|the|has|with|an" | sort | uniq -c | sort -nr | head -n 5


Output:



cat - to display the content of the file

tr '[ :upper: ]' '[ :lower: ]'  - convert all uppercase to lowercase

grep - to find particular words

sort - sort the list of words here

uniq -c  - it provides the count of unique words 

head - it provides the top few lines from the list or file