String manipulation

I want to do the next

"I don't want to go school
because I'm sick today."

I want to join these two line but only when the first line is not more than 20 characters
and ended whit nothing or a comma and the second line not more than 15.
The 20 and the 15 can be change in the script.
I know how to join two lines but not how to count this.
thank you

Try like...

paste  -d '' - -  < test1.txt
1 Like
$join = "join";
open ( FILE ,"$join") or $rc = 101;
while( <FILE> )
{
   chomp($_);
   $First = $_  if ( $_ =~ /\s$/ );
   $length = length($_);
   if ( $length == "20" )
   {
      next;
   }
   if ( $length == "15" )
   {
      $line = $First . $_ ;
   }
   print " LENGTH ::::: $line \n";
}
close(FILE);
1 Like

Hi,
Try this one,

awk -v inpf=35 -v inps=30 '{a[NR]=$0;}END{for(i=1;i<=NR;i++){f=length(a);s=length(a[i+1]);if(f<inpf && s<inps && s!=0 && f!=0){ORS="";}else{ORS="\n";}print a;}}' file

Cheers,
Ranga:-)

1 Like

Try:

awk '1;length<=m && /[[:word:][:space:],]$/ && getline p{print (length(p)<=n?FS:RS) p}{print RS}' m=20 n=15 ORS= infile
1 Like

for Scrutinizer
I have the error awk: wrong classename: /[[:word][:space:],]$/

for rangarasan
I have the error
awk: commandrule;1: fatal: can't file '/ $/ || /\,$/{l=length($0);if(l<=f){ORS="";}print;}!/ $/ || !/\,$/{ORS="\n";l1=length($0);if(l1>s){print "\n"$0;}else{print;}}' not open for to read (file doens't exist

for anusurya
I have the errors
line 1: =: order not find
line2; fault near unexpected symbol 'FILE'
line2: `open (FILE ,"$join") or $rc =101;'
I don't understand the first line and I don't see where I must give the name of my file inside your script.
But anyway thanks for the help

Try this class instead:

awk '1;length<=m && /[[:alnum:][:space:],]$/ && getline p{print (length(p)<=n?FS:RS) p}{print RS}' m=20 n=15 ORS= infile

Does that work?

Hi, i have updated in my previous post, Please check it.

Thanks very much Scrutinizer and rangarasan.
These 2 solutions works the way I need it.
How you make these codes I don't understand it, maybe I'm to old for to learn this but I'm trying.

---------- Post updated 03-16-12 at 12:39 AM ---------- Previous update was 03-15-12 at 12:57 AM ----------

I try to change [:alnum:] with [:a-z:] because the script may not joing lines ended with numbers but this doens't work.
Than I change with [ a-zA-Z ] but here the script do nothing.

152
456
I don't want to joing
lines ended with numbers.
-----------------------------
I want to convert this to
----------------------------
152
456
I don't want to joing lines ended with numbers.

I have to learn awk because I can't always ask questions here on this forum.
But thanks anyway.

Hi,

Try this one, There is little modification in my previous post.

awk -v inpf=35 -v inps=30 '{a[NR]=$0;}END{for(i=1;i<=NR;i++){f=length(a);s=length(a[i+1]);if(f<inpf && s<inps && s!=0 && f!=0 && a !~ /[0-9]$/){ORS="";}else{ORS="\n";}print a;}}' file

If current line not ends with Numeric and current line length is less than 35 and also next line lenth is less than 30 then, we are joining the two lines. Hope i covered all your requirement.

Cheers,
Ranga:)

1 Like