Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-05-2006 04:54 PM
тАО12-05-2006 04:54 PM
"ab",12345,456,"home ahi","new one","get that me",45678,"new",123,"mnw",45,"mnuens"
"ab",12345,456,"home ahi","new one","get that me",45678,"new",123,"mnw",45,"mnuens","kil je"
I want the fields where a character fields contains double quotes" (in between the field not the " which encloses the field)
Please help me out.
Solved! Go to Solution.
- Tags:
- csv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-05-2006 06:51 PM - edited тАО10-23-2011 05:55 PM
тАО12-05-2006 06:51 PM - edited тАО10-23-2011 05:55 PM
Re: awk help
I don't see any entry that contains embedded double quotes??
This script assume that comma isn't embedded. It also assume that double quote isn't embedded in a non-quoted string.
#!/usr/bin/ksh
awk -F"," '
function check_field(i, ff) {
len = length(ff)
if (substr(ff, 1, 1) == "\"" && substr(ff, len, 1) == "\"") {
if (index(substr(ff,2,len-2), "\"") != 0) {
print "Found embedded double quote"
print "record:", NR, " field:", i, ":" ff ":"
}
}
}
{
# check each field for embedded double quotes
for (i=1; i <= NF; ++i) {
check_field(i, $i)
}
}' itrc_cvs.in
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-05-2006 07:16 PM
тАО12-05-2006 07:16 PM
Re: awk help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-05-2006 07:27 PM - edited тАО10-23-2011 05:56 PM
тАО12-05-2006 07:27 PM - edited тАО10-23-2011 05:56 PM
SolutionNo, try this version. Note: it doesn't check for quotes being balanced.
#!/usr/bin/ksh
awk -F"," '
function check_field(i, ff) {
len = length(ff)
# strip off any double quotes
if (substr(ff, len, 1) == "\"")
--len
start=1
if (substr(ff, 1, 1) == "\"") {
start=2
--len
}
if (index(substr(ff,start,len), "\"") != 0) {
print "Found embedded double quote"
print "record:", NR, " field:", i, ":" ff ":"
}
}
{
# check each field for embedded double quotes
for (i=1; i <= NF; ++i) {
check_field(i, $i)
}
}' itrc_cvs.in
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 01:30 AM
тАО12-06-2006 01:30 AM
Re: awk help
I would divide this request into two parts:
1) check for balanced quoting
2) generate the requested output
Solution 1)
awk -F, -v qu='"' '{for(i=1;i<=NF;i++) {if(! index($i,qu)) continue
m=match($i,qu".*"qu)
if(m) { if(length($i)==RLENGTH) continue
printf("Err: line=%d f=%d data outside quotes:%s:\n",NR,i,$i)}
else printf("Err: line=%d f=%d unbalanced quotes:%s:\n",NR,i,$i)}}'
Feeding this data
1234,aa","aa bb","dd"hh,"bb,456
would create
Err: line=1 f=2 unbalanced quotes:aa":
Err: line=1 f=4 data outside quotes:"dd"hh:
Err: line=1 f=5 unbalanced quotes:"bb:
Solution 2)
awk -F, -v qu='"' '{out=0; for(i=1;i<=NF;i++) {if(! index($i,qu)) continue
if (match($i,qu".*"qu) && (length($i)==RLENGTH)) {if(out) printf(FS);out++;printf(substr($i,2,RLENGTH-2))}}
if(out) printf("\n")}' above.csv
would create this output (with above data)
ab,home ahi,new one,get that me,new,mnw,mnuens
ab,home ahi,new one,get that me,new,mnw,mnuens,kil je
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 03:28 AM
тАО12-06-2006 03:28 AM
Re: awk help
See the awk script below...
awk -F, '{
for (i=1;i<=NF;++i) {
f=$i
s=gsub("\"","",f)
if (s) {
if (s==2) {
if ($i!~/^".*"$/)
print $i
}
else
print $i
}
}
}' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 03:42 AM
тАО12-06-2006 03:42 AM
Re: awk help
a simple approach based on an idea similar to Sandman's:
#!/usr/bin/sh
typeset -i POS=0 NUMBER=0
while read LINE
do
while [ POS -lt ${#LINE} ]
do
POS=$POS+1
KARAK=$(echo ${LINE} | cut -c $POS)
if [ "$KARAK" = "\"" ]
then
NUMBER=$(( $NUMBER + 1 ))
KARAK=" "
fi
let DECISION=NUMBER%2
if [ "$DECISION" != 0 ]
then
echo "$KARAK\c"
fi
done
echo ""
POS=0
NUMBER=0
done <$1
Run it using your inputfile as $1
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 03:35 PM
тАО12-06-2006 03:35 PM
Re: awk help
Presently double quotes are not enclosed with ""
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 04:14 PM
тАО12-06-2006 04:14 PM
Re: awk help
but, there are several perl modules written for handling csv files. Just a suggestion, use code that has already been written and tested instead of creating your own.
http://www.perlmeme.org/tutorials/parsing_csv.html
http://www.interopp.org/ncci/man/CSV.htm
http://search.cpan.org/search?query=csv&mode=all
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 04:41 PM
тАО12-06-2006 04:41 PM
Re: awk help
~hope it helps