UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old September 27th, 2004, 02:43 PM
kielmar kielmar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 kielmar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
text file manipulation

Given a text file containing the following data:

C, QTR 1,
, Jan, 1.00
, Feb, 1.00
, Mar, 1.00

I need to create a file like:

QTR 1, Jan
QTR 1, Feb
QTR 2, Mar

I thought using an awk command would be the best way, but I do not know how to assign a variable that is valid for more than one record.

The statement I tried:

awk -F, '{if ($1=="C") x==$2; else if ($1!="C") print x,$2}' filename.cma

The result is a blank field for x and the $2 variable for the current record. Please help!!

TIA

Reply With Quote
  #2  
Old September 28th, 2004, 02:07 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
Close...

Code:
awk -F, '{if ($1=="C") {x=$2} else {print x","$2}}'


To get rid of leading spaces, pipe it through sed (note that there are two spaces before the *):
Code:
awk -F, '{if ($1=="C") {x=$2} else {print x","$2}}' \
  |sed -e 's/^  *//'

Reply With Quote
  #3  
Old September 28th, 2004, 09:32 AM
kielmar kielmar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 kielmar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
text file

thanks for the response...

but using:

awk -F, '{if ($1=="C") {x=$2} else {print x","$2}}' \
|sed -e 's/^ *//' filename.txt

on the following file:

C, QTR 1,
, Jan, 1.00
, Feb, 1.00
, Mar, 1.00

only reprints the file and hangs...is there a method for referencing a variable in the previous line of a file??

i'd really like to yield the following from the above file:

QTR 1, Jan
QTR 1, Feb
QTR 1, Mar

I can do it in Oracle PL/SQL using a cursor for loop, but I think it would be easier / more efficient to do in awk/sed...somehow....

Reply With Quote
  #4  
Old September 28th, 2004, 12:00 PM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
The first snippet should have worked.

Sorry. My second one wasn't clear. You'd need to run it as below:
Code:
awk -F, '{if ($1=="C") {x=$2} else {print x","$2}}' filename.txt \
  |sed -e 's/^ *//' 

Reply With Quote
  #5  
Old September 28th, 2004, 01:55 PM
kielmar kielmar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 kielmar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up Perfect!!

Perfect!!!

Thank you!!!

Reply With Quote
  #6  
Old October 6th, 2004, 05:40 AM
Muthukumar_K Muthukumar_K is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 Muthukumar_K User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Muthukumar_K
We can get your desired one as,
awk '{ if ( NR == 1 ) name=$2" "$3;
> if ( NR != 1 ) { split($2,a,","); print name" "a[1] } }' logfile
QTR 1, Jan
QTR 1, Feb
QTR 1, Mar

where logfile contains your input file.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > text file manipulation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway