Operating System - HP-UX
1833784 Members
4555 Online
110063 Solutions
New Discussion

Re: scripting help required

 
george_57
Occasional Advisor

scripting help required

Hi Experts,
Could somebody help me with a script that im trying to make. The idea is just reformating data....Example data:
############################
2
1.1 2.2 3.1 4.1 5.1 6.2 7.1
9.1 9.2 9.6 9.8 9.8 8.2 3.4
1.1 2.2 3.1 4.1 5.1 6.2 7.1
1
1.1 2.2 3.1 4.1 5.1 6.2 7.1
9.1 9.2 9.6 9.8 9.8 8.2 3.4
1.1 2.2 3.1 4.1 5.1 6.2 7.1
5
1.1 2.2 3.1 4.1 5.1 6.2 7.1
9.1 9.2 9.6 9.8 9.8 8.2 3.4
1.1 2.2 3.1 4.1 5.1 6.2 7.1
############################

The script should be able to go through the file and check for all lines containing 1 field and make all the associated data in to one row.
for example in this case for the first part:

2 1.1 2.2 3.1 4.1 5.1 6.2 1.1
9.1 9.2 9.6 9.8 9.8 8.2 3.4 1.1 2.2 3.1 4.1 5.1 6.2 7.1


Thanks in advance
Soji
none
8 REPLIES 8
george_57
Occasional Advisor

Re: scripting help required

2 1.1 2.2 3.1 4.1 5.1 6.2 7.1 9.1 9.2 9.6 9.8 9.8 8.2 3.4 1.1 2.2 3.1 4.1 5.1 6.2 7.1
none
Patrick Chim
Trusted Contributor

Re: scripting help required

Hi,

Here is a quick script for your purpose,

=====Begin
#!/bin/ksh

cat | grep -v "#" | while read LINE
do
if [ "$LINE" = "`echo $LINE | awk '{print $1}'`" ]
then
echo $NEWLINE
NEWLINE=$LINE
else
NEWLINE=$NEWLINE" "$LINE
fi
done
echo $NEWLINE

======End

Regards,
Patrick
Leif Halvarsson_2
Honored Contributor

Re: scripting help required

Hi

Perhaps this is what you need. Call it for example txt1 and use it "txt1

#!/usr/bin/sh
txt3()
{
case "$#" in
0)
echo $line ;;
1)
if [ -n "$line" ]
then
echo $line
fi
line=$1" " ;;
*)
line=${line}$@ ;;
esac
}

while read a
do
txt3 $a
done
txt3
Leif Halvarsson_2
Honored Contributor

Re: scripting help required

Hi

Some modifications (better handling of spaces).

#!/usr/bin/sh
txt3()
{
case "$#" in
0)
echo $line ;;
1)
if [ -n "$line" ]
then
echo $line
fi
line=$1 ;;
*)
for b in $@
do
line=${line}" "$b
done
esac
}

while read a
do
txt3 $a
done
txt3
Rodney Hills
Honored Contributor

Re: scripting help required

Soji,

If you don't mind using perl, here is a short one liner-

perl -ne 'chomp; if (/^\d+$/) { print "\n",$a if $a; $a=$_; } else { $a.= " " . $_;
} print $a;' inputfile.txt

HTH

-- Rod Hills
There be dragons...
RAC_1
Honored Contributor

Re: scripting help required

cat your_file|grep -v "#"|tr "\n" " "

There is no substitute to HARDWORK
Robin Wakefield
Honored Contributor

Re: scripting help required

Hi Soji

...using awk:

awk '{if (NF==1 && NR >1)printf("\n",$0)}{printf("%s",$0)}END{print}' file

Rgds, Robin
john korterman
Honored Contributor

Re: scripting help required

Hi George,

maybe I read too much into your question, but you could try running the attached script using your input file as par1.

regards,
John K,
it would be nice if you always got a second chance