awk (or other) script that assigns fields from a line to multiple variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk (or other) script that assigns fields from a line to multiple variables
# 1  
Old 04-14-2011
awk (or other) script that assigns fields from a line to multiple variables

Hey all,

Unfortunately I have only basic knowledge of awk and/or scripting. If I have a file with lines that can look similar to this:

Code:
Name=line1   Arg1=valueA   Arg2=valueB   Arg3=valueC
Name=line2   Arg1=valueD
Name=line3   Arg1=valueE   Arg3=valueF
Name=line4   Arg2=valueG   Arg3=valueH

I would like to use a script that will parse out the values from each of these fields and assign them to a variable that I can use in the rest of my script.

I thought about using 'read' but the order/number of values in the each line is inconsistent. By the way the file can be changed in pretty much any way to make this easier if need be.
# 2  
Old 04-14-2011
Those lines could be fed into the shell raw and work. You can just read lines, and wrap an eval around them, which tells it to parse and evaluate that line as if it'd read it from the script itself..

Code:
while read LINE
do
        [ -z "$LINE" ] && break;        # Ignore blank lines
        Name="" Arg1="" Arg2="" Arg3="" # Clear out old variables
        eval "$LINE"                    # Assign variables
        echo $Name $Arg1 $Arg2 $Arg3    # Tada
done < cfgfile

Code:
$ ./rdconfig.sh
line1 valueA valueB valueC
line2 valueD
line3 valueE valueF
line4 valueG valueH
$

# 3  
Old 04-15-2011
Just make sure you know what your input file contains. If your file contains a line with any command, its gonna get executed!
# 4  
Old 04-15-2011
Wow that's so cool, thanks guys!
# 5  
Old 04-15-2011
Quote:
Originally Posted by mirni
Just make sure you know what your input file contains. If your file contains a line with any command, its gonna get executed!
Good catchSmilie

How about:
Code:
sed 's/[^ ]*=//g' cfgfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read line and save fields as variables

Hej guys, I am trying to read a csv file line by line, save it's fields as variables per line so I can use them as parameters and execute stuff. I am new to shell scripting and was just strictly following a tutorial. Somehow my version seems to ignore the loop. Any help? TY! :) #!/bin/bash... (12 Replies)
Discussion started by: Splinter479
12 Replies

2. Shell Programming and Scripting

Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file: 1 3 3 2 3 3 4 0 4 0 5 4 5 2 2 0 5 3 4 0 6 0 3 2 I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's" I tried the following, but it's not working awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input ... (8 Replies)
Discussion started by: Rabu
8 Replies

3. Shell Programming and Scripting

awk multiple fields separators

Can you please help me with this .... Input File share "FTPTransfer" "/v31_fs01/root/FTP-Transfer" umask=022 maxusr=4294967295 netbios=NJ09FIL530 share "Test" "/v31_fs01/root/Test" umask=022 maxusr=4294967295 netbios=NJ09FIL530 share "ENR California" "/v31_fs01/root/ENR California"... (14 Replies)
Discussion started by: greycells
14 Replies

4. Shell Programming and Scripting

awk question ? set 2 variables by matching fields

Hello, I'm trying to get the TOP and BASE numbers printed out File looks like this: 2300 CAR # 2300 is the TOP 2310 CAR 2335 CAR 2455 CAR # 2455 is the BASE 1000 MOTOR # 2455 will become this TOP 2000 MOTOR 3000 MOTOR 4000 MOTOR # 4000 is the BASE 2345 BIKE # 4000... (8 Replies)
Discussion started by: charlieglen
8 Replies

5. Shell Programming and Scripting

awk gsub multiple fields

Hi, I am trying to execute this line awk -F ";" -v OFS=";" '{gsub(/\./,",",$6); print}' FILE but for multiple fields $6 $7 $8 Do you have a suggstion? Tried: awk -F ";" -v OFS="";"" "function GSUB( F ) {gsub(/\./,\",\",$F); print} { GSUB( 6 ); GSUB( 7 ); GSUB( 8 ) } 1"... (2 Replies)
Discussion started by: nakaedu
2 Replies

6. Shell Programming and Scripting

awk: Print fields between two delimiters on separate lines and send to variables

I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc... I know the sed to get rid of... (11 Replies)
Discussion started by: tay9000
11 Replies

7. UNIX for Dummies Questions & Answers

Formatting Multiple fields on 1 line to multiple rows

I'm trying extract a number of filename fields from a log file and copy them out as separate rows in a text file so i can load them into a table. I'm able to get the filenames but the all appear on one line. I tried using the cut command with the -d (delimiter) option but cant seem to make it... (1 Reply)
Discussion started by: Sinbad-66
1 Replies

8. Shell Programming and Scripting

AWK multiple line fields sorting

I have a bash script which takes a log file with each record separated by a #. The records have multiple fields but field $1 is always the date and time. When the script is run it prints the record just fine from oldest to newest. I need to have records print out from newest first. Here is the... (7 Replies)
Discussion started by: numele
7 Replies

9. Shell Programming and Scripting

Awk - Compare fields and increment variables

Hi, My first post to this group... I have a need to to parse a source file which is a capture from a network analyser. I have two fields that need to be checked: - Field 7 represents the packet length (an integer), and Field 4 represents a network address (e.g. 192.168.25.3) - The... (10 Replies)
Discussion started by: mv652
10 Replies

10. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies
Login or Register to Ask a Question