Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert column in one line and also add ' and ,

Status
Not open for further replies.

netbackup1234

Programmer
Aug 27, 2006
24
US
Hi
I have column like this
CARBON
CBDSWTS08
CBDSAPPNUS01
CBDSWTS09
CBDSWTS1
CCURE_HOST

And i want output like following
('CARBON','CBDSWTS08','CBDSAPPNUS01','CBDSWTS09','CBDSWTS1','CCURE_HOST')

How do i achive this with awk ?
 
Hi
Feherke

I am new to awk. Would you explain each pieces in above awk program ?

Your help apprciated.
 
Code:
# run awk and set the value of variable q to "'"
awk -vq="'" '
    # before processing any input, print a "("
    BEGIN{ printf"(" }
    # for each line of input if the current record number
    # is greater than 1, print a ",", otherwise nothing,
    # followed by the contents of variable q, the current
    # line, and then the contents of variable q again
    {printf(NR>1?",":"")q$0q}
    # when all input processed, print a ")"
    END{print")"}
' /input/file

Annihilannic.
 
Hi
Annihilannic

i am still confuse following ( NR>1?",":"")
If NR greater than 1 then put "," then what is role of ":" ?
 
Code:
( NR>1?",":"")

in plain-English: if 'NR>1', return ","
otherwise return ""

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top