LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-02-2011, 10:56 AM   #1
mailvaganam
LQ Newbie
 
Registered: May 2011
Posts: 1

Rep: Reputation: 0
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.
Old 05-02-2011, 11:04 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
Old 05-02-2011, 03:35 PM   #3
speedy64
LQ Newbie
 
Registered: Apr 2011
Location: Kentucky
Distribution: Ubuntu 10.10
Posts: 7

Rep: Reputation: 0
alternately this works for multiple columns.


Code:
sed 's/^/"/; s/$/"/; s/ /","/'
 
Old 05-02-2011, 04:47 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
An awk solution:
Code:
awk 'BEGIN{OFS = ("\"" " " "\"")} $1=$1{print "\"" $0 "\"" }' file
 
Old 05-02-2011, 11:46 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,577

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
Old 05-03-2011, 12:35 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
My distinguished colleague is right: I missed that one!
Code:
awk 'BEGIN{OFS = "\",\"")} $1=$1 {print "\"" $0 "\"" }' file
 
  


Reply

Tags
awk, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed/awk help Eppo Programming 19 04-09-2011 02:50 AM
need help with sed/awk jbeiter Programming 7 02-26-2010 11:13 AM
Help with awk or sed. tuxtutorials Linux - Software 1 07-23-2009 02:45 AM
awk or sed help cmontr Programming 16 05-14-2008 10:59 AM
Sed and Awk Gins Programming 7 04-19-2006 10:32 AM


All times are GMT -5. The time now is 11:06 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration