Visit Our UNIX and Linux User Community


multiple delimeters in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting multiple delimeters in awk
# 1  
Old 10-14-2011
multiple delimeters in awk

Hi all,


The have 92 columns with combination of "" and , two delimiters‎ and i need to takes some 32 columns only in that. i used awk command to extract .but its not working good.

Example: "aaaa","10,00.00",work,5555,.............

Command i tried :

awk -F"[ ,,"" ]" -v OFS="," 'FNR>1{print $1,$5,$6,$15,$17,$24,$25,$26,$27,$28,$31,$32,$34,$35,$37,$39,$40,$41,$42,$43,$46,$72,$73,$74,$77,$80 ,$81,$82,$84,$88,$90,$92}' >> outfile.csv



I would really appreciate if some one helps as soon as poss


Thanks,
Baski
# 2  
Old 10-14-2011
Code:
$ sed 's,\",,g' infile | awk -F, '{ print $1............$92 }' >> outfile.csv

# 3  
Old 10-14-2011
Does the output need to be in the same format (i.e. comma-separated and with quotes preserved)?

Quote:
Originally Posted by jayan_jay
Code:
$ sed 's,\",,g' infile | awk -F, '{ print $1............$92 }' >> outfile.csv

Note that this will split the single field "10,00.00" into 10 and 00.00.

Last edited by CarloM; 10-14-2011 at 06:40 AM..
# 4  
Old 10-14-2011
Thanks a lot frnds.....

yes Carlom , it it should be preserved with quote...like 10,000 not 10 and 000

but sed is not reading multiple files at time

mean,

$ sed 's,\",,g' infile*.csv | awk -F, '{ print $1............$92 }' >> outfile.csv
# 5  
Old 10-14-2011
The command looks ok. Post your error.
# 6  
Old 10-14-2011
Sorry .. donot have enough time and hence done it in ugly mode ..
Code:
for i in `ls infile*.csv`
do
sed 's,[0-9]\,,&%,g;
  s,\,%\",|,g;
  s,\"\,\",|,g;
  s,\"\,,|,g;
  s,\,%,?,g;
  s,\,,|,g;
  s,?,\,,g;
  s,\",,g' $i |
awk -F\| '{ print $1............$92 }' >> outfile.csv 
done

# 7  
Old 10-14-2011
This is probably a deeply inelegant way of doing it, but it's just a quickie from some other code I had lying around (that I probably copied from somewhere else!).

