Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-30-2012
Registered User
 
Join Date: Jul 2012
Posts: 7
Thanks: 9
Thanked 0 Times in 0 Posts
Need HELP with AWK split. Need to check for "special characters" in string before splitting the file

Hi Experts.

I'm stuck with the below AWK code where i'm trying to move the records containing any special characters in the last field to a bad file.


Code:
awk -F, '{if ($NF ~ /^[0-9]|^[A-Za-z]/) print >"goodfile";else print >"badfile"}' filename

sample data

Code:
1,abc,def,1234,A *
2,bed,dec,342,* A         
3,dec,345,23,*&^          
4,sdf,fgh,234,  
5,ert,345,ghj,C**
6,ert,345,sdf,123          ---- only valid record

The output required must contain the first 5 records in badfile and the last record in good file.
But my above awk logic cosiders only the below records as badfile records:

Code:
2,bed,dec,342,* A 
3,dec,345,23,*&^ 
4,sdf,fgh,234,

The other two invalid records ("A *" and "C**") are being written into goodfile which is wrong.
Please help me fix this.

Note: the $NF values can contain [spaces:] between any alphanumeric chars. However, all spaces or null is considered a bad record.

Thanks Gurus!
Sponsored Links
    #2  
Old 07-30-2012
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
 
Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 695
Thanks: 14
Thanked 84 Times in 80 Posts
Tune your regexp to:

Code:
awk ' /[0-9]$|[A-Za-z]$/{print >"goodfile";next}{print >"badfile"}' infile

Sponsored Links
    #3  
Old 07-30-2012
Registered User
 
Join Date: Jul 2012
Posts: 7
Thanks: 9
Thanked 0 Times in 0 Posts
I tuned the regex as suggested. But it does not give the required output.
code used:

Code:
awk -F, '{if ($NF ~ /[0-9]$|[A-Za-z]$/) print >"goodfile"; else print >"badfile"}' samp.txt

samp.txt

Code:
1,abc,def,1234,A *
2,bed,dec,342,* A
3,dec,345,23,*&^
4,sdf,fgh,234,
5,ert,345,ghj,C*2
6,ert,345,sdf,123

Output

Code:
$ cat goodfile
2,bed,dec,342,* A
5,ert,345,ghj,C*2
6,ert,345,sdf,123
$ cat badfile
1,abc,def,1234,A *
3,dec,345,23,*&^
4,sdf,fgh,234,

    #4  
Old 07-30-2012
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
 
Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 695
Thanks: 14
Thanked 84 Times in 80 Posts
Check your syntax:

Code:
awk -F, '{if ($NF ~ /[0-9]$|[A-Za-z]$/) {print >"goodfile"} else {print >"badfile" }}'  infile

Sponsored Links
    #5  
Old 07-30-2012
Registered User
 
Join Date: Jul 2012
Posts: 7
Thanks: 9
Thanked 0 Times in 0 Posts
Sorry, Klashxx.
It gives the same output.


Code:
$ cat samp.txt
1,abc,def,1234,A *
2,bed,dec,342,* A
3,dec,345,23,*&^
4,sdf,fgh,234,
5,ert,345,ghj,C*2
6,ert,345,sdf,123

$ awk -F, '{if ($NF ~ /[0-9]$|[A-Za-z]$/) {print >"goodfile"} else {print >"badfile" }}' samp.txt

$ cat goodfile
2,bed,dec,342,* A
5,ert,345,ghj,C*2
6,ert,345,sdf,123

$ cat badfile
1,abc,def,1234,A *
3,dec,345,23,*&^
4,sdf,fgh,234,

---------- Post updated at 05:29 AM ---------- Previous update was at 05:28 AM ----------

I'm using korn shell. Maybe that makes a difference .??
Sponsored Links
    #6  
Old 07-30-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Location: Aachen, Germany
Posts: 1,881
Thanks: 25
Thanked 433 Times in 419 Posts
Quote:
Originally Posted by Klashxx View Post
Check your syntax:

Code:
awk -F, '{if ($NF ~ /[0-9]$|[A-Za-z]$/) {print >"goodfile"} else {print >"badfile" }}'  infile

This only looks at $NF's last char. This
Code:
awk -F, '{if ($NF~/[0-9A-Za-z][0-9A-Za-z][0-9A-Za-z]/) print >"goodfile"; else print>"badfile}' samp.txt

will work on the example, but it does not take into account the possible variable length of $NF. The repetition term /.../{length($NF)} does not seem to work, nor does the regex [[:alnum:]] contruct.
The Following User Says Thank You to RudiC For This Useful Post:
shell_boy23 (07-30-2012)
Sponsored Links
    #7  
Old 07-30-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 475 Times in 455 Posts
Try this:

Code:
awk -F, '{if($NF~/^[[:alnum:][:blank:]]+$/) print > "goodfile"; else print > "badfile"}' infile

Sponsored Links
Closed Thread

Tags
awk, awk-if, ksh, split file

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Splitting text string with "|" pipe delimiters alanp36 Shell Programming and Scripting 4 06-03-2010 04:37 AM
"sed" to check file size & echo " " to destination file jockey007 Shell Programming and Scripting 7 04-28-2009 02:08 AM
how to split special characters "|" using awk krishna9 Shell Programming and Scripting 3 05-22-2008 06:30 AM
perl split funciton - special character "/" deepakwins UNIX for Dummies Questions & Answers 5 02-07-2008 11:19 PM
split the string "Setview: arv-temp-view" amitrajvarma Shell Programming and Scripting 2 10-11-2007 05:14 AM



All times are GMT -4. The time now is 03:30 PM.