Using the Sort, Cut, and Awk commands


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using the Sort, Cut, and Awk commands
# 1  
Old 11-15-2008
Using the Sort, Cut, and Awk commands

Hello,

I am trying, utilizing the few commands I know, to extract all records within my file that were generated in November of 2007. Each record within the file has a "date" field giving the month, day, and year (9-8-88). How do I extract those records to their own file?

Once I extract those records I want to remove any records that are duplicates in another field. Please advise.

Thank you.

update: I'm leaving work in 10 minutes so hopefully someone can assist me. Thank you.

Last edited by babbabooey; 11-15-2008 at 01:52 AM..
# 2  
Old 11-15-2008
I'm leaving, but I will check this message board tomorrow.

Thanks.
# 3  
Old 11-15-2008
Please provide an example of the file. You could use awk to separate the month, day and year
Code:
echo 9-8-88|awk -F "-" '{print $2}'

That will print the second field separated by '-'. If you wish to print the other fields just use $1 or $3 instead. To print more than one field, "print $2,$3".
# 4  
Old 11-15-2008
Quote:
Originally Posted by redoubtable
Please provide an example of the file. You could use awk to separate the month, day and year
Code:
echo 9-8-88|awk -F "-" '{print $2}'

That will print the second field separated by '-'. If you wish to print the other fields just use $1 or $3 instead. To print more than one field, "print $2,$3".

Thank you for your response.

Here is a (hypothetical) example of what is in our file:

Bill Adams: ABC Contracting: 11-14-07: 6919408:314-463-2219
Bill Adams: ABC Contracting: 2-22-07: 4109663:314-463-2219
Bill Adams: ABC Contracting: 11-6-07: 6918997:314-463-2219
Steve Leitch: ERB Equipment: 11-6-07: 6918967:314-225-6403

There are thousands of records and I only want the ones from 11-07 and I only want the name and company of the contractor to be displayed once in my output.

Thanks again.
# 5  
Old 11-15-2008
Quote:
Originally Posted by babbabooey
...
Bill Adams: ABC Contracting: 11-14-07: 6919408:314-463-2219
Bill Adams: ABC Contracting: 2-22-07: 4109663:314-463-2219
Bill Adams: ABC Contracting: 11-6-07: 6918997:314-463-2219
Steve Leitch: ERB Equipment: 11-6-07: 6918967:314-225-6403

There are thousands of records and I only want the ones from 11-07 and I only want the name and company of the contractor to be displayed once in my output.

Thanks again.
Assuming the same file formation as the one shown above, and filtering is done based on the contractor's name, you can try:

Code:
  awk -F: '$3 ~ /11-[0-3]?[0-9]-07/ && !a[$1]++ { print $1, $2 > "Nov_2007" }' file

Code:
$  cat Nov_2007
Bill Adams  ABC Contracting
Steve Leitch  ERB Equipment

For a fancier output use printf.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sending output of program into subsequent commands (i.e. awk/sort)

Hi, I have identified how to use command chaining as per below on a file, to capture the header of a file, as well as the line containing the C: drive. $ cat test.txt Filesystem Size Used Avail Use% Mounted on rootfs 237G 153G 84G 65% / none 237G 153G 84G ... (6 Replies)
Discussion started by: sand1234
6 Replies

2. Shell Programming and Scripting

How to combing output of cut commands with a delimiter?

While looping through a file, I am cutting different length of characters (based on their length) like columns and want to produce the output in a separate file with different columns being separated by a comma. How to achieve this with an online command. I don't want to create multiple variables... (8 Replies)
Discussion started by: mady135
8 Replies

3. UNIX for Advanced & Expert Users

cut and sort

Please help. I have a file containing rows of information. The row needs to be broken down into blocks of 5 and then sorted. Example: 10381 1042010046 ... (4 Replies)
Discussion started by: Dolph
4 Replies

4. 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

5. Homework & Coursework Questions

Help with cut and sort

<B>andan100:Anders:Andersson:800101-1234:TNCCC_1:TDDB46 TDDB80:berbe101:Bertil:Bertilsson:800102-1234:TNCCC_1:TDDB46 TDDB80:The top is how it looks right now I want it t look like this under and I want it to be sorted. I have tried with cut -f -d studenter.txt and so on but it still doesnt work... (2 Replies)
Discussion started by: aannaann
2 Replies

6. Solaris

concatenate/sort/cut

I have the following requirement. 1. I have to concatenate the 10 fixed width files. 2. sort based on first 10 characters 3. after that i have remove first 10 chacters from the file. can you please tell me how to do it. Thanks in Advance Samba (1 Reply)
Discussion started by: samba
1 Replies

7. Shell Programming and Scripting

sort / cut question

Hi All, I have a small problem, hope you can help me out here. I have a file that contains the same format of lines in 99% of the cases. 906516 XYZ.NNN V 0000 20070711164648 userID1 userID2 hostname 20070711164641 There are unfortunately several lines with these... (5 Replies)
Discussion started by: BearCheese
5 Replies

8. Shell Programming and Scripting

using cut command with sort

how to cut for pattern in the file and then count each occurance? say, each line has unique pattern and u want to grep but at last, you want to see how many of them occur? say, cut -d'\" -f15 filename | sort -? or.. do i need to use sed or something.. i need to count lets say how... (5 Replies)
Discussion started by: hankooknara
5 Replies

9. UNIX for Dummies Questions & Answers

Help with Last,uniq, sort and cut

Using the last, uniq, sort and cut commands, determine how many times the different users have logged in. I know how to use the last command and cut command... i came up with last | cut -f1 -d" " | uniq i dont know if this is right, can someone please help me... thanks (1 Reply)
Discussion started by: jay1228
1 Replies

10. UNIX for Dummies Questions & Answers

Need Help with Awd/sort commands

Can anyone give me a hand with some questions by e-mailing me directly, so I can relay the questions inorder. I really would appreciate anyone that can help. Thank You in advance, I am online and will reply instantly. Charley <email removed> (5 Replies)
Discussion started by: flarebel
5 Replies
Login or Register to Ask a Question