Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1027965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:18:51+00:00 2026-05-16T12:18:51+00:00

I have few long commands that I will be using on a day to

  • 0

I have few long commands that I will be using on a day to day basis. So I though it would be better to have a bash script where I could pass arguments, thus saving typing. I guess this is the norm in Linux but I am kind of new to it. Could someone show me how to do it. A example is the following command

cut -f <column_number> <filename> | sort | uniq -c | 
sort -r -k1 -n | awk '{printf "%-15s %-10d\n", $2,$1}'

so i want this in a script where i can pass the filename and column number (preferably in any order) and get the desired ouput instead of having to type the whole thing everytime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:18:52+00:00Added an answer on May 16, 2026 at 12:18 pm

    Create a file say myscript.sh –

    #!/bin/bash
    if [ $# -ne 2 ]; then 
    echo Usage: myscript.sh column_number file_path
    exit
    fi
    
    if ! [ -f $2 ]; then
    echo File doesnt exist
    exit
    fi
    
    if [ `echo $1 | grep -E ^[0-9]+$ | wc -l` -ne 1 ]; then
    echo First argument must be a number
    exit
    fi
    
    cut -f 10  $1 $2 | sort | uniq -c | 
    sort -r -k1 -n | awk '{printf "%-15s %-10d\n", $2,$1}'
    

    Make sure this file is executable using command chmod +x mytask.sh

    You can invoke it like sh myscript.sh 30 myfile.sh or ./myscript.sh 30 myfile.sh

    The first line of above script specifies the shell you would like your script to be executed in. $1 and $2 refer to the first and second command line arguments.

    About argument validity checks:

    1. First check ensures that there are exactly two arguments passed to the script.
    2. Second check ensures the file pointed by the argument two is existing
    3. Third check ensures that the number passed as first argument is really a number. It uses regular expression for that purpose. May be someone provide a better replacement for this check but this is what came to my mind instantly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few lines of PowerShell code that I would like to use
I have a few Visual Studio Solutions/Projects that are being worked on in my
We have a few projects that involve building an application that is composed of
I have a list/queue of 200 commands that I need to run in a
I have a Perl script that sets up variables near the top for directories
I have a script that runs on the command line, called by a crontab.
how to stop executing code in thread if takes to long. I have few
I have a script that appends some rows to a table. One of the
I have few asynchronous tasks running and I need to wait until at least
I have few different applications among which I'd like to share a C# enum.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.