Register
It is currently Tue Sep 30, 2014 9:51 am

reformatting txt file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Mon Mar 08, 2010 11:51 am   

Joined: Sun May 31, 2009 11:29 pm
Posts: 10
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.


Top
 Profile  
 PostPosted: Mon Mar 08, 2010 6:45 pm   

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


Top
 Profile  
 PostPosted: Mon Mar 08, 2010 7:22 pm   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
I tried it in awk and got this..Here's the starting text

Code:
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:
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:
#! /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..


Top
 Profile  
 PostPosted: Mon Mar 08, 2010 8:54 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
some bash basics:
Code:
#!/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
=))


Top
 Profile  
 PostPosted: Mon Mar 08, 2010 9:54 pm   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
Watael wrote:
some bash basics:
Code:
#!/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.


Top
 Profile  
 PostPosted: Tue Mar 09, 2010 12:14 am   

Joined: Sun May 31, 2009 11:29 pm
Posts: 10
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!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP