Unix Shell - Using awk or perl or sed to format/combine rows

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2005 > Using awk or perl or sed to format/combine rows





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Using awk or perl or sed to format/combine rows
da

2005-10-26, 6:01 pm

Dear Group,

I have the following file.

$ cat my_input.txt

Group Id 6a -- 10 hrs

3 2 1114313 Fixed 0
3 4 1114296 Fixed 0

Group Id 7a -- 10 hrs

3 2 1114313 Fixed 0
3 4 1114296 Fixed 0
3 5 1014200 Fixed 0

I would like to take the number following the "Group Id' and stick the
value in fron of the following lines, untill another "Group Id" is met.

i.e. The output file has te be in the following format.

6a 3 2 1114313 Fixed 0
6a 3 4 1114296 Fixed 0

7a 3 2 1114313 Fixed 0
7a 3 4 1114296 Fixed 0
7a 3 5 1014200 Fixed 0


Could the above be achieved using some perl/awk or sed script? If so,
can you please give me some guidence?

Thanks!!!

Ed Morton

2005-10-26, 6:01 pm



da wrote:
> Dear Group,
>
> I have the following file.
>
> $ cat my_input.txt
>
> Group Id 6a -- 10 hrs
>
> 3 2 1114313 Fixed 0
> 3 4 1114296 Fixed 0
>
> Group Id 7a -- 10 hrs
>
> 3 2 1114313 Fixed 0
> 3 4 1114296 Fixed 0
> 3 5 1014200 Fixed 0
>
> I would like to take the number following the "Group Id' and stick the
> value in fron of the following lines, untill another "Group Id" is met.
>
> i.e. The output file has te be in the following format.
>
> 6a 3 2 1114313 Fixed 0
> 6a 3 4 1114296 Fixed 0
>
> 7a 3 2 1114313 Fixed 0
> 7a 3 4 1114296 Fixed 0
> 7a 3 5 1014200 Fixed 0
>
>
> Could the above be achieved using some perl/awk or sed script? If so,
> can you please give me some guidence?
>
> Thanks!!!
>


awk '/^Group/{grp=$3;next}{print grp,$0}' file

Ed.
Michael Tosch

2005-10-26, 6:01 pm

da wrote:
> Dear Group,
>
> I have the following file.
>
> $ cat my_input.txt
>
> Group Id 6a -- 10 hrs
>
> 3 2 1114313 Fixed 0
> 3 4 1114296 Fixed 0
>
> Group Id 7a -- 10 hrs
>
> 3 2 1114313 Fixed 0
> 3 4 1114296 Fixed 0
> 3 5 1014200 Fixed 0
>
> I would like to take the number following the "Group Id' and stick the
> value in fron of the following lines, untill another "Group Id" is met.
>
> i.e. The output file has te be in the following format.
>
> 6a 3 2 1114313 Fixed 0
> 6a 3 4 1114296 Fixed 0
>
> 7a 3 2 1114313 Fixed 0
> 7a 3 4 1114296 Fixed 0
> 7a 3 5 1014200 Fixed 0
>
>
> Could the above be achieved using some perl/awk or sed script? If so,
> can you please give me some guidence?
>
> Thanks!!!
>


awk '/^Group Id/{s=$3;print "";next}/./{print s,$0}' "my_input.txt"

--
Michael Tosch @ hp : com
William James

2005-10-26, 6:01 pm


Michael Tosch wrote:
> da wrote:
>
> awk '/^Group Id/{s=$3;print "";next}/./{print s,$0}' "my_input.txt"
>
> --
> Michael Tosch @ hp : com


awk '/^Group Id/{s=$3;print "";next}NF{print s,$0}' "my_input.txt"

da

2005-10-26, 8:51 pm

Thank you all!!!

They all worked fine.

Ed's solution printed some extra lines (group ids) when the line is
blank, but every thing else is fine.

da

2005-10-26, 8:51 pm

Also,

is there any way, I can consider the lines only if the first line start
with "Group Id" or the 4th field has "Fixed"?

Ie, is it possible to do something like

awk ' $4 ~ /Fixed/|/^Database/ {print $0}'

Thank You...

da

2005-10-26, 8:51 pm

I got it working

awk ' $4 ~ /Fixed/ || /^Group Id/ {print $0}'

The last one had copule of mistakes. I was looking at the wrong pattern
and also the syntax was incorrect.

Thanks,

Ed Morton

2005-10-27, 2:48 am

da wrote:
> I got it working
>
> awk ' $4 ~ /Fixed/ || /^Group Id/ {print $0}'


You can reduce that to just:

awk '$4 ~ /Fixed/ || /^Group Id/'

Regards,

Ed.
da

2005-10-28, 4:53 pm

Thank you!!

da

2005-10-28, 4:53 pm

Sorry to ask for more,

Is it possible to get the trailing row from each group and append to
the columns.

ex, this is my input file

======================================
Group Id 6a -- 10 hrs

3 2 1114313 Fixed 0
3 4 1114296 Fixed 0

MemberX: ok

Group Id 7a -- 10 hrs

3 2 1114313 Fixed 0
3 4 1114296 Fixed 0
3 5 1014200 Fixed 0

MemberY: ok
====================================

The output would be,
-----------------------------
MemberX 6a 3 2 1114313 Fixed 0
MemberX 6a 3 4 1114296 Fixed 0

MemberY 7a 3 2 1114313 Fixed 0
MemberY 7a 3 4 1114296 Fixed 0
MemberY 7a 3 5 1014200 Fixed 0

Thanks.

Michael Tosch

2005-10-28, 4:53 pm

da wrote:
> Sorry to ask for more,
>
> Is it possible to get the trailing row from each group and append to
> the columns.
>
> ex, this is my input file
>
> ======================================
> Group Id 6a -- 10 hrs
>
> 3 2 1114313 Fixed 0
> 3 4 1114296 Fixed 0
>
> MemberX: ok
>
> Group Id 7a -- 10 hrs
>
> 3 2 1114313 Fixed 0
> 3 4 1114296 Fixed 0
> 3 5 1014200 Fixed 0
>
> MemberY: ok
> ====================================
>
> The output would be,
> -----------------------------
> MemberX 6a 3 2 1114313 Fixed 0
> MemberX 6a 3 4 1114296 Fixed 0
>
> MemberY 7a 3 2 1114313 Fixed 0
> MemberY 7a 3 4 1114296 Fixed 0
> MemberY 7a 3 5 1014200 Fixed 0
>
> Thanks.
>

awk '/^Group Id/{i=1;gid=$3;next}
i>0&&/^[0-9]/{s[i++]=$0}
/^Member/{
for(j=1;j<i;j++)printf "%s %s %s\n",$1,gid,s[j];
print ""}' file


--
Michael Tosch @ hp : com
William James

2005-10-28, 4:53 pm


da wrote:
> Sorry to ask for more,
>
> Is it possible to get the trailing row from each group and append to
> the columns.
>
> ex, this is my input file
>
> ======================================
> Group Id 6a -- 10 hrs
>
> 3 2 1114313 Fixed 0
> 3 4 1114296 Fixed 0
>
> MemberX: ok
>
> Group Id 7a -- 10 hrs
>
> 3 2 1114313 Fixed 0
> 3 4 1114296 Fixed 0
> 3 5 1014200 Fixed 0
>
> MemberY: ok
> ====================================
>
> The output would be,
> -----------------------------
> MemberX 6a 3 2 1114313 Fixed 0
> MemberX 6a 3 4 1114296 Fixed 0
>
> MemberY 7a 3 2 1114313 Fixed 0
> MemberY 7a 3 4 1114296 Fixed 0
> MemberY 7a 3 5 1014200 Fixed 0
>
> Thanks.


ruby '
ARGF.read.scan(/^Group Id (\S+).*?\n\n(.*?)\n\n(.*?):/m){ |x|
# x is now like
# [ "6a",
# "3 2 1114313 Fixed 0
# 3 4 1114296 Fixed 0",
# "MemberX"
# ]
puts x[1].map{ |s| x[2]+" "+x[0]+" "+s}
}' infile

da

2005-10-28, 4:53 pm

Thank you! Thank you...

It worked....

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com