LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2013, 05:30 PM   #1
soupmagnet
LQ Newbie
 
Registered: Sep 2012
Posts: 27

Rep: Reputation: Disabled
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.
 
Old 11-09-2013, 12:34 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,629

Rep: Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] pass shell parameters to awk does not work kristo5747 Linux - Newbie 2 06-20-2011 12:40 PM
Simple bash scripting question, involving finding newer files salasi Linux - Software 2 08-01-2009 04:22 PM
Problem with AWK in a BASH script geek_man Programming 4 01-20-2008 08:16 PM
Bash Awk Variable problem _hadi_ Programming 5 12-13-2006 01:25 AM
sed/awk most frequently used parameters ilnli Linux - General 1 07-02-2005 05:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration