quoting just selected words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting quoting just selected words
# 1  
Old 05-26-2010
quoting just selected words

Hi all,

i have a file that looks like:
Code:
one:two:three:four:five
six:seven:eight:nine:ten

and i'd like to quote the fourth column, getting:
Code:
one:two:three:"four":five
six:seven:eight:"nine":ten

i was thinking something like:
Code:
awk 'BEGIN{FS=":"}{print $1 FS $2 FS $3 FS \"$4\" FS $5}'

but clearly i'm wrong and seems like really not efficient.
Any idea please?

thanks

D.
# 2  
Old 05-26-2010
Try:
Code:
echo "one:two:three:four:five"| awk '$4="\""$4"\""' FS=':' OFS=':'

# 3  
Old 05-26-2010
MySQL

Hi, try with
Code:
awk 'BEGIN{FS=":"}{print $1 FS $2 FS $3 FS "\"" $4"\"" FS $5}' file_name

it's sure anybody will post a better solution...

---------- Post updated at 12:26 PM ---------- Previous update was at 12:19 PM ----------

Quote:
Originally Posted by Klashxx
Try:
Code:
echo "one:two:three:four:five"| awk '$4="\""$4"\""' FS=':' OFS=':'

Beautiful and elegant solution!
# 4  
Old 05-26-2010
Hi

thanks boths

D.
# 5  
Old 05-26-2010
Code:
awk -F: '$4="\""$4"\""' OFS=: infile

Code:
sed 's/\([^:]*\)/"\1"/4' infile

Code:
sed 's/:/&"/3;s/:/"&/4' infile



---------- Post updated at 12:58 ---------- Previous update was at 12:38 ----------

This one's a bit shorter:
Code:
sed 's/[^:]*/"&"/4' infile

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-26-2010
try
Code:
 awk -F: -v a='"' ' $4=a$4a ' OFS=: input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Help with Bash quoting

I am trying to write a BASH script that will prompt a user to enter a number of days, then calculate the date. My problem is the date command uses single or double quotes. For Example.. date -d "7 days" Here is an example of some same code I am trying to work through. echo "when do you... (4 Replies)
Discussion started by: javajockey
4 Replies

3. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

4. Shell Programming and Scripting

Osascript quoting issue

I am at a loss on this one. I am trying to run this command on a bunch of (OS 10.7.4) macs:osascript -e "tell application \"System Events\" to return name of every process whose frontmost is true" On some, it works fine. On others, I get this error: I have also tried (note the single quotes):... (3 Replies)
Discussion started by: nextyoyoma
3 Replies

5. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

6. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

7. Shell Programming and Scripting

Quoting Characters

I have this data how do i add ' ' to them like '-AAL00L' , '-BBE4577' , 'ABC' -AAL00L -BBE4577 ABC (5 Replies)
Discussion started by: dinjo_jo
5 Replies

8. Shell Programming and Scripting

awk quoting problem

I think this has to do with the quoting, I just feel I've been looking at it too long. Thanks ~T prompt> cat my.awk BEGIN{"date +%d%b%Y.%H%M%S" | getline sDate} { if (substr($0,151,1) ~ /6/ ) print >> sDate".NEW_ORDER.dat" # print >> sDate # note this works to output the contents to sDate,... (2 Replies)
Discussion started by: tcstuff
2 Replies

9. Shell Programming and Scripting

quoting question

hi guys, i have a question related to quoting but i am not sure how to formulate it... lets say we want to simulate the following shell actions cd ~/project-dir ctags /home/work/folder1/*.sh /home/work/folder2/*.sh /home/work/folder3/*.sh so i make the following script buidtags.sh ... (2 Replies)
Discussion started by: aegis
2 Replies

10. UNIX for Dummies Questions & Answers

Wildcards and quoting

Hi All In a script, I want a user to enter 4 characters, these can be a mix of letters (uppercase and lowercase) and numbers. In this example $var represents what the user has entered. eg $var can be A9xZ, 3DDL, bbHp .........etc I need to check that the user has only entered characters... (2 Replies)
Discussion started by: Bab00shka
2 Replies
Login or Register to Ask a Question