LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2012, 12:14 PM   #1
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Rep: Reputation: 1
Using awk to cut fields and reduce dupes


First, let me provide some sample data.
Code:
Sam,,"Sam","I am"
Sam,,"Sam","I don't like"
Sam,,"Sam","Green eggs and ham"
Snitches,,"Snitches","I have a star"
Snitches,,"Snitches","You wish you had a star"
Snitches,,"Snitches","Lets pay the 3 dollars"
Basically I want to cut the first and fourth field out of the data eliminating any dupes in the first field. This is what the data should look like (formatting not an issue).
Sam "I am"
"I don't like"
"Green eggs and ham"

This is what I wrote and all I am getting is field 4. I think I need to add the 'else' into the 'if' statement, but I dont know how to go about it.

Code:
sort -d <file> | awk -F"," '{ if ($1 != last_name_seen) {print $4; last_name_seen=$1}}'
Any input would be appreciated.

S
 
Old 11-15-2012, 01:15 PM   #2
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
I tried this:

Quote:
sort -d <file> | awk -F"," '{ if ( $1 != last_name_seen ) { print $1, $4; last_name_seen=$1 } else { print $4 }}'
It did not work. It printed:
Quote:
Sam "I am"
Sam "I don't like"
Sam "Green eggs and ham"
As previously stated I want the data to look like this;
Quote:
Sam "I am"
"I don't like"
"Green eggs and ham"
Remember, the formatting is not the focus, only the output.

Last edited by smturner1; 11-15-2012 at 01:16 PM.
 
Old 11-15-2012, 04:40 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
You don't need an else, you just need to separate printing $1 (which you only want sometimes) from printing $4 (which you always want to do):

Code:
awk -F, '($1 != prev_name){printf("%s ", $1)} {print $4; prev_name = $1}'

## which is short for:
awk -F, '{
    if ($1 != prev_name)
       printf("%s ", $1);
    print $4;
    prev_name = $1;
   }'
 
Old 11-16-2012, 10:11 AM   #4
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
ntubski,

It works! Thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I cut fields with repeated delimiters? Mountain Linux - Software 4 11-19-2012 08:50 AM
need to cut fields smritisingh03 Linux - Newbie 1 01-25-2011 03:49 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
cut fields in a file christina_rules Linux - Newbie 12 07-15-2006 11:00 AM


All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration