put double quotes for a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting put double quotes for a column
# 1  
Old 05-09-2007
put double quotes for a column

Hi,

I have a file which looks like this, I have stripped most of the columns:

2006,UCA,"02452","NM","1","M","84",123,aa
2006,UCA,"02452","NM","1","L","84",123,bb
2006,UCA,"02452","NM","1","L","84",432,cc
2006,UCA,"02452","NM","1","L","33",213,dd
2006,UCA,"02452","NM","1","L","33",124,ee
2006,UCA,"02452","NM","5","H","84",325,ff
2006,UCA,"02452","NM","1","H","84",233,dd
2006,UCA,"02452","NM","1","H","84",543,dd
2006,UCA,"02452","NM","5","H","84",235,ff
2006,UCA,"02452","NM","1","L","33",233,dd

I have to replace second column UCA with "MMU". Mind you its not just MMU its MMU with double quotes.

I have replaced UCA with MMU but could not put double quotes.

2006,MMU,"02452","NM","1","M","84",123,aa
2006,MMU,"02452","NM","1","L","84",123,bb
2006,MMU,"02452","NM","1","L","84",432,cc
2006,MMU,"02452","NM","1","L","33",213,dd
2006,MMU,"02452","NM","1","L","33",124,ee
2006,MMU,"02452","NM","5","H","84",325,ff
2006,MMU,"02452","NM","1","H","84",233,dd
2006,MMU,"02452","NM","1","H","84",543,dd
2006,MMU,"02452","NM","5","H","84",235,ff
2006,MMU,"02452","NM","1","L","33",233,dd

So my awk looks like this :

awk -F, '{OFS=","; if ($2~/UCA/) print $1,"MMU",$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14 }' test

Is there any way to do this and not type each column number like $3,$4,$5...

Thanks
Sumeet
# 2  
Old 05-09-2007
Code:
nawk -F, -v OFS=',' -v qq='"' '$2 ~ "UCA" {$2 = qq "MMU" qq }1' test

# 3  
Old 05-09-2007
I think this should work too:
awk -F, '{OFS=","; if ($2~/UCA/) $2="\"MMU\"" ; print $0 }'
# 4  
Old 05-09-2007
even I got the answer ..was simple just overlooked

awk -F, '{ OFS=",";gsub (/UCA/,"MMU",$2); print $0}' stest5 -- without double quotes

awk -F, '{ OFS=",";gsub (/UCA/,"MMU",$2); print $0}' mtest > mtest3 -- with double quotes

sed 's/UCA/"MMU"/g' stest5 -- if double quotes not there

sed 's/UCA/MMU/g' stest7 -- if double quotes there

Thanks people for helping
Sumeet
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need shell script to append double quotes for each column in a file

Hi Experts, I am beginner to the shell scripting, My requirement is to append double quotes for each column in a file if double quotes does not exist. Example: "abc"|123|"gh-ch"|23.067 Use code tags, thanks. (10 Replies)
Discussion started by: spidy
10 Replies

2. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

3. Shell Programming and Scripting

Handling embedded double quotes within column data

I have a text file where each field is enclosed in double quotes and separated by a comma. But in certain rows we have embedded double quotes within column data For e.g """TRUSPICE CENTRE"" CLAYTON AU" The above value is for a single column but there are embedded quotes within... (2 Replies)
Discussion started by: abhilashnair
2 Replies

4. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

5. Shell Programming and Scripting

Put double quotes around numbers

Hi, consider a file which has data such as "random text",912345,"54","finish" "random text",9991236745,"9954","finish" I want to replace the numbers that don't have double quotes around them with ones that do; so the output should be "random text","912345","54","finish" "random... (4 Replies)
Discussion started by: Storms
4 Replies

6. Programming

Macro to put single quotes around character

Dear Mates, I am trying to write a macro to replace with its arguments with singles quotes around each argument. #define DECR(a,b,c,d,e) decr('a','b','c','d','e') if the usage is DECR(k,e,y,s,\0) I want it to be replaced with the following decr('k','e','y','s','\0') However it... (2 Replies)
Discussion started by: tamil.pamaran
2 Replies

7. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

8. Shell Programming and Scripting

Single quotes and double quotes

Hi guys, I have a sed line in double quotes which works fine, but I want it to be in single quotes here is the sed line sed "/abc_def/s/\'.*\'/\'\${abc_def}\'/" can some one give the equivalent to the above script in single quotes Thanks a ton (5 Replies)
Discussion started by: sol_nov
5 Replies

9. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies

10. UNIX for Advanced & Expert Users

how to put quotes ina .sql file

Hi i am new on Unix.......... I have to write a ddl and for tables and I am using single quotes in the comment for the table and its column...........but when i see on the unix server the ' dont show . e.g. DROP TABLE DMR_PURGED; CREATE TABLE DMR_PURGED ( HEALTH_SERVICE_ID ... (9 Replies)
Discussion started by: agarwalniru
9 Replies
Login or Register to Ask a Question