Mohan Timilsina
Mohan Timilsina

Reputation: 500

Adding the double quote in the first field of the file

I have a file like this where the field are tab separated:

http://article.wn.com/view/wnat51e64f5c0a06e3e18f45e66d5185fc04/                                                        0       0       0       0       0       0       0       0       0       0       0       0  
http://newsok.com/ward-blanks-bruins-in-hurricanes-3-0-win./article/feed/217313?custom_click=rss                        0       0       0       0       0       0       0       0       0       0       0       0  
http://www.neurosoftware.ro/finance/insurance/stock-market/zoom-ctch-wvvi-hiru-couv-stock-alerts-from-stock-pr-com/     0       0       0       0       0       0       0       0       0       0       0       0  

I wanted to add the double quote in the first column of file like this

"http://article.wn.com/view/wnat51e64f5c0a06e3e18f45e66d5185fc04/"                                                        0       0       0       0       0       0       0       0       0       0       0       0  
"http://newsok.com/ward-blanks-bruins-in-hurricanes-3-0-win./article/feed/217313?custom_click=rss"                        0       0       0       0       0       0       0       0       0       0       0       0  
"http://www.neurosoftware.ro/finance/insurance/stock-market/zoom-ctch-wvvi-hiru-couv-stock-alerts-from-stock-pr-com/"     0       0       0       0       0       0       0       0       0       0       0       0  

I tried this:

awk -F \t '{sub($1, "\"&\""); print}' file

I got the result like this:

"h"ttp://article.wn.com/view/wnat51e64f5c0a06e3e18f45e66d5185fc04/                                                        0       0       0       0       0       0       0       0       0       0       0       0  
"h"ttp://newsok.com/ward-blanks-bruins-in-hurricanes-3-0-win./article/feed/217313?custom_click=rss                        0       0       0       0       0       0       0       0       0       0       0       0  
"h"ttp://www.neurosoftware.ro/finance/insurance/stock-market/zoom-ctch-wvvi-hiru-couv-stock-alerts-from-stock-pr-com/     0       0       0       0       0       0       0       0       0       0       0       0 

Are there any suggestions to fix this?

Upvotes: 1

Views: 95

Answers (6)

potong
potong

Reputation: 58371

This might work for you (GNU sed):

sed 's/\S\+/"&"/' file

Surround one or more non-spaces by double quotes.

Upvotes: 0

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14945

Another way using gensub funtion from gawk:

gawk '{print gensub(/^([^[:space:]]+)/, "\"&\"", "")}' infile

NOTE: Don´t have to worry about FS and OFS values.

Upvotes: 1

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

With awk:

awk -v OFS="\t" '{$1= "\""$1"\""; print}' file

Upvotes: 0

fedorqui
fedorqui

Reputation: 289535

This can be easily done with sed:

sed -r 's/^([^\t]+)/"\1"/' file

It catches the first block of text before a tab (+ to match at least one character, thanks Jidder in comments!) and prints it back surrounded by double quotes.

Also, your approach would be good if you used -F"\t":

awk -F"\t" '{sub($1, "\"&\""); print}' file

Whereas this approach might be better (thanks anubhava in comments!)

awk 'BEGIN{FS=OFS="\t"} NF{$1="\"" $1 "\""}1' file

This sets input and output field separator to tab. Then, in case there are some fields (NF being at least one, that is, no empty lines) it adds the quotes around the first field. Then, 1 performs the default awk action: print line.

Upvotes: 1

user3442743
user3442743

Reputation:

Easily done with awk

awk '$1="\""$1"\""' OFS="\t" file

If there are blank lines

awk 'NF&&$1="\""$1"\""' OFS="\t" file

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

echo \t
t

The bash literal for a tab character is $'\t'.

Upvotes: 0

Related Questions