LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   AWK problem involving parameters in Bash (https://www.linuxquestions.org/questions/programming-9/awk-problem-involving-parameters-in-bash-4175483939/)

soupmagnet 11-08-2013 04:30 PM

AWK problem involving parameters in Bash
 
I'm pretty new to awk so I'm having a hard time understanding why this doesn't work like I think it should...

I have a properties file that will be used to store information that will be retreived with awk:

Code:

-field1
value1

-field2
value2

-field3
value3

-field4
value4
...

In a script, I'm trying to set variables based on information in the properties file...

Code:

!# /bin/bash

getprop() {
awk '{print $1}' RS="$1" file
}

ONE=$(getprop -field1)
TWO=$(getprop -field2)
THREE=$(getprop -field3)
FOUR=$(getprop -field4)

echo $ONE
echo $TWO
echo $THREE
echo $FOUR

The output I'm expecting to see is this...
value1
value2
value3
value4
...

Instead, the output I'm getting is this...
value1
-field1 value2
-field1 value3
-field1 value4
...

Clearly, I'm either missing or don't understand something, but I have no idea what it is or where to look. Thanks in advance for any input given.

grail 11-08-2013 11:34 PM

RS is the Record Seperator

So if you take your first entry where you pass '-field1', your file you then have 2 records. One in front of the RS which is null hence the blank line printed at the start
and then the first field up until FS which is value1

Hope that helps


All times are GMT -5. The time now is 07:29 PM.