Extracting variables between commas : GAWK or SED


 
Thread Tools Search this Thread
Special Forums Windows & DOS: Issues & Discussions Extracting variables between commas : GAWK or SED
# 1  
Old 02-14-2011
Error Extracting variables between commas : GAWK or SED

Hello,

I need some help,

I got a CSV file called test.txt with this text in it :

Code:
08/02/2011;0,677;0,903;1,079;1,336;1,513;1,683

There's only a line and i need to copy theese numbers into variables :

0,677
0,903
1,079
1,336
1,513
1,683

The output file should look like this :

Code:
IN      TAUX1                                    20110208D  0,677          0,677
IN      TAUX2                                    20110208D  0,903          0,903
ETC ...

I'm under windows7 and but i have SED and GAWK installed.

Can someone Help me doing this ? With Gawk I always get a parse error while setting the FS to ";"

Thanks a lot for your Help guys !

Julien
# 2  
Old 02-14-2011
Try:
Code:
awk 'NR==1{split($0,a,"/");next}
{print "IN\tTAUX" ++c "\t" a[3] a[2] a[1] "D\t"  $1 "\t" $1}
' RS=";" file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 02-14-2011
I wrote this Batch File :
Code:
gawk 'NR==1{split($0,a,"/");next}
{print "IN\tTAUX" ++c "\t" a[3] a[2] a[1] "D\t"  $1 "\t" $1}
' RS=";" C:\file.txt
pause

Dos returns me this Error message :
Code:
C:\Documents and Settings\julien.lacombe\Desktop>gawk 'NR==1{split($0,a,"/");nex
t}
gawk: cmd. line:1: 'NR==1{split($0,a,/);next}
gawk: cmd. line:1: ^ invalid char ''' in expression

C:\Documents and Settings\julien.lacombe\Desktop>{print "IN\tTAUX" ++c "\t" a[3]
 a[2] a[1] "D\t"  $1 "\t" $1}
'{print' is not recognized as an internal or external command,
operable program or batch file.

C:\Documents and Settings\julien.lacombe\Desktop>' RS=";" C:\file.txt
''' is not recognized as an internal or external command,
operable program or batch file.

C:\Documents and Settings\julien.lacombe\Desktop>pause
Press any key to continue . . .

Thanks for your time, I'm currently learning Batch et Gawk scripting !

---------- Post updated at 05:48 AM ---------- Previous update was at 05:43 AM ----------

I changed my Batch file into :

Code:
gawk "NR==1{split($0,a,"/");next}{print "IN\tTAUX" ++c "\t" a[3] a[2] a[1] "D\t"  $1 "\t" $1}" RS=";" C:\file.txt 
pause


And dos returns me this Error :

Code:
C:\Documents and Settings\julien.lacombe\Desktop>gawk "NR==1{split($0,a,"/");nex
t}{print "IN\tTAUX" ++c "\t" a[3] a[2] a[1] "D\t"  $1 "\t" $1}" RS=";" C:\file.t
xt
gawk: cmd. line:1: NR==1{split($0,a,/);next}{print IN\tTAUX ++c \t a[3] a[2] a[1
] D\t  $1 \t $1}
gawk: cmd. line:1:                   ^ unterminated regexp
gawk: cmd. line:2: NR==1{split($0,a,/);next}{print IN\tTAUX ++c \t a[3] a[2] a[1
] D\t  $1 \t $1}
gawk: cmd. line:2:
                ^ unexpected newline

C:\Documents and Settings\julien.lacombe\Desktop>pause
Press any key to continue . . .

# 4  
Old 02-14-2011
Probably you should escape the double quotes, try this in one line:
Code:
gawk "NR==1{split($0,a,\"/\");next}{print \"IN\tTAUX\" ++c \"\t\" a[3] a[2] a[1] \"D\t\"  $1 \"\t\" $1} " RS=";" file

# 5  
Old 02-14-2011
Wooow ! Thanks a lot !!

Actually, I have a last question : I wrote TAUX1, TAUX2,TAUX3 ... But in fact TAUX1 = EONIA ... TAUX2=EURIBOR ... TAUX3 = EUR3M ...

IS THERE A SIMPLE WAY TO CHANGE THEM ?

---------- Post updated at 06:18 AM ---------- Previous update was at 06:06 AM ----------

And hum, is there a way to store each number in a separated variable ?
Like :
$1 = 0,677
$2 = 0,903
$3 = 1,079

Thanks for your time !


# 6  
Old 02-14-2011
Quote:
Originally Posted by jujulips
Wooow ! Thanks a lot !!

Actually, I have a last question : I wrote TAUX1, TAUX2,TAUX3 ... But in fact TAUX1 = EONIA ... TAUX2=EURIBOR ... TAUX3 = EUR3M ...

IS THERE A SIMPLE WAY TO CHANGE THEM ?
How to determine when to use EONIA, EURIBOR, EURM3?
And how about TAUX4, TAUX5 ,TAUX6.......

Quote:
Originally Posted by jujulips
And hum, is there a way to store each number in a separated variable ?
Like :
$1 = 0,677
$2 = 0,903
$3 = 1,079

Thanks for your time !
I don't know how to do that in windows....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk and sed question

Can anyone tell me what this script does? sort file1.txt | awk -F, -f event.gawk Thank you. (2 Replies)
Discussion started by: dee campbell
2 Replies

2. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

3. Shell Programming and Scripting

extracting substrings from variables

Hello Everyone, I am looking for a way to extract substrings to local variables. Here is the format of the string variable i am using : /var/x/www && /usr/x/share/doc && /etc/x/logs where the substrings i must extract are the "/var/x/www" and such. I was originally thinking of using... (15 Replies)
Discussion started by: jimmy75_13
15 Replies

4. Shell Programming and Scripting

Help with sed and replacing white spaces with commas

Dear all, I am in a bit of a quandary. I have 400 text files which I need to edit and output in a very specific way. Here is a sample text file copied from gedit ... The columns will come out a bit messed up but when I cat <file>, it gives a table with six columns (0-28, tot lob vol, vcsf,... (6 Replies)
Discussion started by: antonz
6 Replies

5. Shell Programming and Scripting

Extracting variables from echo

Hello all. I can not remember the command to extract a variable from the date command. Basically what I need to do is to store the values of date in variable and rearrange them. I can not remember the command or the syntax to do so. so.. date Mon Mar 8 06:57:19 GMT 2010 $1 $2 ... (12 Replies)
Discussion started by: adelsin
12 Replies

6. Shell Programming and Scripting

Select ip and date using sed or gawk

1.56.253.48 - - "GET " 1.6.253.48 - - "GET " 1.65.253.48 - - "GET " 1.65.253.48 - - "GET " 1.63.53.48 - - "GET " 1.65.253.48 - - "GET " 1.16.23.48 - - "GET " 1.64.25.48 - - "GET " need command which give the output 1.6.253.48 - 09/Nov/2009:07:02:24 1.65.253.48 -... (7 Replies)
Discussion started by: sagar_evc
7 Replies

7. Shell Programming and Scripting

Editing Commas in a textfile using sed

Hi guys task removing the last commas of 5th and 6th columns. The bug in the script is causing effect because of whitespaces around commas. I tried to delete white spaces first and running the above script. but still some where getting the results wrong. I already have a script to do this... (12 Replies)
Discussion started by: repinementer
12 Replies

8. Shell Programming and Scripting

Extracting strings surrounded by parentheses and seperate by commas

Excuse the terrible title. I have a text file of 1..n lines, each one containing at least one string between parentheses. Within each string, there is one or more strings separated by commas. I need to extract each string, thus: input file: (THIS,THAT) (THE,OTHER) (THING) (OR,MAYBE)... (6 Replies)
Discussion started by: kpfeif
6 Replies

9. UNIX for Dummies Questions & Answers

Inserting commas and replacing backslashes with commas

Hi, Newbie here. I have a file that consists of data that I want to convert to a csv file. For example: Jul 20 2008 1111 / visit home / BlackBerry8830/4.2.2 Profile/MIDP-2.0 Configuration/CLOC-1.1 VendorID/105 Jul 21 2008 22222 / add friend / BlackBerry8830/4.2.2 Profile/MIDP-2.0... (3 Replies)
Discussion started by: kangaroo
3 Replies

10. Shell Programming and Scripting

how to use variables and gawk in a script?

Hi, I want to define variables in a shell script and make gawk use them to make some operations Mfn = $(grep " 1 " $fitxer | gawk '{print $2}') Xfn = $(grep " 1 " $fitxer | gawk '{print $3}') Yfn = $(grep " 1 " $fitxer | gawk '{print $4}') Zfn = $(grep " 1 " $fitxer | gawk... (9 Replies)
Discussion started by: pau
9 Replies
Login or Register to Ask a Question