Add " " to a field with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add " " to a field with awk
# 1  
Old 03-03-2017
Add " " to a field with awk

Hello,
I would like to add " " in the last column of my file.
For example, I have:
Code:
"1";toto
"2";tata

I would like:
Code:
"1";"toto"
"2";"tata"

I tried :
Code:
awk 'BEGIN{FS=";"}$2="\""$2"\""'

and the result was:
Code:
"1";toto""

If you have any ideas, I would appreciate your help, thanks you!



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by Nathalie10; 03-03-2017 at 04:26 PM.. Reason: solved
# 2  
Old 03-03-2017
Surprising - I get
Code:
awk 'BEGIN{FS=";"}$2="\""$2"\""' file
"1" "toto"
"2" "tata"

To get the field separator right, try setting the output field sep.:
Code:
awk 'BEGIN{FS=OFS=";"}$2="\""$2"\""' file
"1";"toto"
"2";"tata"

Please be aware that above is a bit unorthodox in that it exploits awk default behaviour (print $0 if pattern is TRUE) with a pattern that is true by (sort of) coincidence. That can be done if you know exactly WHAT you are doing. Are you sure that this is the desired method / operation?

Last edited by RudiC; 03-03-2017 at 04:19 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-03-2017
humf you are right, this work!

Thanks.
# 4  
Old 03-03-2017
sed alternatives, for the second column
Code:
sed 's/[^;]\{1,\}/"&"/2' file

just for the last column as happens to be the case here:
Code:
sed 's/[^;]*$/"&"/' file

# 5  
Old 03-04-2017
Sorry, removing Perl suggestion since awk was specifically mentioned in the post title...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

2. Shell Programming and Scripting

Add "1234" to specific field in second column

i have this file 073274753,0544901701,20101201,000316 038873722,69647455,20101130,235257 26213399,0545335767,20101201,000930 063330167,0566000101,20101201,000226 026773376,11966,20101130,234429,1194 075431120,0565900600,20101201,000428 75431120,0565900600,20101201,000538... (6 Replies)
Discussion started by: maxim42
6 Replies

3. Shell Programming and Scripting

[SOLVED] How to set field separator in awk when FS is "||" ?

Dear Experts, How to use field separator in awk , when the field separation is needed as "||" Example: (file1 ) aa || bb || cc || 1234 xx || yy || zz || 1123 Q: If I want to print $3 and $4 only , with using the field separator "||" How to write awk code: (6 Replies)
Discussion started by: rveri
6 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk: Force "escaped" representation of a field

Is there any way to get the escaped version of a field in awk? For those that don't understand the question, here is a clarifying example. Lets say a field $1 gives me the string "(dumb'" (I've changed the delimiter to be something other than whitespace). If i use that value in a command in awk... (2 Replies)
Discussion started by: IvanMalison
2 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

Additional question to "awk to replace particular field"

I guess it was getting a little messy on the other post so here goes: Link to previous post for Question: https://www.unix.com/shell-programming-scripting/111338-awk-replace-particular-field.html Continuation of Question hey i was messing around a bit ... made me wonder... If the... (1 Reply)
Discussion started by: VGR
1 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question