|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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"}' filenamesample 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
|
||||
|
||||
|
Tune your regexp to: Code:
awk ' /[0-9]$|[A-Za-z]$/{print >"goodfile";next}{print >"badfile"}' infile |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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.txtsamp.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
|
||||
|
||||
|
Check your syntax: Code:
awk -F, '{if ($NF ~ /[0-9]$|[A-Za-z]$/) {print >"goodfile"} else {print >"badfile" }}' infile |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
Code:
awk -F, '{if ($NF~/[0-9A-Za-z][0-9A-Za-z][0-9A-Za-z]/) print >"goodfile"; else print>"badfile}' samp.txtwill 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
|
||||
|
||||
|
Try this: Code:
awk -F, '{if($NF~/^[[:alnum:][:blank:]]+$/) print > "goodfile"; else print > "badfile"}' infile |
| Sponsored Links | ||
|
![]() |
| Tags |
| awk, awk-if, ksh, split file |
| Thread Tools | Search this Thread |
| 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 |
|
|