LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk record separator question (https://www.linuxquestions.org/questions/linux-newbie-8/awk-record-separator-question-573372/)

johnpaulodonnell 07-30-2007 08:50 AM

awk record separator question
 
Hi.

given a data file 'test' such as:

Code:

test1a test1b test1c
test2a test2b test2c
test3a test3b test3c

within awk I would like to treat each line as a separate field...

but
Code:

awk -v FS='\n' '{print $1}' test
does not give me what I expect. It outputs the entire file rather than the first line...
Code:

test1a test1b test1c
test2a test2b test2c
test3a test3b test3c

Code:

awk -v FS='\n' '{print $2}' test
gives me nothing...

I thought by setting the FS to newline, that test1a test1b test1c would become the first field,
test2a test2b test2c the second. and so on...

therefore I would expect
Code:

awk -v FS='\n' '{print $1}' test
to just give me:

Code:

test1a test1b test1c

Would appreciate if someone could point me in the right direction.

bigrigdriver 07-30-2007 09:24 AM

Your understanding of the difference between 'field' and 'record' seems to be part of the problem.

Each line is a record, made up of three fields, seperated by a space.

So, you need to print each line (record) of the file.

johnpaulodonnell 07-30-2007 09:35 AM

Thanks for the reply...

I'm doing this inside a bash while loop. Each record (entire line) is filtered through awk which will write the line to a temporary file...the values in this temporary file will then be passed to a separate program for calculations and so on...

I need awk to output each record as a single field for this purpose...so the problem remains

Code:

#!/bin/bash

i=1

while [ $i -le `wc -l preliminary.file | awk '{print $1}'` ] ; do


  awk  -v FS="\n" -v i="$i" '$1 == i {print $1}' preliminary.file > temp1.file

  read eq date otime elat elon depth mag sac_evt.file station residual error < temp1.file

                          etc.......                 

    i=`expr $i + 1`

done



All times are GMT -5. The time now is 02:21 PM.