Reputation: 247
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"} }'
Upvotes: 2
Views: 1799
Reputation: 204381
$ awk '{$0="\""$0"\""; print $0 "{\n" $0 "\n}"}' file
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}
Upvotes: 5
Reputation: 67547
as a comparison with sed
$ sed 's/.*/"&"{\n"&"\n}/' file
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}
also another awk
$ awk -v OFS="\n" -v q='"' '{v=q $0 q; print v "{", v, "}" }' file
Upvotes: 2
Reputation: 195229
You can simply use printf
:
awk -v fmt='"%s"{\n"%s"\n}\n' '{printf fmt, $0,$0 }' file
Test with your data:
kent$ awk -v fmt='"%s"{\n"%s"\n}\n' '{printf fmt, $0,$0 }' f
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}
Upvotes: 1
Reputation: 23687
$ 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 escapesql = q $0 q
this adds double quotes around the input recordql "{" ORS ql ORS "}"
required output, ORS
is output record separator which is newline character by default
" }"
to get a space before }
in the outputUpvotes: 3