[wplug] Scripting - Text file manipulation help - probably perl

Mike Griffin mike at nbi.edu
Tue Mar 20 12:13:31 EST 2007


Quite a bit of typing, and I'm sure there's much more elegant ways to do
this, but this little script would get the job done.

- Use the file /tmp/test as an input data file to read
- Count how many lines are in the file
- Each line is parsed assigning each tab delimited field


--------- Start Script -------------------
#/bin/bash

FILENAME=/tmp/test
LNum=`sed -n '$=' ${FILENAME}`

for i in `seq 1 ${LNum}`
do
        echo "===Create-Ticket: ticket${i}"

        echo "Queue: `tail -${i} ${FILENAME} | head -1 | cut -f1`" 
        echo "Subject: `tail -${i} ${FILENAME} | head -1 | cut -f2`" 
        echo "Status: `tail -${i} ${FILENAME} | head -1 | cut -f3`" 

        echo ""  
done 

exit 0
----------- End Script -------------------------



You stated that there are 23 fields per line, and I took that as there will
always be 23 (never less, never more).
You can change the script around a bit if that would be a problem.

I.E. Let's make sure data exists in a field.

instead of 
  echo "Queue: `tail -${i} ${FILENAME} | head -1 | cut -f1`" 
do

--------- Snip ---------------
DATA="`tail -${i} ${FILENAME} | head -1 | cut -f1`"
        if [ -n ${DATA} ]
        then
          echo "Queue: ${DATA}"
        fi
-------- Snap ----------------



-Mike

-----Original Message-----
From: wplug-bounces+mike=nbi.edu at wplug.org
[mailto:wplug-bounces+mike=nbi.edu at wplug.org] On Behalf Of Gentgeen
Sent: Tuesday, March 20, 2007 11:28 AM
To: General user list
Subject: Re: [wplug] Scripting - Text file manipulation help - probably perl


Yes, each tab would become a new line, and each new line would have to start
with one of the tags noted below.

Between each entry, there is always "===Create-Ticket: ticket#"
then first column of .tsv can be ignored or deleted. (or even changed to
1,2,3,4, etc to replace in the # above if that would be easier)
running)

then:
  1) first TAB gets replaced with "<NEW LINE>Queue:<SPACE>" 
     and then the value from the .tsv
  2) second TAB gets replaced with "<NEW LINE>Subject:<SPACE>" 
     and then the value from the .tsv
  3) repeat until first line of the .tsv is done
  4) rinse, lather and repeat.


Simple example of original file (.tsv) :

  1<tab>dog<tab >cat<tab>mouse<tab> elephant, lion
  2<tab>linux<tab>windows<tab>macintosh<tab>sun, novell

Simple exaple of new file:

  ===Create-Ticket: ticket1
  Queue: dog
  Subject: cat
  Status: mouse
  TimeEstimated: elephant, lion
  
  ===Create-Ticket: ticket2
  Queue: linux
  Subject: windows
  Status: macintosh
  TimeEstimated: sun, novell

I hope that helps, and thank you Mike and smk at fyi.net for your work so far.


On Tue, 20 Mar 2007 10:53:46 -0400
"Mike Griffin" <mike at nbi.edu> wrote:

> To understand better, you want to replace the tab characters with new 
> lines, correct?
> 
> perl -pi -e 's/[\t]/\n/g' filename  (note* it's sed, not perl.)
> 
> Do you also want to add the tags to the end of each newly created line 
> as shown in your example?
> 
> 
> -Mike
>  
> 
> -----Original Message-----
> From: wplug-bounces+mike=nbi.edu at wplug.org
> [mailto:wplug-bounces+mike=nbi.edu at wplug.org] On Behalf Of Gentgeen
> Sent: Tuesday, March 20, 2007 10:11 AM
> To: WPLUG
> Subject: [wplug] Scripting - Text file manipulation help - probably 
> perl
> 
> 
> I need a bit of help changing a tab-delimited document (.tsv file) 
> into a special format so I can import it into our trouble ticket 
> system (RT).
> 
> The .tsv file has 23 columns, and 108 entries.  I am going to use a 
> spreadsheet style to reference the values (i.e. A1 for the 1st column, 
> 1st entry - B1 for the 2nd column, 1st entry)
> 
> I need to get it into a text file that would look like this:
> 
> ===Create-Ticket: ticket1
> Queue: <B2>
> Subject: <C2>
> Status: <D2>
> TimeEstimated: <E2>
> TimeWorked: <F2>
> TimeLeft: <G2>
> Priority: <H2>
> FinalPriority: <I2>
> Owner: <J2>
> Requestors: <K2>
> Due: <L2>
> Told: <M2>
> Created: <N2>
> Resolved: <O2>
> CustomField-10: <P2>
> CustomField-11: <Q2>
> CustomField-12: <R2>
> CustomField-13: <S2>
> CustomField-14: <T2>
> CustomField-5: <U2>
> CustomField-7: <V2>
> CustomField-8: <W2>
> CustomField-9: <X2>
> 
> ===Create-Ticket: ticket1
> Queue: <B3>
> Subject: <C3>
> Status: <D3>
> 
> on, and on, and on.....
> 
> 
> 
> Knowing that (most likly) the suggestion will come back as a PERL 
> solution... I would also like to request any suggesting for a good 
> PERL book.  PERL for dummys kind of thing (My coding/scripting skills 
> are limited to bash and HTML, not exactly very fancy stuff :-)  )
> 
> Thanks in advance for your time.
> 
> --
> http://gentgeen.homelinux.org
> 
> #############################################################
>  Associate yourself with men of good quality if you esteem    
>  your own reputation; for 'tis better to be alone then in bad 
>  company.        - George Washington, Rules of Civility
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
> 
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
> 




--
http://gentgeen.homelinux.org

#############################################################
 Associate yourself with men of good quality if you esteem    
 your own reputation; for 'tis better to be alone then in bad 
 company.        - George Washington, Rules of Civility
_______________________________________________
wplug mailing list
wplug at wplug.org
http://www.wplug.org/mailman/listinfo/wplug



More information about the wplug mailing list