Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

awk or shell solution

43 views
Skip to first unread message

anidil.r...@gmail.com

unread,
May 22, 2013, 10:31:47 AM5/22/13
to

Hi
I have a file with just two lines as below :

hostname^Enclosure^Company^Model^Host^IP^Datacenter^CPU^RAM
hostname1^ENC0009^Company1^rx8640^^10.130.120.10^Datacenter1^4^64G

I want the output as below:

hostname : hostname1
Enclosure : ENC0009
Company : Company1
Model : rx8640
Host :
IP : 10.130.120.10
Datacenter : Datacenter1
CPU : 4
RAM : 64G

How can I do it?
Sounds like a few lines of code , but I am not getting the logic on how to do it.

Thanks in advance.

Joe User

unread,
May 22, 2013, 12:50:55 PM5/22/13
to
I haven't tested this:

#!/usr/bin/gawk
FNR==1 {split($0, titles, '[^]'); next}
{for (i=1; i<=NF; i++) print titles[i] " : " $i;}


--
Haiku computer error message:

Rather than a beep
Or a rude error message,
These words: "File not found."

anidil.r...@gmail.com

unread,
May 22, 2013, 1:55:27 PM5/22/13
to
Hi Joe user

Thanks for your answer. I don't have gawk.
Will it work in awk?

Thank you

Kenny McCormack

unread,
May 22, 2013, 2:05:40 PM5/22/13
to
In article <2cc94163-1aa1-4b0d...@googlegroups.com>,
anidil.r...@gmail.com <anidil.r...@gmail.com> wrote:
...
>Thanks for your answer. I don't have gawk.

Do you have a C compiler?

--
- Since the shootings on Friday, the ultra-defensive [maybe wrongly
- hyphenated, but that would be fitting] Roy "News" LIEberman has posted
- at least 90 times, and almost every single post is about his obsessive
- knee-jerk loonball [wacko] gun politics. How much longer before the
- authorities [police] finally disable the trip wires and confiscate the
- arsenal in his St. Louie hovel?

So true. So true.

anidil.r...@gmail.com

unread,
May 22, 2013, 2:16:59 PM5/22/13
to
Thanks for advise.To roll out gawk on servers , management approval is needed and will be mostly denied. We are talking about 100s of enterprise level servers. Awk is bundled in the OS.
Appreciate if you can suggest something using awk or other tools. TIA

Joe User

unread,
May 22, 2013, 4:20:32 PM5/22/13
to
On Wed, 22 May 2013 10:55:27 -0700, anidil.r...@gmail.com wrote:

> Hi Joe user
>
> Thanks for your answer. I don't have gawk. Will it work in awk?
>
> Thank you

Dude, it is two lines of code.

If you can't be bothered to type it in and try it, no one will be
bothered to help you further.

It was an example to get you started, it was not a solution.

--
We are in a crisis in the evolution of human
society. It's unique to both human and geologic
history. It has never happened before and it
can't possibly happen again. You can only use oil
once. You can only use metals once. Soon all the
oil is going to be burned and all the metals mined
and scattered.

-- M. King Hubbert, geophysicist and
energy advisor Shell Oil Company and
USGS, 1983

anidil.r...@gmail.com

unread,
May 22, 2013, 4:42:17 PM5/22/13
to
I did try right after your responded first. It is not working.

root@admin10:/tmp:-)awk 'FNR==1{split($0, titles, '[^]'); next} {for (i=1; i<=NF; i++) print titles[i] " : " $i;}' /tmp/file10
syntax error The source line is 1.
The error context is
FNR==1{split($0, titles, >>> [ <<<
awk: The statement cannot be correctly parsed.
The source line is 1.

root@admin10:/tmp:-)cat /tmp/file10
hostname^Enclosure^Company^Model^Host^IP^Datacenter^CPU^RAM
hostname1^ENC0009^Company1^rx8640^^10.130.120.10^Datacenter1^4^64G
root@admin10:/tmp:-)

When I made slight change inside split , no error , however it is still not there yet:


root@admin10:/tmp:-)awk 'FNR==1{split($0, titles, "\^"); next} {for (i=1; i<=NF; i++) print titles[i] " : " $i;}' /tmp/file10
hostname : hostname1^ENC0009^Company1^rx8640^^10.130.120.10^Datacenter1^4^64G
root@admin10:/tmp:-)

Janis Papanagnou

unread,
May 22, 2013, 5:10:04 PM5/22/13
to
The problem is that for the second data line there's no appropriate field
separator defined. It's easily fixed by specifying the -F option

awk -F^ '...'


Janis

anidil.r...@gmail.com

unread,
May 22, 2013, 5:18:28 PM5/22/13
to
Thanks. Got it working

root@admin10:/tmp:-) awk -F "^" 'FNR==1{split($0, titles, "\^"); next} {for (i=1; i<=NF; i++) print titles[i] " : " $i; }' /tmp/file10
hostname : hostname1
Enclosure : ENC0009
Company : Company1
Model : rx8640
Host :
IP : 10.130.120.10
Datacenter : Datacenter1
CPU : 4
RAM : 64G
root@admin10:/tmp:-)
0 new messages