Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add special characters in a text using awk

Tags:

awk

I have a file which is :

line1
line2
line3

What I am trying to have is

"line1"{
"line1"
 }

I am trying to do this using awk but I don't know how to use the special characters. For now I have this.

awk '{ "$0" {"$0"} }' 
like image 212
oezlem Avatar asked Feb 19 '26 00:02

oezlem


2 Answers

$ awk '{$0="\""$0"\""; print $0 "{\n" $0 "\n}"}' file
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}
like image 182
Ed Morton Avatar answered Feb 20 '26 16:02

Ed Morton


$ awk -v q='"' '{ql = q $0 q; print ql "{" ORS ql ORS "}" }' ip.txt
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}
  • -v q='"' save the double quote character in variable named q, makes it easier to insert double quotes instead of messing with escapes
  • ql = q $0 q this adds double quotes around the input record
  • ql "{" ORS ql ORS "}" required output, ORS is output record separator which is newline character by default
    • space between the different parameters is ignored, use " }" to get a space before } in the output
like image 43
Sundeep Avatar answered Feb 20 '26 14:02

Sundeep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!