awk or cut


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or cut
# 15  
Old 08-21-2007
Quote:
Originally Posted by anypager
Help!
Please give me a solution.

Give a sample file (not only one record):

Code:
$ cat file
"x1,2",,"y",,"z"
"x1,2",,"y",,,"z",,,
"y",,,"z",,,"x1,2"
$ sed 's/\("[^"]*"\),*/\1 /g' file
"x1,2" "y" "z"
"x1,2" "y" "z"
"y" "z" "x1,2"

# 16  
Old 08-21-2007
Change the delimiter to something else. This converts all unquoted commas to pipes...
Code:
awk '{
    for(i=1;i<=length;i++){
        c=substr($0,i,1)
        if(c=="\"") q++
        printf (q%2==0&&c==","?"|":c)
    }
    printf ORS
 }' file1 > file2

You can then use cut to get the columns you want, e.g...
Code:
cut -d \| -f1,3,6 file2 > file3

Or use awk again to get the fields you want and convert back to the original delimiter, e.g...
Code:
awk -F \| -v OFS=, '{print $1,$3,$6}' file2 > file3

Results from testing...
Code:
$ head file[123]
==> file1 <==
"x1,2",,"y",,"z"
"x1,2",,"y",,,"z",,,
"y",,,"z",,,"x1,2"

==> file2 <==
"x1,2"||"y"||"z"
"x1,2"||"y"|||"z"|||
"y"|||"z"|||"x1,2"

==> file3 <==
"x1,2","y",
"x1,2","y","z"
"y",,

# 17  
Old 08-22-2007
Quote:
Originally Posted by Ygor
Change the delimiter to something else. This converts all unquoted commas to pipes...
Code:
awk '{
    for(i=1;i<=length;i++){
        c=substr($0,i,1)
        if(c=="\"") q++
        printf (q%2==0&&c==","?"|":c)
    }
    printf ORS
 }' file1 > file2

This works fine, but performance is poor.

Could we use regular expression for awk -F to differentiate unquoted commas?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Regarding cut or awk command

Hi, I need a help with cut/awk command. I need to get certain values from a string provided. For example: i have a config file with below mentioned details oracle="user=name"/"pass/word"@databasename. I have used a command var1=`grep -w oracle sample.cfg | cut -d"=" -f2 | cut -d"/" -f1`. ... (10 Replies)
Discussion started by: kumars2102
10 Replies

2. Shell Programming and Scripting

Cut or awk in reverse

I may be making this too hard on myself, but I'm trying to find a way that I can use a cut or awk string to always remove the last two delimited fields of a string. Say I have PackageName-U939393-8.2.3.4.s390x.rpm But the s390x could be any string w/o periods in it, x8664 for example,... (9 Replies)
Discussion started by: cbo0485
9 Replies

3. Shell Programming and Scripting

Using grep and cut within awk

Hi My input file looks like as follows: say a.txt "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss... (5 Replies)
Discussion started by: bittoo
5 Replies

4. Shell Programming and Scripting

Want to use awk instead of cut

I want to use awk instead of cut command. Following is my code: line="slNumber=US78AJF11643, slName=USJFKAAUSYDAAUL80441032900095, dummy sl found? sqlca.sqlcode=0" sl_WORD=`echo $line| cut -f 1 -d','` sl=`echo $sl_WORD | cut -f 2 -d'='` echo "$sl" Please suggest me about the code. ... (5 Replies)
Discussion started by: rinku
5 Replies

5. Shell Programming and Scripting

HELP! using cut/awk

how would i write a shell script to show the number of lines in which int variable appears in a c++ program. how would i do this using cut or awk methods is it possbile and having a output similar to this x, number of apperances = y, number of apperances = (2 Replies)
Discussion started by: deadleg
2 Replies

6. UNIX for Dummies Questions & Answers

Help please awk or cut

Hi I'm new to unix programming so struggling with something thats probably simple to many of you I have data files of the format : ID, date, value1, value2, blank on each line either value1 or value2 will be zero. I need my output file to contain ID, date, non-zero value The input... (3 Replies)
Discussion started by: thewench
3 Replies

7. Shell Programming and Scripting

Is awk vs cut which one is better

i was trying to work on program to look for users never log on sever.. using awk with awk is working last| awk '{print $1}' |sort -u > /tmp/users1$$ cat /etc/passwd | awk -F: '{print $1}' |sort -u > /tmp/users2$$ comm -13 /tmp/users$$ rm -f /tmp/users$$ with cut it is not working ... (3 Replies)
Discussion started by: macrules
3 Replies

8. Shell Programming and Scripting

[grep awk cut] > awk

Hi, I'm very new to scripting. grep $s $filename | awk '{print $2}' | cut -c 1-8 How can I optimize this using a single awk? I tried: awk '/$s/ {print $2}' $filename | cut -c 1-8 However didn't work, I think the awk is not recognizing $s and the verbal is something else. (6 Replies)
Discussion started by: firdousamir
6 Replies

9. UNIX for Dummies Questions & Answers

cut vs. sed vs. awk ?

hi again...need new help guys:p the file contains following infos... users/abc/bla1.exe newusers/defgh/ik/albg2.exe users2/opww/ertz/qqwertzu/rwerwew.exe how to get the file content into... users/abc/ newusers/defgh/ik/ users2/opww/ertz/qqwertzu/ with... you can erase the... (5 Replies)
Discussion started by: svennie
5 Replies
Login or Register to Ask a Question