reformatting txt file

Need a script ? Ask here.

reformatting txt file

Postby japie » Mon Mar 08, 2010 11:51 am

Hello,

Can someone help me out with reformatting a text file?
The file has space separated entrys and contains blocks of 6 lines.

From the first line I need to delete the first entry (which is 6 characters including the whitepace).
The second and 3th line need no modification.
From the 4th line I only need the first entry.
From the 5th line I need the first 2 entrys but with the whitespace removed.
The 6th line is empty and the above block of lines starts again at line 7.

I have been struggling with sed and awk but can't make it happen.
japie
 
Posts: 10
Joined: Sun May 31, 2009 11:29 pm

Re: reformatting txt file

Postby G4143 » Mon Mar 08, 2010 6:45 pm

Could you post an example of the text file...before and after.
G4143
 
Posts: 24
Joined: Tue Mar 02, 2010 9:55 pm
Location: Canada

Re: reformatting txt file

Postby G4143 » Mon Mar 08, 2010 7:22 pm

I tried it in awk and got this..Here's the starting text

Code: Select all
1one 2one 3one 4one 5one 6one 7one 8one 9one 10one
1two 2two 3two 4two 5two 6two 7two 8two 9two 10two
1three 2three 3three 4three 5three 6three 7three 8three 9three 10three
1four 2four 3four 4four 5four 6four 7four 8four 9four 10four
1five 2five 3five 4five 5five 6five 7five 8five 9five 10five

1one 2one 3one 4one 5one 6one 7one 8one 9one 10one
1two 2two 3two 4two 5two 6two 7two 8two 9two 10two
1three 2three 3three 4three 5three 6three 7three 8three 9three 10three
1four 2four 3four 4four 5four 6four 7four 8four 9four 10four
1five 2five 3five 4five 5five 6five 7five 8five 9five 10five

1one 2one 3one 4one 5one 6one 7one 8one 9one 10one
1two 2two 3two 4two 5two 6two 7two 8two 9two 10two
1three 2three 3three 4three 5three 6three 7three 8three 9three 10three
1four 2four 3four 4four 5four 6four 7four 8four 9four 10four
1five 2five 3five 4five 5five 6five 7five 8five 9five 10five


Here's what awk produced
Code: Select all
2one 3one 4one 5one 6one 7one 8one 9one 10one
1two 2two 3two 4two 5two 6two 7two 8two 9two 10two
1three 2three 3three 4three 5three 6three 7three 8three 9three 10three
1four
1five2five

2one 3one 4one 5one 6one 7one 8one 9one 10one
1two 2two 3two 4two 5two 6two 7two 8two 9two 10two
1three 2three 3three 4three 5three 6three 7three 8three 9three 10three
1four
1five2five

2one 3one 4one 5one 6one 7one 8one 9one 10one
1two 2two 3two 4two 5two 6two 7two 8two 9two 10two
1three 2three 3three 4three 5three 6three 7three 8three 9three 10three
1four
1five2five


The awk script:
Code: Select all
#! /usr/bin/awk -f

BEGIN   {
      cline = 0;   
   }

{
   if (++cline == 6)
   {
      cline = 0;
      printf("%s\n", $0);
   }
   else if (cline == 1)
   {
      for (i = 2; i <= NF; ++i)
      {
         if (i == NF)
         {
            printf("%s\n", $i);
         }
         else
         {
            printf("%s ", $i);
         }
      }
   }
   else if (cline == 2 || cline == 3)
   {
      printf("%s\n", $0);
   }
   else if (cline == 4)
   {
      printf("%s\n", $1);
   }
   else if (cline == 5)
   {
      printf("%s%s\n", $1,$2);
   }
}

END   {
      
   }


Let me know if this works out..
G4143
 
Posts: 24
Joined: Tue Mar 02, 2010 9:55 pm
Location: Canada

Re: reformatting txt file

Postby Watael » Mon Mar 08, 2010 8:54 pm

some bash basics:
Code: Select all
#!/bin/bash

while read line
do case $((++lnb)) in
      1) line="${line:6}" ;;
      4|5) set -- $line
           [ $lnb -eq 4 ] && line=$1 || line=$1$2
      ;;
      6) unset lnb
      ;;
   esac
   echo "$line"
done < G4143.txt
=))
Watael
 
Posts: 232
Joined: Mon Mar 02, 2009 3:03 am

Re: reformatting txt file

Postby G4143 » Mon Mar 08, 2010 9:54 pm

Watael wrote:some bash basics:
Code: Select all
#!/bin/bash

while read line
do case $((++lnb)) in
      1) line="${line:6}" ;;
      4|5) set -- $line
           [ $lnb -eq 4 ] && line=$1 || line=$1$2
      ;;
      6) [ $lnb -eq 6 ] && unset lnb
      ;;
   esac
   echo "$line"
done < G4143.txt
=))


Yup that's some pretty 'basic bash'...Nice code...Didn't know you could do that in bash.
G4143
 
Posts: 24
Joined: Tue Mar 02, 2010 9:55 pm
Location: Canada

Re: reformatting txt file

Postby japie » Tue Mar 09, 2010 12:14 am

Thanks guys!

Both solutions worked but not instantly. It seemed that the first line contained some evil characters my editor didn't show, tr -cd '\11\12\40-\176' made it clean.
The text file was part of an openbasic database on a died Sco machine used at our local rifle club, since they assume I am the *nix specialist... So thanks for your help!
japie
 
Posts: 10
Joined: Sun May 31, 2009 11:29 pm


Return to Requests

Who is online

Users browsing this forum: No registered users and 1 guest

cron