parsing a file by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing a file by line
# 1  
Old 10-14-2008
PHP parsing a file by line

I'm trying to make a script that will read variables line by line from a flatfile


i.e.

$ cat testfile
dbfoo sfoo prifoo poofoo bfoo osfoo
dbfoo2 sfoo2 prifoo2 poofoo2 bfoo2 osfoo2
$


The first pass of the script through the flatfile I want:

$1=dbfoo $2=sfoo $3=prifoo $4=poofoo $5=bfoo $6=osfoo


next pass I want:

$1=dbfoo2 $2=sfoo2 ... etc

I've tried several ideas:

doing a 'for i in `cat testfile`' runs a loop for each tab separated item

trying to use awk to get a line reads my variable literally...#PASSNFO=`cat $1 | awk 'NR=="$PASSNO"'` come out on the command line as #PASSNFO=`cat $1 | awk 'NR==$PASSNO'`


and I tried using shift to push to the next 6, but that seems to be used for interpreting command line arguments and not a file.

EDIT: trying the below from ksh seems to do nothing either, it won't even echo the output of $LINE
function while_read_LINE
{
cat $1 | while read LINE
do

Last edited by loadnabox; 10-14-2008 at 05:21 PM..
# 2  
Old 10-14-2008
Code:
awk '{ for (i=1; i <= NF ; i++) {printf("$%d=%s ", i, $i) }
         printf("\n")  } ' inputfilename

The default names for fields in awk are:
$0 - whole line
$1 - field 1
$2 - field 2
.........
NF is the number of fields on the line.
You can substitute a variable like "i" for numbers of fields: "$i" is the field
that when i is evaluated, that field number is referenced.
# 3  
Old 10-14-2008
Quote:
Originally Posted by jim mcnamara
Code:
awk '{ for (i=1; i <= NF ; i++) {printf("$%d=%s ", i, $i) }
         printf("\n")  } ' inputfilename

The default names for fields in awk are:
$0 - whole line
$1 - field 1
$2 - field 2
.........
NF is the number of fields on the line.
You can substitute a variable like "i" for numbers of fields: "$i" is the field
that when i is evaluated, that field number is referenced.


Thanks for the reply Jim, I apologize in advance for my newbishness

so the result I get from the above is:

# ./awktest.sh testfile
$1=dbfoo $2=sfoo $3=prifoo $4=poofoo $5=bfoo $6=osfoo
$1=dbfoo2 $2=sfoo2 $3=prifoo2 $4=poofoo2 $5=bfoo2 $6=osfoo2
#

but I'm not sure how I would reference (for example) $4 on line 2 without getting $4 on line 1
# 4  
Old 10-14-2008
I'm not sure I'm entirely clear on what you want to accomplish, so please feel free to reply so we can tweak the commands as needed. It might help to know one more standard awk variable, FNR. This is the line number. Here are a couple of examples of its use, with the data file being piped in to simplify the syntax a hair. In general, though, I'd suggest calling the data file as part of the command.

Code:
> cat testfile | awk 'FNR == "1"'
dbfoo sfoo prifoo poofoo bfoo osfoo

> cat testfile | awk 'FNR == "2"'
dbfoo2 sfoo2 prifoo2 poofoo2 bfoo2 osfoo2

> cat testfile | awk 'FNR == "2" || FNR == "4"'
dbfoo2 sfoo2 prifoo2 poofoo2 bfoo2 osfoo2
dbfoo4 sfoo4 prifoo4 poofoo4 bfoo4 osfoo4

The above shows how to print a specific line(s), identified by line number(s). Oh, BTW, I extended your original testfile to 5 lines.

Now, suppose that you want to print only field 4 of line 1. You can use:

Code:
> cat testfile | awk 'FNR == "2" {print $4}'
poofoo2

I hope that's a start. Reply as needed... I'm sure we can scrape together something that'll get the job done.
# 5  
Old 10-14-2008
You should not use $1 as a variable name in shell - it is already reserved
That said you can create an array:
Code:
#/bin/ksh

