awk questions using sort and grep


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions awk questions using sort and grep
# 1  
Old 10-18-2014
Computer awk questions using sort and grep

1. The problem statement, all variables and given/known data:
So i'll probably get told off for this but I have a few problems and rather than clog up the whole forum I'll post them here. Please bare in mind I am a complete novice when it comes to all this and so if you help please treat me like a complete idiot Smilie

I have a text file of names and test scores called class.txt e.g.

Code:
Bob Smith 15
James Jones 25
Emily Hunt 70

Then another text file with more detail called classdetails.txt e.g. (note the subject, date, first and surname, class, scores are separated by tabs, also the names are one field not two)

Code:
Maths     10/05/12     Bob Smith     Class 5      65:54:32:96
English   15/04/12     James Jones   Class 7      84:64:83:38
Science   12/07/13     Emily Hunt    Class 9      73:28:95:07

Then finally a text file called test.txt that simply contains

qwertyuiop

I have done half of the exercise and am really struggling with the rest of the questions. We are told to use awk from the command line,


6)Print all of the contents of the file (class.txt) and give a title to each column and separate out each column with a tab, send he output to another file.

7)Print out all of the students who have scored over 30 and redirect the output to sort to display alphabetically.

8)Print out all of the students and redirect the output to sort to display in order of their score.

9)Use colrm on a copy of the file test.txt to remove the last column.

10)Use colrm on a copy of the file test.txt to remove columns 3 to 5.

11)Print “testing” five times using awk.

12)Using the classdetails.txt file print col 5 and then sort the output in numerical order. Note this file is tab separated - hint use sort to sort the output from awk.

13)Create 4 files with the suffix .txt in your home directory. Create a sub-directory. Using awk and the UNIX shell move all of the .txt files to the sub-directory.


2. Relevant commands, code, scripts, algorithms:

I have a very basic knowledge of things like awk, grep, sort and pipes but other than that I'm clueless.

3. The attempts at a solution (include all code and scripts):

To print all of the contents of the file I have tried
Code:
awk ‘{print}' class.txt > class2.txt

and then perhaps edit the file with nano although I'm sure there is a better way.

Question 7 I imagine is largely similar to 6 except instead of the " > class2.txt" perhaps something involving pipes ??

Question 8 the same as above except using pipes to sort i.e. | sort -l

Question 9 and 10 I have no clue about colrm so again any help appreciated.

Question 12 will be similar to 6 I think

Question 13 No clue.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Northern Illinois University, DeKalb (IL), United States, Raymond Ege, CSCI330


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 10-18-2014
Welcome to the forums.. Some pointers:

@6) for printing titles:read up on the purpose of the BEGIN{} section in awk. I doubt editing with nano will be accepted
@7) sounds good
@8) read up on the various sort command line options ("man sort")
@9 and 10) see "man colrm"
# 3  
Old 10-18-2014
Really helpful mate, thanks a lot Smilie
# 4  
Old 10-18-2014
Quote:
13)Create 4 files with the suffix .txt in your home directory. Create a sub-directory. Using awk and the UNIX shell move all of the .txt files to the
sub-directory.
...
Question 13 No clue.
touch is the command that you can use to create the files.
mkdir the sub-dir
pipe a name list of the 4 .txt files using something like ls to awk and sh

It is unusual to use awk for this, but since it looks that it is focus on piping, here's a clue

Code:
ls *.txt | awk '{print "ls -l", $0} | sh

# 5  
Old 10-19-2014
question 6 is basically adding a header. you can use a special block called a BEGIN block in awk to to do. for example awk 'BEGIN { print "name\tid"; } 1 { print $0; }' class.txt

You've not shown much work on the rest.

awk works with fields. You seem to understand how to change the field separator. If you want to print every record where column 5 is greater than 30, you'd use something like awk '$5 > 30'

awk code follows the syntax condition { action }. The default condition is 1 (or always true) and the default action is print $0. Thus, the above example is equivalent to awk '$5 > 30 { print $0 }'.

Have a look at man sort and you will see how to sort on certain fields as well. Make sure to use -n for numeric sort where needed.

The questions involving making a directory and moving files shouldn't require awk. awk is a text processor. Unless those files are to be made with certain text or are part of the other questions output... In the shel you'd use mkdir and mv to make a directory and move files, respectively.
Moderator's Comments:
Mod Comment This post was originally submitted to a duplicate thread.

Last edited by Don Cragun; 10-19-2014 at 10:51 PM.. Reason: Add note.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

awk with Grep and Sort

1. The problem statement, all variables and given/known data: Please bare in mind I am a complete novice to this and have very very basic knowledge so please keep any answers as simple as possible and explain in terms I will understand ahha :):) I have a text file of names and test scores... (1 Reply)
Discussion started by: jamesb18
1 Replies

2. UNIX for Dummies Questions & Answers

Couple of questions wth grep/sort

I have different things that I was trying to do but am kind of struggling with this since I'm a Linux noob. The backround is that I have two files with student names in the same directory, and each file lists the student name, their major and their grade level. What is the most efficient way to... (6 Replies)
Discussion started by: tastybeer
6 Replies

3. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

4. Shell Programming and Scripting

Some questions about grep/awk

Hi guys. I need to filter some values from a number of log files. One of the files is: Interconnect Utilisation Results: Achieved Maximum Number of Concurrent Connections: 17 Statistics for Average Number of Concurrent Connections: Point Estimation: Confidence Interval: ... (2 Replies)
Discussion started by: Faaz0
2 Replies

5. Shell Programming and Scripting

history awk grep sort

can someone help me in the awk part...little confuse on that part. The problem is this: what input each utility gets and what it does with data and what output is provides to the next utility) history | awk '{a++}END{for(i in a){print a " " i}}' | sort -rn | grep '^' Thanks (4 Replies)
Discussion started by: Learnerabc
4 Replies

6. Shell Programming and Scripting

Grep questions

Hello All, I have few of questions related to Grep given below: 1. Like Perl, is it possible in Grep to negate characters in square brackets. For example in Perl, if '^' is used inside '' then it acts as a negation characters. Can same be achieved through Grep's regular expression. 2. How... (9 Replies)
Discussion started by: paragkalra
9 Replies

7. UNIX for Dummies Questions & Answers

Using Grep Questions

Hello All, 1.) I am searching for ".exe" in a text file 2.) I need to search for a hexadecimal entree of at least four digits (8 Replies)
Discussion started by: Omega1589
8 Replies

8. UNIX for Dummies Questions & Answers

Questions on GREP command

Hi, Is it possible to display a specific number of lines starting from a line having a particular text using grep command? e.g. I have a text file with the contents below: AAA BBB CCC DDD EEE FFF I want to display 3 lines starting with the line having "BBB" to get the result below:... (11 Replies)
Discussion started by: stevefox
11 Replies

9. UNIX for Dummies Questions & Answers

grep questions

I have the data file: A 1 2 3 BBB 4 5 6 A 7 8 9 I want to grep "A" then-skip a line-then-add two sublines: I my command: grep +3 "A" datafile (8 Replies)
Discussion started by: bobo
8 Replies

10. UNIX for Dummies Questions & Answers

Simple grep questions

Hi all, My boss wants me to find out how often e-m users are accessing their account:confused:. The mail server keeps log of all logins. I want to use grep the 'usernames', but it should come out the moment it first encounters the username in the log. Can I do that? I want to avoid 10+ greps... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question