Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with text file

Status
Not open for further replies.

sabetik

IS-IT--Management
Nov 16, 2003
80
GU
How can I add double quotes to my two last fields.
Here what I have:


"00060-65H45","0023465H45",11.46,106
"00060-65H45","0023465H45",11.46,106

I want to print like that:

"00060-65H45","0023465H45","11.46","106"
"00060-65H45","0023465H45","11.46","106"

Please help
Thanks
 
Code:
BEGIN {
  FS=OFS=","
}
{
 for (i=NF-2; i <= NF; i++)
    $i= "\"" $i "\""
 print


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks, How can i pass to a file not to screen.
 
One more thing why the second field I get two double quotes:
"00060-65H45",""0023465H45"","11.46","106"
"00060-65H45",""0023465H45"","11.46","106
 
nawk -f sabetik.awk myFile > output.txt

here's sabetik.awk:
Code:
BEGIN {
  FS=OFS=","
}
{
 for (i=NF-2; i <= NF; i++)
    $i= "\"" $i "\""
 print
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
One more thing. in the second field I have ',' that part of the field. I get result like this

Orginal format:
"2FS72-2F1S","CABLE,BAT",28.30,516

Out put format:
"2FS72-2F1S","CABLE,"BAT"","28.30","516"

Is there I can ignore the ","

sabeti
 
One tiny problem (the insidious off-by-one error): [tt]NF-2[/tt] ought to be [tt]NF-1[/tt]:

Code:
BEGIN {  FS=OFS="," }
{
  for ( i=NF-1; i <= NF; i++)
    $i= "\"" $i "\""
  print
}
 
good catch, futurelet - thanks!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Can someone tell me a short explanation of this line:
for (i=NF-2; i <= NF; i++)
$i= "\"" $i "\""
 
> Can someone tell me a short explanation

[tt]
for ( i=NF-1; i <= NF; i++)[/tt]
Start a "for" loop; the variable i will range from the number of fields - 1 (NF-1) to the number of fields (NF); we are working on the last 2 fields.
[tt]
$i= "\"" $i "\""[/tt]
Fields are indicated by $ followed by a number; the first field in a line is $1; the last is $NF.
Literal strings are enclosed in quotes ("); since we have a quote character within a string, we have to "escape" it, i.e., put a backslash (\) in front of it.

 
Thanks to both of you: futurelet and vgersh99
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top