1834093 Members
2333 Online
110063 Solutions
New Discussion

Re: awk/sed/tr problem

 
SOLVED
Go to solution
alec pringle
Frequent Advisor

awk/sed/tr problem

Hi,

I have a file of the format:

start: 1001
12
32
34
35
36

start: 1002
asd
we
23

I want to format this output to read:
start: 1001, 12, 32, 34, 35, 36
start: 1002, asd, we, 23

..so in other words remove line feeds and use the keywords start,and a blank line to delimit the start and end of a record (as you can see these records are of variable length. So I know I can remove line feeds with things like tr (tr -d '\n' < file.txt), but can't work out how I can get this to work with record delimiters. I tried stuff in awk and sed but can't work this one out - any ideas?
7 REPLIES 7
Ken Penland_1
Trusted Contributor

Re: awk/sed/tr problem

Someone will probably have a prettier way of doing it, but here is one way:

#!/usr/local/bin/perl
foreach $line(`cat input`)
{
$line =~ s/\s+$//;
if ($line eq "") { next; }
if(grep/start/,$line)
{
print "\n";
print "$line, ";
}
else { print "$line, "; }
}
print "\n";
'
Rajeev Tyagi
Valued Contributor
Solution

Re: awk/sed/tr problem

alec,

Assuming your data contained in /tmp/file you can use awk as follows.

awk -F":" '{if($1=="start") \
printf "\n%s",$0 \
else \
printf " %s",$0 \
}' /tmp/file

Hope this helps. Please take care of newline after each statement.
Gordon  Morrison_1
Regular Advisor

Re: awk/sed/tr problem

#!/bin/ksh
newline=""
infile=./testfile
outfile=./test.out
> $outfile

cat $infile | while read line
do
if [[ -z $line ]] ; then
newline="TRUE"
elif echo "${line}" | grep "^start" > /dev/null ; then
if [[ $newline = "TRUE" ]] ; then
echo >> $outfile
fi
echo "${line}\c" >> $outfile
newline=""
else
echo ", ${line}\c" >> $outfile
newline=""
fi
done
What does this button do?
Jean-Luc Oudart
Honored Contributor

Re: awk/sed/tr problem

#!/bin/sh

awk '
{
if($1=="start:") { printf("\n%s",$0);}
else { if(length($0) > 1) printf(", %s",$0);}
}
END{printf("\n");}'

Regards
Jean-Luc
fiat lux
Peter Godron
Honored Contributor

Re: awk/sed/tr problem

Alec,
as a sh shell script (assuming your data is in a.lis)
#!/usr/bin/sh
while read record
do
if [ -z "$record" ]
then
echo $data
data=""
else
if [ `expr substr "$record" 1 6` = "start:" ]
then
data="$record"
else
data="$data,$record"
fi
fi
done < a.lis

To extract data between reg. expressions use:
sed -n '/reg1/,/reg2/p'


Regards
c_51
Trusted Contributor

Re: awk/sed/tr problem

this worked for me

cat yourFile | awk '
/^start/ { printf("%s", $0); next;}
NF > 0 {printf(", %s", $0); next;}
{printf("\n"); #blankline
}'
alec pringle
Frequent Advisor

Re: awk/sed/tr problem

Hi, thanks for all your help. I used the awk method in the end as it was closest to way I was trying to do it.