|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
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
|
|||
|
|||
|
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 ![]() |
|
#2
|
|||
|
|||
|
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/^ *//'
|
|
#3
|
|||
|
|||
|
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.... |
|
#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/^ *//'
|
|
#5
|
|||
|
|||
|
Perfect!!!
Thank you!!! |
|
#6
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > text file manipulation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|