Extract columns from first and second line simultaneously

This is a discussion on Extract columns from first and second line simultaneously within the awk forums in Programming Languages category; Hi, I have a text file with lines like those shown below: Power Supply #1 is OK hello Power Supply #1 is Faulty world I have to print like this hello OK -----> the second line and status from first line world Faulty Please help me in this regards. Thanks in advance for the help. Regards, Adarsh....

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-28-2008, 02:38 AM
Adarsh
Guest
 
Default Extract columns from first and second line simultaneously

Hi,
I have a text file with lines like those shown below:

Power Supply #1 is OK
hello
Power Supply #1 is Faulty
world

I have to print like this
hello OK -----> the second line and status from first
line
world Faulty

Please help me in this regards.

Thanks in advance for the help.

Regards,
Adarsh.
Reply With Quote
  #2  
Old 08-28-2008, 02:54 AM
Janis Papanagnou
Guest
 
Default Re: Extract columns from first and second line simultaneously

Adarsh wrote:
> Hi,
> I have a text file with lines like those shown below:
>
> Power Supply #1 is OK
> hello
> Power Supply #1 is Faulty
> world
>
> I have to print like this
> hello OK -----> the second line and status from first
> line
> world Faulty


If this is exact and complete data you have various possibilities.
One is to select by information whether it is on even or odd line
numbers...

awk 'NR%2 {s=$NF;next} {print $1,s}'

Or you can operate on detected Keywords...

awk '/^Power Supply/ {s=$NF;next} {print $1,s}'

Or depending on the number of words on the line...

awk 'NF>1 {s=$NF;next} {print $1,s}'


Janis

>
> Please help me in this regards.
>
> Thanks in advance for the help.
>
> Regards,
> Adarsh.

Reply With Quote
  #3  
Old 08-28-2008, 04:36 AM
Adarsh
Guest
 
Default Re: Extract columns from first and second line simultaneously

Thanks a lot.
I will go ahead with: awk '/^Power Supply/ {s=$NF;next} {print $1,s}'

-Adarsh.

On Aug 28, 11:54*am, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
> Adarsh wrote:
> > Hi,
> > I have a text file with lines like those shown below:

>
> > Power Supply #1 is OK
> > hello
> > Power Supply #1 is Faulty
> > world

>
> > I have to print like this
> > hello *OK * * * * * * *-----> the second line and status from first
> > line
> > world Faulty

>
> If this is exact and complete data you have various possibilities.
> One is to select by information whether it is on even or odd line
> numbers...
>
> * *awk 'NR%2 {s=$NF;next} {print $1,s}'
>
> Or you can operate on detected Keywords...
>
> * *awk '/^Power Supply/ {s=$NF;next} {print $1,s}'
>
> Or depending on the number of words on the line...
>
> * *awk 'NF>1 {s=$NF;next} {print $1,s}'
>
> Janis
>
>
>
> > Please help me in this regards.

>
> > Thanks in advance for the help.

>
> > Regards,
> > Adarsh.

>
>


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 07:01 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.