Join two lines in pattern matching, search, then assign fields to variables

  • Follow


I have a file on stdin that I want contains a bunch of lines that are
basically a dump from a flatfile database.  I want to be able to match
a field in one of the lines and assign some of the other fields in the
same row to environment variables.  The problem is that the database
is dumped with 2 lines for each row and it doesn't always insert a
newline after the same field.  Is there a way in awk that I could join
the two lines, search for a pattern on say field2, then assign field5
and field7 to environment varialbes?

Say for example the the input is:

field1a  field2a  field3a  field4a field5a \n
field6a  field7a  field8a  field9a field10a
field1b  field2b  field3b  field4b \n
field5b  field6b  field7b  field8b field9b field10b
....


I've been doing it with in this fairly cumbersom way but I must think
that there's a much better way to do this all with an awk script.
Sorry, I'm still a newb at this stuff.  

Thanks

LINE1=`lynx -dump $URL | sed '/^$/{ N ; /^\n$/D }' | cat -n | grep -i
$HOSTNAME`

LINENUM=`echo $LINE1 | awk '{print $1}'`
LINENUM=`expr $LINENUM + 1`
LINE2=`lynx -dump $URL | cat -n | sed 's/^ *//' | grep -i "^$LINENUM"`
LINE=`echo $LINE1 $LINE2 | sed -e '/.*/N' -e 's/\n/ /'`

echo
echo Rack: `echo $LINE | awk '{print $11}'`
echo Side: `echo $LINE | awk '{print $12}'`
echo Slot: `echo $LINE | awk '{print $13}'`
echo Asset Tag: `echo $LINE | awk '{print $2}'`
echo Machine name and IP: `echo $LINE | awk '{print $5 " " $6}'`




.................................................................
       Posted via TITANnews - Uncensored Newsgroups Access
             >>>> at http://www.TitanNews.com <<<<
-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

0
Reply Tyaan 11/14/2003 9:07:18 PM

Tyaan <asdf@asdf.com> wrote:
> I have a file on stdin that I want contains a bunch of lines that are
> basically a dump from a flatfile database.  I want to be able to match
> a field in one of the lines and assign some of the other fields in the
> same row to environment variables.  The problem is that the database
> is dumped with 2 lines for each row and it doesn't always insert a
> newline after the same field.  Is there a way in awk that I could join
> the two lines, search for a pattern on say field2, then assign field5
> and field7 to environment varialbes?
> 
> Say for example the the input is:
> 
> field1a  field2a  field3a  field4a field5a \n
> field6a  field7a  field8a  field9a field10a
> field1b  field2b  field3b  field4b \n
> field5b  field6b  field7b  field8b field9b field10b
> ...

1.  while read a; do
	line="$line $a" ;;
	case "$a" in
	    *[^n]) echo $line; line='' ;;
	esac
    done

2.  echo 'g/\\n$/ \
	  .,/[^n]$/j
	  wq' | ed file
You still have to get rid of '\n'.

-- 
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution for data management and processing. 
0
Reply William 11/15/2003 3:52:55 AM


1 Replies
451 Views

(page loaded in 0.064 seconds)

Similiar Articles:













7/20/2012 11:56:13 AM


Reply: