LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2010, 08:58 PM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
Awk (lower case, concatenate, & excluding matches by 'skipping'')


I have a file like below. For all the lines (except for the ones listed as 'Unknown Owner' and N/A') I would like to change to lower case and concatenate the first and last names.

Before:
Code:
aaa.bbb.ccc.ddd,Unknown Owner
ddd.eee.fff.ggg,N/A
hhh.iii.jjj.kkk,John Doe
aaa.bbb.ccc.ddd,Mary Jane
After:
Code:
aaa.bbb.ccc.ddd,Unknown Owner
ddd.eee.fff.ggg,N/A
hhh.iii.jjj.kkk,john.doe
aaa.bbb.ccc.ddd,mary.jane
I have the following but it 'removes' the lines, I would like it to just 'skip' those lines.
Code:
awk -F "," '!/N\/A|Unknown Owner/ {print tolower ($2)}' file
I am not sure on the concatenate part as I have never done it in awk.

Thanks & Regards
 
Old 11-28-2010, 09:20 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So you need to look at what you have asked:
Quote:
!/N\/A|Unknown Owner/
This is the only pattern construct you have and it says to ignore lines containing either of those combinations.
Yes this is what you want for the transformation.

So maybe you need a different rule for those words?

Or, if you want it more robust but kept small, maybe make your change without printing and then print all lines as a separate action.

Lastly, it could be awk is the wrong tool and maybe you should use sed?

I have refrained from supplying a direct answer as you have had many others and I believe you need to start to solve some of these simpler ones
on your own once pointed in the right direction.
 
1 members found this post helpful.
Old 11-28-2010, 10:32 PM   #3
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
Fare enough. Here is my solution. Rough but works...

Code:
# Extract lines of IP's with POC's
awk -F "," '!/Unknown Owner|N\/A/' file > file1.tmp
# Extact names
cut -d"," -f 2 file1.tmp > file2.tmp
# Extract IP
cut -d"," -f 1 file1.tmp > file3.tmp
# Concatenate names
awk '{print $1 "." $2}' file2.tmp > file4.tmp
# Join IP and names
paste -d"," file3.tmp file4.tmp > file5.tmp
# Extract IP's with no owners (Unknown Owner, N/A)
awk -F "," '/Unknown Owner|N\/A/ {print $1 "," $2}' file.TMP > file6.tmp
# Join  back together
cat file6.tmp  file5.tmp  > output.csv

Last edited by hattori.hanzo; 11-28-2010 at 10:34 PM.
 
Old 11-28-2010, 10:52 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well definitely a solution

I feel your awk skills are probably a little better than that by now

If we look at the task:

1. Line containing N/A or Unknown Owner to be left as is - currently covered by your regex and not
2. Last field (I see NF here) to be set to lowercase
3. If last field has a space in it, then replace with a dot (smells like a gsub (use gsub instead of sub in case person has more than one part to last name))

Let me know if this is enough for you to complete?

I really like to remember the K.I.S.S philosophy when writing scripts - Keep It Simple Silly (or Stupid depending on how you like to say the last word)

See how you go and ask away at the questions when you get stuck.
 
Old 11-28-2010, 10:52 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
EDIT: removed double post
 
  


Reply



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
mkisofs -iso-level 1 converts to lower case instead of upper case. stf92 Slackware 3 10-29-2010 12:32 AM
renaming directories from upper case to lower case, help!! linux_teller Linux - Newbie 3 03-07-2008 05:15 AM
grep [A-Z] & [a-z] match both upper and lower case. jschiwal Linux - General 4 05-28-2005 06:43 AM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 04:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 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
Open Source Consulting | Domain Registration