while read line
do
     set -A array $line
     # at this point you have array elements that you reference with a number
     # ${#array[*]}   number of fields or elements in the array.
     let i=0
     while [[ $i -lt  ${#array[*]} ]]
     do
           echo " field $i = ${array[i]} \c"
           i=$(( i + 1 ))
     done
     echo " "
done < inputfile

arrays count from zero, and go from zero to one less than the number of elements.
echo "...\c" supresses a new line, so all of the output is one one line.
# 6  
Old 10-14-2008
Quote:
Originally Posted by treesloth
I'm not sure I'm entirely clear on what you want to accomplish, so please feel free to reply so we can tweak the commands as needed. It might help to know one more standard awk variable, FNR. This is the line number. Here are a couple of examples of its use, with the data file being piped in to simplify the syntax a hair. In general, though, I'd suggest calling the data file as part of the command.

Code:
> cat testfile | awk 'FNR == "1"'
dbfoo sfoo prifoo poofoo bfoo osfoo

> cat testfile | awk 'FNR == "2"'
dbfoo2 sfoo2 prifoo2 poofoo2 bfoo2 osfoo2

> cat testfile | awk 'FNR == "2" || FNR == "4"'
dbfoo2 sfoo2 prifoo2 poofoo2 bfoo2 osfoo2
dbfoo4 sfoo4 prifoo4 poofoo4 bfoo4 osfoo4

The above shows how to print a specific line(s), identified by line number(s). Oh, BTW, I extended your original testfile to 5 lines.

Now, suppose that you want to print only field 4 of line 1. You can use:

Code:
> cat testfile | awk 'FNR == "2" {print $4}'
poofoo2

I hope that's a start. Reply as needed... I'm sure we can scrape together something that'll get the job done.


Thanks for the post Tree,

allrighty, the end goal is is to create policies in netbackup, I'm going to be creating a LOT of policies very soon to where a script will make life easier.

Below is what I have as it's been edited repeatedly (so there's lots of stuff I commented out rather than delete until I figure out what works, possibly not considered the best method of hammering things out) All the commands are just echo'd right now as I don't want it to do anything until I'm sure it's submitting correct information.

The stuff in the while statement is what needs to happen for each line. Since which line needs to be read from the file advances with each pass I can't hard code an awk '[NR|FNR]==X' and putting a $PASSNO in place of 'X' didn't seem to work.

$ cat RMAN_policy.sh
#! /bin/ksh
#
# set command line variables
#
PATH=/usr/openv/netbackup/bin:/usr/openv/netbackup/bin/admincmd:/usr/openv/netbackup/bin/goodies:/usr/openv/netbackup/bin/goodies/support:/usr/openv/volmgr/bin:/usr/openv/volmgr/bin/goodies:/opt/VRTSvxfs/sbin:$PATH
#USAGE="USAGE: RMAN_policy.sh <filename> "


#shift `expr $OPTIND - 1`

#INSTANCE=$1
#SERVER=$2
#PRIORITY=$3
#VPOOL=$4
#BPATH=$5
#OSV=$6

#PASSES=`wc -l $1 | awk '{ print $1 }'`
#PASSNO=0

function while_read_LINE
{
cat $1 | while read LINE
do

echo "$LINE"

INSTANCE=`echo "$LINE" | awk '{ print $1 }'`
SERVER=`echo "$LINE" | awk '{ print $2 }'`
PRIORITY=`echo "$LINE" | awk '{ print $3 }'`
VPOOL=`echo "$LINE" | awk '{ print $4 }'`
BPATH=`echo "$LINE" | awk '{ print $5 }'`
OSV=`echo "$LINE" | awk '{ print $6 }'`

echo "$INSTANCE"
echo "$SERVER"
echo "$PRIORITY"
echo "$VPOOL"
echo "$BPATH"
echo "$OSV"

echo bppolicynew ora_"$INSTANCE"
echo bpplinfo ora_"$INSTANCE" -set -ut -active -blkincr 0 -collect_tir_info 0 -compress 0 -crossmp 1 -disaster 0 -encrypt 0 -follownfs 0 -multiple_streams 1 -policyjobs 0 -pool "$VPOOL" -priority "$PRIORITY" -pt Standard -residence foo -chkpt 1 -chkpt_intrvl 30
echo bpplsched ora_"$INSTANCE" -add Full
echo bpplschedrep ora_"$INSTANCE" Full -cal 0 -freq 86400 -mpxmax 1 -rl 5 -st FULL
echo bpplinclude ora_"$INSTANCE" -add "$BPATH"/*
echo bpplclients ora_"$INSTANCE" -add "$SERVER" Solaris "$OSV"

#PASSNO=`expr $PASSNO + 1`

#echo END PASS "$PASSNO"

#INSTANCE=$INSTANCE; shift 6
#SERVER=$SERVER; shift 6
#PRIORITY=$PRIORITY; shift 6
#VPOOL=$VPOOL; shift 6
#BPATH=$BPATH; shift 6
#OSV=$OSV; shift 6
done
}
$
# 7  
Old 10-14-2008
Thanks for the help guys! I managed to get something working, though it was a little hectic and there's probably more graceful way of dealing with a little problem I had involving the script running once for each "word" in the flatfile instead of once for each line.

Here's the practical example should someone be interested in the future.

$ cat array.sh
#/bin/ksh

TESTFILE=$1

while read line
do
set -A array $line
# at this point you have array elements that you reference with a number
# ${#array[*]} number of fields or elements in the array.
let i=0
while [[ $i -lt ${#array[*]} ]]
do

INSTANCE=${array[0]}
i=$(( i + 1 ))
SERVER=${array[1]}
i=$(( i + 1 ))
PRIORITY=${array[2]}
i=$(( i + 1 ))
VPOOL=${array[3]}
i=$(( i + 1 ))
BPATH=${array[4]}
i=$(( i + 1 ))
OSV=${array[5]}
i=$(( i + 1 ))

#echo $INSTANCE
#echo $SERVER
#echo $PRIORITY
#echo $VPOOL
#echo $BPATH
#echo $OSV

echo bppolicynew ora_"$INSTANCE"
echo bpplinfo ora_"$INSTANCE" -set -ut -active -blkincr 0 -collect_tir_info 0 -compress 0 -crossmp 1 -disaster 0 -encrypt 0 -follownfs 0 -multiple_streams 1 -policyjobs 0 -pool "$VPOOL" -priority "$PRIORITY" -pt Standard -residence \"foo_bar\" -chkpt 1 -chkpt_intrvl 30
echo bpplsched ora_"$INSTANCE" -add Full
echo bpplschedrep ora_"$INSTANCE" Full -cal 0 -freq 86400 -mpxmax 1 -rl 5 -st FULL
echo bpplinclude ora_"$INSTANCE" -add "$BPATH"/*
echo bpplclients ora_"$INSTANCE" -add "$SERVER" Solaris "$OSV"

#echo ENDPASS


done
done < $TESTFILE
$
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command Line Perl for parsing fasta file

I would like to take a fasta file formated like >0001 agttcgaggtcagaatt >0002 agttcgag >0003 ggtaacctga and use command line perl to move the all sample gt 8 in length to a new file. the result would be >0001 agttcgaggtcagaatt >0003 ggtaacctga cat ${sample}.fasta | perl -lane... (2 Replies)
Discussion started by: jdilts
2 Replies

2. Shell Programming and Scripting

Parsing a file based on next line

I have a file1 like ID E2AK1_HUMAN Reviewed; 630 AA. CC -!- SUBCELLULAR LOCATION: Host nucleus {ECO:0000305}. ID E1A_ADEM1 Reviewed; 200 AA. ID E1A_ADES7 Reviewed; 266 AA. CC -!- SUBCELLULAR LOCATION: Host nucleus... (8 Replies)
Discussion started by: sammy777
8 Replies

3. Shell Programming and Scripting

Suggestions for command line parsing

Hi all I need to put a command line parser together to parse numeric fields and ranges passed to a script. I'm looking for a bash function that is as elegant and simple as possible. So the input would be of the following form - 1,2,8-12 This would return - 1,2,8,9,10,11,12 Input can... (7 Replies)
Discussion started by: steadyonabix
7 Replies

4. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

5. UNIX for Dummies Questions & Answers

multiple line parsing help

Hey everyone, I'm having trouble figuring out how to reformat the following (large) file: >Cluster 1 0 563nt, >FX2FH6V05GB01A... * 1 405nt, >FX2FH6V05F7LOL... at +/98% >Cluster 2 0 551nt, >FX2FH6V05FTLO0... at +/98% 1 561nt, >FX2FH6V05F5F1E... * 2 343nt, >FX2FH6V05GBHRK... at +/98% I... (2 Replies)
Discussion started by: mycoguy
2 Replies

6. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

7. Shell Programming and Scripting

Perl question, parsing line by line

Hello, Im very new to PERL and as a project to work on developing my skills at PERL Im trying to parse poker hands. Ive tried many methods however I cant get the last step. $yourfile= 'FILENAME';#poker hands to parse open (FILE, "$yourfile") or die $!; @lines = <FILE>; for (@lines) ... (1 Reply)
Discussion started by: Ek0
1 Replies

8. Shell Programming and Scripting

Parsing line out of a file, please help !!

Hello, I have a file with several lines for example; I need to extract a line radiusAuthServTotalAccessRequests.0 = 0 and I don't have line #s in the file. I need to write a script to extract the above line, put a date beside it and parse this line out to another directory / file. How... (5 Replies)
Discussion started by: xeniya
5 Replies

9. Shell Programming and Scripting

Problem parsing line in file

I am VERY new to unix scripting. I am having trouble parsing a line into fields for further processing. I have this script: #bin/sh cat ztest2.txt | while read line do zvar1=`echo $line | cut -f6` echo "zvar1 is " $zvar1 done ******************** ztest2.txt looks like: 1 ... (2 Replies)
Discussion started by: rlwilli
2 Replies

10. Shell Programming and Scripting

parsing a delimited line

I have a text file of lines like: A=5|B=7|G=4|C=3|P=4|... In other words, each line is a pipe-delimited set of pairs of strings of the form "X=Y". What I want to do is find the token starting with "C", and print it and its value (so I'd want to print "C=3" in the example above). I'm... (11 Replies)
Discussion started by: monkeys
11 Replies
Login or Register to Ask a Question