awk/sed help
Hi All ,
Need help how do I change following file to below format. Please advice Current look ============= AMBG2K340DR_HBA01 50:01:43:80:07:28:7b:ca AMBG2K340UAT_HBA01 50:01:43:80:07:28:84:e2 AMBG2K341DR_HBA01 50:01:43:80:07:28:86:26 AMBG2K342DR_HBA01 50:01:43:80:07:28:7b:82 Modify to =========== "AMBG2K340DR_HBA01","50:01:43:80:07:28:7b:ca" "AMBG2K340UAT_HBA01","50:01:43:80:07:28:84:e2" "AMBG2K341DR_HBA01","50:01:43:80:07:28:86:26" "AMBG2K342DR_HBA01","50:01:43:80:07:28:7b:82" |
Hi,
Give this a try: Code:
sed 's/\(.*\) \(.*\)/"\1","\2"/' infile Code:
cat infile This uses back-referencing: That what is found (all between \( and \) ) in the search part can be represented as \1 (\2 etc) in the replace part. Here's an on-line sed document: Sed - An Introduction and Tutorial by Bruce Barnett Hope this helps. |
alternately this works for multiple columns.
Code:
sed 's/^/"/; s/$/"/; s/ /","/' |
An awk solution:
Code:
awk 'BEGIN{OFS = ("\"" " " "\"")} $1=$1{print "\"" $0 "\"" }' file |
Slight change and alternative as my esteemed colleague has overlooked the comma required :)
Code:
awk 'BEGIN{OFS="\",\""}{gsub("^|$","\"")}$1=$1' file |
My distinguished colleague is right: I missed that one! :)
Code:
awk 'BEGIN{OFS = "\",\"")} $1=$1 {print "\"" $0 "\"" }' file |
All times are GMT -5. The time now is 04:24 PM. |