Computing.Net > Forums > Unix > sed awk question

sed awk question

Reply to Message Icon

Original Message
Name: cire
Date: May 6, 2005 at 08:04:44 Pacific
Subject: sed awk question
OS: HPUX
CPU/Ram: 16way 875mhz/20gig ram
Comment:

A question for ya. Using sed or awk or both. Below is an example of a file. These are accounts. From the record PT to the next PT is one account. I want to strip off the carriage return & line feeds and have one account be in one long record. Make sence??

PT293457034957234572345703953045723045720345
GT102308412034-2-4-0
IN129374959739945893975398475
IN1902347502938457
TR89023495345890
CO9324572304857
EN190234753478
PT293457034957234572345703953045723045720345
GT102308412034-2-4-0
IN129374959739945893975398475
IN1902347502938457
TR89023495345890
CO9324572304857
EN190234753478
PT293457034957234572345703953045723045720345
GT102308412034-2-4-0
IN129374959739945893975398475
IN1902347502938457
TR89023495345890
CO9324572304857
EN190234753478

eric


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: May 6, 2005 at 11:17:43 Pacific
Subject: sed awk question
Reply: (edit)

Here's one example:

#!/bin/ksh

fr=1
while read line
do
if [[ $(echo "$line"|cut -c1-2) = "PT" && (fr -ne 1) ]]
then
printf "\n%s" $line
else
printf "%s" $line
fi
fr=0
done < datafile
# end script

read the file a line at a time and except for the first line, whenever the line starts with "PT" print that line beginning with a CR otherwise don't print the CR



Report Offensive Follow Up For Removal

Response Number 2
Name: WilliamRobertson
Date: May 8, 2005 at 11:45:24 Pacific
Subject: sed awk question
Reply: (edit)

Here's similar logic in awk. Save script, then execute as:
scriptname filename.dat

#!/usr/bin/awk -f

{
if ($0 ~ /^PT..*/) {
# Print old concatenated value,
# then restart new one:
if (conc_value ~ /^PT..*/) {
print conc_value
printed="TRUE"
}
conc_value = $0
}
else if (conc_value ~ /^PT..*/) {
conc_value = conc_value $0
printed="FALSE"
}
}

END {
if (printed = "FALSE") print conc_value
}


Report Offensive Follow Up For Removal

Response Number 3
Name: WilliamRobertson
Date: May 8, 2005 at 14:43:15 Pacific
Subject: sed awk question
Reply: (edit)

Just for fun, I tried writing my awk logic in ksh:

#!/bin/ksh

while read line
do
if [[ ${line} = PT* ]]
then
if [[ ${conc_value} = PT* ]]
then
print ${conc_value}
printed="TRUE"
fi
conc_value=${line}
elif [[ ${conc_value} = PT* ]]
then
conc_value=${conc_value}${line}
printed="FALSE"
fi
done < $1

if [[ ${printed} = "FALSE" ]]
then
print ${conc_value}
fi

It runs in about twice the time of the awk version (not usually a problem unless the file is huge).

I'm sure there is something totally cryptic you could write in sed or Perl that would run like blazes.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed awk question

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software