1751781 Members
3938 Online
108781 Solutions
New Discussion юеВ

Making 5 lines into 1

 
SOLVED
Go to solution
Chris Frangandonis
Regular Advisor

Making 5 lines into 1

Hi All,

For eg:

I have 100 lines which
could read in a single line
and so on , +++11 122321
I have 200 lines which
could read in a single line

The result would be :
I have 100 lines which could read in a single line and so on , +++11 122321.
I have 200 lines which could read in a single line


Thanks in advance
Chris

12 REPLIES 12
Bryan_6
Frequent Advisor

Re: Making 5 lines into 1

If the text is in a file, try this:

cat textFile | while read line
do
print "$line \c" >> newTextFile
done
ian Dennison
Regular Advisor

Re: Making 5 lines into 1

Use awk with the printf command,

eg cat file1 |awk '{printf("%-s", $0)}'

This will not print line feeds by default.

If you want to group by a certain number of lines, then increment a counter in the awk command and every time it reaches the limit, perform a 'printf("\n")'.

Share and Enjoy! Ian
Lets do it to them before they do it to us! www.fred.net.nz
Krishna Prasad
Trusted Contributor

Re: Making 5 lines into 1

If you can identfy the begining of a line of data you can use awk to do this.

You could do something like this. I don't know how long your possible text string can be so you might need to change the 130 value.

awk ' $0~/^I have 100/
{ TXT = substr($0,1,130);
}
{ printf "%s\n",TXT } ' input.file
Positive Results requires Positive Thinking
Carlos Fernandez Riera
Honored Contributor

Re: Making 5 lines into 1



awk ' $0 ~ "^I have" {
printf "%s", $0
times=$3+0
for (i=1 ; i< times; i++) {
getline
printf "%s", $0
} ; printf"\n"}' <I have 3 lines which
could read in a single line
and so I have on , +++11 122321
I have 2 lines which
could read in a single line
END
unsupported
Chris Frangandonis
Regular Advisor

Re: Making 5 lines into 1

Hi All,

Thanks a lot,

Carlos,

Thanks ,it works but what if the lines are > than 4 lines , and should have a carriage return when the word "I have" appears. Perhaps
my explanation was blurry.
Steven Sim Kok Leong
Honored Contributor

Re: Making 5 lines into 1

Hi,

Taking bryan's solution,

#!/sbin/sh
cat textFile | while read line
do
if echo $line|grep "I have" >/dev/null 2>&1
then
print "\n" >> newTextFile
fi
print "$line \c" >> newTextFile
done

Hope this helps. Regards.
Robin Wakefield
Honored Contributor
Solution

Re: Making 5 lines into 1

Hi Chris,

Try:

awk '
/^I have/{printf "%s%s ",sep,$0;sep="\n";next}
{printf "%s",$0}
END{print}
' yourfile

This should format your file with no more carriage returns than necessary.

Rgds, Robin.
Chris Frangandonis
Regular Advisor

Re: Making 5 lines into 1

Hi Robin,

Thanks a ton, now finallay could you break it up in english especially the "sep" part

Many Thanks
Chris
Robin Wakefield
Honored Contributor

Re: Making 5 lines into 1

Hi Chris

awk '
/^I have/{printf "%s%s ",sep,$0;sep="\n";next}
{printf "%s",$0}
END{print}
' yourfile

Until it's explicitly defined, the sep variable will be empty - therefore the first time "I have" is encountered, this will be the case, and it will print this line without a leading carriage return (CR). Once this has happened, sep is then defined as a CR, so that subsequent "I have" matches ARE preceded by a CR. All other lines are printed as found. The END statement ensures the last line finished off with a CR.

Rgds, Robin.