Question

(COMMAND LINE) I have a file, abc.csv and I am interested in a command line solution...

(COMMAND LINE) I have a file, abc.csv and I am interested in a command line solution (not something like C coding) for the following:

In which zip code was the highest number of narcotic/drug offenses.

ZIP_CODE and ABC_CODE are the relevant headings but there are many headings and many records within the file. Within ABC_CODE, 35A and 35B are narcotic and drug offenses. So, I want to find out which zip code has the highest number of narcotic/drug offenses, which are denoted by only two of many possible codes within ABC_CODE.

What is a command line solution for this in bash ubuntu?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am using the below t.csv file as input for testing you can use yours

zipcode narcotics   
1 C
1 B
2 A
2 C
2 D

My command goes like this:

cat t.csv | awk '{a[$1]++}END{for (n in a) print n, a[n]}' | sort -k2,2r -k1 | head -1 | awk '{print $1}'

1)we output out csv (t.csv) using cat command and then we pipe it to

2)we use awk for the previous output to take the 1st i.e Zipcode coloum and finding out the Zipcode and their frequency count and pipe it to


3) sort the previous output by second coloumn(frequency) as we want highest frequency zipcode on top. r option is for reverse and k is to specify key


4) then we use the previous output and pipe it to head -1 which prints the 1st row which is our desired output

5)from here we are cutting out our filed name i.e the desired zipcode value

Add a comment
Know the answer?
Add Answer to:
(COMMAND LINE) I have a file, abc.csv and I am interested in a command line solution...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command...

    LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command line arguments and does NOT use the read command. I will give you no credit if your bash script includes a read command. Also, you need to have at least one for..in loop in your code even if there may be other ways to do this problem. The script will go through a list of entries in the current directory (do not process subdirectories):...

  • I am writing a C program that takes either command line arguments or has a file...

    I am writing a C program that takes either command line arguments or has a file passed to it using < on the command line. My question is how do I check to see if there is a file passed using <. I think I need a way to check stdin to see if there is any data present. Thanks

  • Hello! In this bash program, there is an error when i try to execute it. I am running it on Unix and I used emacs for the text editor it says: line 3: [0: command not found line 9: if[-f ]: command no...

    Hello! In this bash program, there is an error when i try to execute it. I am running it on Unix and I used emacs for the text editor it says: line 3: [0: command not found line 9: if[-f ]: command not found line 10: syntax error near unexpected token `then' line 10: `then' ------------------------------- #!/bin/bash if [ $# != 1 ] then echo "Usage: myScript.sh " exit 1 fi if [ -f $1 ] then awk ' BEGIN...

  • I need help with this. 22. Show a command that will create a file named "etc.tar"...

    I need help with this. 22. Show a command that will create a file named "etc.tar" in your home directory from recursively going through all of the files in the "/etc" directory. In other words, the "etc.tar" file will include the files in the "/etc" directory as well as all of its subdirectories. IMPORTANT: This command must work for any user as well as work from any directory. 23. In bash, multiple commands can appear on a single command line...

  • Question 2 0/1 point (graded) What happens when you remove a directory using the command rm...

    Question 2 0/1 point (graded) What happens when you remove a directory using the command rm -r? You cannot remove a directory using the rm command. You permanently remove the entire directory, including all files and subdirectories. You move the entire directory to a trash folder, but it can be restored later. You get a warning message asking if you want to proceed, then you delete the directory. incorrect Answer Incorrect: Try again. Unix does not warn you before permanently...

  • I am having trouble on what exactly to write so I can make the script work,...

    I am having trouble on what exactly to write so I can make the script work, can someone show how to properly do it? Use the vi editor to enter the following script (name this file testscript): #!/bin/bash #script to demonstrate : reading arguments from a command line & # interactively from within script # selection (if… then… else…. fi) # iteration (for loop) # USE FILES ENTERED ON THE COMMAND LINE echo The script you are running is $0...

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • 2. At the command prompt, type ls –l /dev/log and press Enter. What is the file type? Which daemo...

    2. At the command prompt, type ls –l /dev/log and press Enter. What is the file type? Which daemon on Ubuntu Server Linux uses this file and what is its purpose? 3. At the command prompt, type less /etc/rsyslog.conf and press Enter to view the configuration file for the System Log Daemon. Are there any entries that specify facilities, priorities, or log file locations? What does the last line of the file specify? Press q when finished to quit the...

  • LUNIX (Please Label) Exit vi (:q) and from the command line, type viscript4.sh. For this script,...

    LUNIX (Please Label) Exit vi (:q) and from the command line, type viscript4.sh. For this script, we will iterate through all files in the current directory print out their name using the for loop. Example (do not type yet): foriin*;do …;done where the…does some operation on$I, which stands for the current file. To do this, enter the following in your script4.sh file:           #!/bin/bash           for i in *; do                echo $i           done Once the above works, change the for loop to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT