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??
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
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.