awk function in handling quotes


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users awk function in handling quotes
# 1  
Old 08-16-2010
awk function in handling quotes

Hi all,

I have input lines like below
Code:
empno,ename,sal,description
----------------------------

311,"jone,abc",2000,manager
301,david,200,"president,ac"

I need to sum the salary of them i.e. 2000+200

anything suggested

Thanks,
Shahnaz.

Moderator's Comments:
Mod Comment Use code tags.

Last edited by zaxxon; 08-16-2010 at 11:39 AM..
# 2  
Old 08-16-2010
This works only if you one comma between quotes
Code:
sed 's/"\([^,"]*\),\([^"]*\)"/\1|\2/' file | awk -F"," ' { sum+=$3 } END  {print sum } '

# 3  
Old 08-18-2010
Thank you so much.

but what about the following line exists in a file

Quote:
"hello","how are you","hi,dear"

echo "\"hello\",\"how are you\",\"hi,dear\""|sed 's/"\([^,"]*\),\([^"]*\)"/\1|\2/'
IT shows o/p as

Quote:
"hello|how are you","hi,dear"
but I expect to be
Quote:
"hello","how are you","hi,dear"


---------- Post updated 08-18-10 at 09:01 AM ---------- Previous update was 08-17-10 at 06:29 PM ----------

Unix gurus,
anything can be updated for my query?

thanks.
# 4  
Old 08-18-2010
Your request for help is a bit confusing. I'm assuming that you want to take a string with comma separated values and convert the values into bar separated fields. Further, if the field is quoted, then it may contain commas which do not indicate a field. Thus:

Code:
"hello","how are you",doing?,are,you,"at,home,tonight","this,is a,test","of the system"

would be translated to:
Code:
hello|how are you|doing?|are|you|at,home,tonight|this,is a,test|of the system

If that is what you are looking for, then this sed programme will do it:

Code:
sed -E '
s/^([^",]+)/"\1"/;    # edit -- enables processing of your original file
s/^"/</;
s/"$/>/;
s/","/></g;
s/",/>/g;
s/,"/</g;
s/>([^<,]+)/><\1>/;
: try_again
s/>,([^,<]+)/><\1>/;ttry_again
s/></|/g;
s/^<//;
s/>$//'

It does not handle input with < or > characters. If you are not using a BSD or AT&T AST version of sed, then you'll need to replace -E with -r on the command.

Edit -- with small change, the original input you presented can be processed and passed to awk or other programme:

Code:
$cat t17a.data
311,"jone,abc",2000,manager
302,david,200,"president,ac"
"303",david,200,"vicepresident,ac"
"304",Charlie,400,"peon"


$t17 <t17a.data
311|jone,abc|2000|manager
302|david|200|president,ac
303|david|200|vicepresident,ac
304|Charlie|400|peon


Last edited by agama; 08-18-2010 at 01:40 AM..
# 5  
Old 08-18-2010
Thank you so much.
yeah, the output you produced is the requirement for me.

but -E didn't work for me either -r (no option).
I am using AIX 5.3

anything can be done here ?

Anticipating your reply.
# 6  
Old 08-18-2010
Quote:
Originally Posted by shahnazurs
Thank you so much.
yeah, the output you produced is the requirement for me.

but -E didn't work for me either -r (no option).
I am using AIX 5.3

anything can be done here ?

Anticipating your reply.
You are welcome. No clue about what AIX installs. Haven't played on an AIX box in at least 10 years, and remember only that things were very non-standard. Maybe someone with AIX savvy can chime in and shed some light. In the mean time I'd check the sed man page to see what it says about regular expressions.

If you are able to, you could always download and install the AST tools from AT&T http://www.research.att.com/export/s...ols/index.html -- It's a bit of work to get them to build, but once you have them they are great.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Using quotes in awk

Hello, i had a problem running a script , and after investigation found its all to do with the quotes: cat file1 line1 val1 val2 line2 val1 val2 line3 val1 val2 awk 'BEGIN {RS="\n\n"; FS="\n";} {print $1 $2}' file1 This gives me the wrong output: (5 Replies)
Discussion started by: andy391791
5 Replies

3. Shell Programming and Scripting

Handling embedded double quotes within column data

I have a text file where each field is enclosed in double quotes and separated by a comma. But in certain rows we have embedded double quotes within column data For e.g """TRUSPICE CENTRE"" CLAYTON AU" The above value is for a single column but there are embedded quotes within... (2 Replies)
Discussion started by: abhilashnair
2 Replies

4. Shell Programming and Scripting

awk without quotes

I want to execute awk command without quotes. who am i | awk {'print $2'} above code should be something like: who am i | awk {print $2} Why such weird requirement? Im assigning command to a variable, hence i need to escape the quotes. e.g: x='who am i | awk {\'print $2\'}' I want... (11 Replies)
Discussion started by: Arun_Linux
11 Replies

5. Shell Programming and Scripting

handling arrays with awk

Hi, I have an issue that I am trying to resolve using arrays in awk. I have two files, the first one is a dictionary with this format: FILE 1 (dictionary) 'Abrir' 'Open' 'Aceptar' 'Accept' Every line has two fields, a word in two languages. The second file is a simple list of... (3 Replies)
Discussion started by: gmartinez
3 Replies

6. Shell Programming and Scripting

quotes using awk

i want to print the statement below using awk,but i am unable to get the quotes ("22345",1,"Thank you"); How can i do this (5 Replies)
Discussion started by: tomjones
5 Replies

7. Shell Programming and Scripting

Handling multiple fields of a database file for toupper() function in awk

hello everyone.... script is: To convert the contents of a database file into uppercase my code is: printf "%s\n" , $2 | awk '{print toupper($2)}' emp.lst i m able to do only for one field.....didn't get any sources for handling multiple fields. please suggest me for multiple... (1 Reply)
Discussion started by: Priyanka Bhati
1 Replies

8. Programming

Handling a signal with a class member function

Hello, i am using the sigaction function to handle the SIGCHLD signal.Is it possible to use a class member function as the handler function (the sa_handler member of the sigaction structure)? The function's signature is: void (*sa_handler)(int);so i don't think i can use a static member function... (2 Replies)
Discussion started by: Zipi
2 Replies

9. Shell Programming and Scripting

column handling in awk

Dear Scripting experts, I have a problem which i cannot get my head around and wondered if anyone can help me. I have two files "file1" and "file2" and i want to replace column one from file 1 with column one with file2.(where file two has many columns). see example.. ive tried to use cut and... (4 Replies)
Discussion started by: Mish_99
4 Replies

10. Programming

signal handling while in a function other than main

Hi, I have a main loop which calls a sub loop, which finally returns to the main loop itself. The main loop runs when a flag is set. Now, I have a signal handler for SIGINT, which resets the flag and thus stops the main loop. Suppose I send SIGINT while the program is in subloop, I get an error... (1 Reply)
Discussion started by: Theju
1 Replies
Login or Register to Ask a Question