Help Please. Need to add quotes to o field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Please. Need to add quotes to o field
# 8  
Old 02-10-2006
Here is the code .....

Quote:
cat test.txt
K112 E-3433 SHOE REPAIR KIT 3
K119 E-1887ASHD SHOE REPAIR KIT 6
K128 E-2769SHD SHOE REPAIR KIT 3
K128K E-2769HD & SHD SHOE REPAIR KIT 15

awk -F"\t" '{ print "\""$1"\"""\t""\""$2"\"""\t""\""$3"\"""\t""\""$4"\"" }' test.txt

"K112" "E-3433" "SHOE REPAIR KIT" "3"
"K119" "E-1887ASHD" "SHOE REPAIR KIT" "6"
"K128" "E-2769SHD" "SHOE REPAIR KIT" "3"
"K128K" "E-2769HD & SHD" "SHOE REPAIR KIT" "15"
isn't this what you wanted.....
# 9  
Old 02-10-2006
thanks. I have my fields surrounded by " (quotes) like I need, Now my problem is the fields are seperated by 'tabs' how do I change it to be seperated by 'space' ???
Not sure how to specify a 'space' or a 'tab'
# 10  
Old 02-10-2006
Quote:
Originally Posted by tjbopp
thanks. I have my fields surrounded by " (quotes) like I need, Now my problem is the fields are seperated by 'tabs' how do I change it to be seperated by 'space' ???
Not sure how to specify a 'space' or a 'tab'
borrowing from futurelet....
Code:
awk 'BEGIN { FS = "\t" ; q="\"";  OFS = q " " q }
{ $1 = $1; print q $0 q }'  oldfile >newfile

# 11  
Old 02-10-2006
Quote:
Originally Posted by tjbopp
thanks. I have my fields surrounded by " (quotes) like I need, Now my problem is the fields are seperated by 'tabs' how do I change it to be seperated by 'space' ???
Not sure how to specify a 'space' or a 'tab'
with space....
Quote:
awk -F"\t" '{ print "\""$1"\""" ""\""$2"\""" ""\""$3"\""" ""\""$4"\"" }' test.txt
# 12  
Old 02-10-2006
Try this

This will take care of your situation not matter how many fields you have on a line.

sed -e 's/^/\"/g' -e 's/\tab/\"\tab\"/g' -e 's/$/\"/g' < input_file

Do not try and copy and paste the command. Substitute the tab with acutal tabs.
# 13  
Old 02-10-2006
The way it works:

Before reading any lines from the file, change the field-separator, set the variable q to a double quote, and change the ouput field-separator:
Code:
BEGIN { FS = "\t" ; q="\"";  OFS = q " " q }

This is executed for each line of the file:
Code:
{ $1 = $1; print q $0 q }

$0 is the line just read; $1 is the first field. When you change one of the fields (or pretend to change it by saying "$1 = $1"), awk rebuilds $0 from the individual fields by placing OFS between each pair of fields. So, if $0 is
Code:
foo bar

, after executing $1 = $1, $0 is
Code:
foo" "bar

.
The last step is to print $0 with a quote before and after it.
# 14  
Old 02-10-2006
Thanks to everyone.
I appreciate it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

Remove quotes and commas from field

In the attached file I am trying to remove all the "" and , (quotes and commas) from $2 and $3 and the "" (quotes) from $4. I tried the below as a start: awk -F"|" '{gsub(/\,/,X,$2)} 1' OFS="\t" enhancer.txt > comma.txt Thank you :). (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

awk - single quotes as field separator

How can I use single quotes as field separator in awk? (1 Reply)
Discussion started by: locoroco
1 Replies

4. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

5. UNIX for Dummies Questions & Answers

Cut a field from a line having quotes as delimiter

Hi , I have extract a single field from the 2nd row of a file of which the format is as given below "Field1","Field2","Field3",...,"Field17",... I need to cut Field17 as such (no quotes required).. I give the command head -2 file_name | tail -1 | cut -d "," -f17 But the output is... (2 Replies)
Discussion started by: nivin_govindan
2 Replies

6. Shell Programming and Scripting

Add field delimiter for the last field

I have a file with three fields and field delimiter '|' like: abc|12:13:45|123 xyz|12:87:32| qwe|54:21:09 In the file the 1st line has proper data -> abc|12:13:45|123 ,the 2nd line doesnt has data for the 3rd field which is okay , the 3rd line doesnt has data for the 3rd field as well the... (5 Replies)
Discussion started by: mehimadri
5 Replies

7. Shell Programming and Scripting

awk field equal something, then add something to the field

Hi Everyone, a.txt a b c 1 e e e e e a b c 2 e e e e e the output is a b c 1 e e e e e a 00b c 2 e e e e e when 4th field = '2', then add '00' in the front of 2nd field value. Thanks (9 Replies)
Discussion started by: jimmy_y
9 Replies

8. Shell Programming and Scripting

Add double quotes around the string

I have a line in multiple scripts:select into table /dir1/dir2/file.dat dir1 and dir2 are the same but file.dat is different from script to script. I need to include /dir1/dir2/file.dat into double quotes in each file of my directory:select into table "/dir1/dir2/file.dat" (13 Replies)
Discussion started by: surfer515
13 Replies

9. UNIX for Dummies Questions & Answers

Add single quotes in string

Hi All, I love this site, it helps newbie people like me and I appreciate everyone's help! Here is my questions. I am trying to concatenate a single quote into a character/string from a text file for each line (lets say ABC should look like 'ABC'). I tried to use awk print command to do... (1 Reply)
Discussion started by: mrjunsy
1 Replies

10. Shell Programming and Scripting

add increment field when first field changes

Hi I have a file that looks like the following: Name1 aaa bbbb Name1 ffd hhghg Name1 dffg ghhhh Name2 rtrt dfff Name2 rrtfr tgtt Name2 dsddf gfggf Name2 ffffg gfgf NAme3 fdff ghhgh Is it possible to format it so that a number... (2 Replies)
Discussion started by: azekry
2 Replies
Login or Register to Ask a Question