Code:
#  cat csv2.awk
BEGIN {
        if (NUMCOLS == "") NUMCOLS=32
        if (DELIM == "") DELIM = "\t"
        if (REPL == "") REPL = "~"
}
{
        gsub(DELIM, REPL)
        $0 = gensub(/([^,])\"\"/, "\\1'", "g")
        out = ""
        n = length($0)
        for (i = 1;  i <= n;  i++) {
                if ((ch = substr($0, i, 1)) == "\"") {
                        inString = (inString) ? 0 : 1
                }
                out = out ((ch == "," && ! inString) ? DELIM : ch)
        }
        nfields=split(out,outfields,DELIM);
        for (i=1;(i<=nfields)&&(i<=NUMCOLS);i++) {
                if (i > 1) {
                        printf (",");
                }
                printf ("%s", outfields[i]);
        }
        printf ("\n");
}

Code:
#  cat a.csv
"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.
 etc...

Code:
# awk -f csv2.awk a.csv
"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555,"aaaa","10,00.00",work,5555


(it will fail if there are any tabs in the data, but you can change the delimiter to something else if need be)

Previous Thread | Next Thread
Test Your Knowledge in Computers #116
Difficulty: Easy
Linux distributions are the most famous and healthiest example of the Unix derivatives; but macOS is currently the largest installed Unix user base (2019).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace last 9 delimeters , with | in UNIX

Hi Guys, I want to replace last 9 "," delimeters with "|" in a file Example :- "ashu,pant",3,5,5,7,7,87,8,8,8 "ashu,pant"|3|5|5|7|7|87|8|8|8 Help would be really appreciated. Thanks guys, Please use CODE tags as required by forum rules! (7 Replies)
Discussion started by: himanshupant
7 Replies

2. Shell Programming and Scripting

Split a line into multiple lines based on delimeters

Hi, I need help to split any lines that contain ; or , input.txtAc020 Not a good chemical process AC030 many has failed, 3 still maintained AC040 Putative; epithelial cells AC050 Predicted binding activity AC060 rodC Putative; upregulated in 48;h biofilm vs planktonic The output... (8 Replies)
Discussion started by: redse171
8 Replies

3. Shell Programming and Scripting

Lines between specific delimeters

Hi, I have a requirement like this Line 1 Line 2 Line 3 Manager Line 4 Line 5 I have to print like this Have you done your work on time because I have to forward the work forward. Any Help is really Appriciated.... (1 Reply)
Discussion started by: abhishek7687
1 Replies

4. Shell Programming and Scripting

Commas within Delimeters

Hi experts, I would like a favour from you guys to get the info from 5th column which was separated by the delimeter comma ( , ) The Data file is as below:- 1,USER1,"90, TEST AVENUE, OLD ROAD",test1,124,N 2,USER2,88 TEST STREET NEW ROAD,test2,123,N The User File is as below:- USER1... (1 Reply)
Discussion started by: shenkz
1 Replies

5. Shell Programming and Scripting

How to get a string between two delimeters using sed.

I want to get IP address only from the input using sed. i.e string between between "(" and ")" delimeters. echo "serverA (11.22.333.444) is alive" | sed 's/<??>/<??>/' I know how to do this using cut and awk's split features. I also want to know how to do this using awk's... (3 Replies)
Discussion started by: kchinnam
3 Replies

6. UNIX for Advanced & Expert Users

Delimeters Count in a FlatFile

Hi, I have the below script to check the count of delimeters for a file (here is File : test.csv Delimeter is ",") awk '{gsub(/"*"/,x);print gsub(/,/,x)}' test.csv And it return the output for each line as: 2 2 cat test.csv: abc,xyz "abc,zxyz",1 I need help one the below things: - IS... (8 Replies)
Discussion started by: venkatajay_18
8 Replies

7. Shell Programming and Scripting

awk with column delimeters

Hi All, I want to execute this cat /etc/passwd | awk -F: '{print $1,$7}' with a fixed column on the first and second. Say on the first I want 12 column and second with 20 column. Example root /usr/bin/ksh lp /bin/false Thanks for any comment you may add. (5 Replies)
Discussion started by: itik
5 Replies

8. Shell Programming and Scripting

awk with two delimeters

Hi, can anyone explain me below codes...i used the below one to use two delimeter(://) at a time.. but i am not able to understand the output options.. note: here there is no requirement ..only to understand myself about below command... command: awk -F "" '{print $1}' inputfile ... (2 Replies)
Discussion started by: Shahul
2 Replies

9. Shell Programming and Scripting

Parsing a file that contains 2 types of delimeters

Now that I have a file that looks something like this; 20050926 Unknown 20050926 MUREXFO 20050926 MUREXFO 20050926 MUREXFO 20050926 Unknown 20050926 KADDUSS 20050926 KADDUSS 20050926 KADDUSS 20050926 MUREXFO Is there a way in vi that I can search the file and remove any line... (2 Replies)
Discussion started by: morgadoa
2 Replies

10. Shell Programming and Scripting

Parsing a file that contains 2 types of delimeters

I am trying to write a script and failing miserably. I have a file that looks something like this; 20050924-155819;Backoffice;1037;0;DDT-TCP/IP;;0;Node 20050924-155902;Unknown;1036;0;DDT-TCP/IP;;0;Node 20050924-155922;FrontOffice;1040;5;DDT- The desired result is one file containing only... (4 Replies)
Discussion started by: morgadoa
4 Replies

Featured Tech Videos