Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
05-02-2011, 10:56 AM
#1
LQ Newbie
Registered: May 2011
Posts: 1
Rep:
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"
Click here to see the post LQ members have rated as the most helpful post in this thread.
05-02-2011, 11:04 AM
#2
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Hi,
Give this a try:
Code:
sed 's/\(.*\) \(.*\)/"\1","\2"/' infile
Test run:
Code:
cat infile
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
sed 's/\(.*\) \(.*\)/"\1","\2"/' infile
"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"
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.
1 members found this post helpful.
05-02-2011, 03:35 PM
#3
LQ Newbie
Registered: Apr 2011
Location: Kentucky
Distribution: Ubuntu 10.10
Posts: 7
Rep:
alternately this works for multiple columns.
Code:
sed 's/^/"/; s/$/"/; s/ /","/'
05-02-2011, 04:47 PM
#4
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,002
An awk solution:
Code:
awk 'BEGIN{OFS = ("\"" " " "\"")} $1=$1{print "\"" $0 "\"" }' file
05-02-2011, 11:46 PM
#5
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,402
Slight change and alternative as my esteemed colleague has overlooked the comma required
Code:
awk 'BEGIN{OFS="\",\""}{gsub("^|$","\"")}$1=$1' file
2 members found this post helpful.
05-03-2011, 12:35 AM
#6
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,002
My distinguished colleague is right: I missed that one!
Code:
awk 'BEGIN{OFS = "\",\"")} $1=$1 {print "\"" $0 "\"" }' file
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 02:58 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News