parse string with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers parse string with awk
# 1  
Old 10-06-2008
parse string with awk

Hi Guys,

I spend half a day getting this to work with no luck, perhaps you guys can help..

I have a string from a file looking like this:

module::name=test::type=generic_data::exec=snmpget.......::desc=A Little Test::interval=300

what I would like to split it, so I get a value for each entry. I currently use a bash script, but I need it to be pretty fast as there is a lot of lines (as above) in my file.

so I need the "value" of name, being "test" of cause. and the value of type and so on..

I need to pass them into my variables

strName=
strType=
strExec=
strDesc=
intInterval=

Can anyone give me an example how this could be done?

Cheers

Esben
# 2  
Old 10-06-2008
Seeing that nobody has answered the OP ...

The following example shows how you can do what you want to do using awk.
Code:
#!/bin/bash

# sample line
line="module::name=test::type=generic_data::exec=snmpget::desc=A Little Test::junk=Today the DOW dropped nearly 500 points::interval=300"

eval `echo $line | awk '
BEGIN {
      strName=""
      strType=""
      strExec=""
      strDesc=""
      intInterval=0
      FS="::"
}

 { strName=$2; sub(/^.*=/,"",strName) }
 { strType=$3; sub(/^.*=/,"",strType) }
 { strExec=$4; sub(/^.*=/,"",strExec) }
 { strDesc=$5; sub(/^.*=/,"",strDesc) }
 { intInterval=$NF; sub(/^.*=/,"",intInterval) }

END {
      printf "strName=\"%s\"\n", strName
      printf "strType=\"%s\"\n", strType
      printf "strExec=\"%s\"\n", strExec
      printf "strDesc=\"%s\"\n", strDesc
      printf "intInterval=%d", intInterval
}'`

# now check that the required variables were set

echo '===================='
echo "strName=$strName"
echo "strType=$strType"
echo "strExec=$strExec"
echo "strDesc=$strDesc"
echo "intInterval=$intInterval"
echo '===================='

exit 0

# 3  
Old 10-07-2008
Quote:
Originally Posted by hyber
I have a string from a file looking like this:

module::name=test::type=generic_data::exec=snmpget.......::desc=A Little Test::interval=300

what I would like to split it, so I get a value for each entry. I currently use a bash script, but I need it to be pretty fast as there is a lot of lines (as above) in my file.

so I need the "value" of name, being "test" of cause. and the value of type and so on..

I need to pass them into my variables

strName=
strType=
strExec=
strDesc=
intInterval=

Can anyone give me an example how this could be done?

Code:
line="module::name=test::type=generic_data::exec=snmpget.......::desc=A Little Test::interval=300"
IFS=:
set -f
set -- ${line//::/:}
set +f
strName=${2#*=}
strType=${3#*=}
strExec=${4#*=}
strDesc=${5#*=}
intInterval=${6#*=}

# 4  
Old 10-07-2008
Perl would work, too, if you aren't stuck on bash:

Code:
#!/bin/perl

my $line="module::name=test::type=generic_data::exec=snmpget.......::desc=A Little Test::interval=300";

my %data;

foreach $item(split(/::/, $line)) {
  next if($item !~ /=/);
  ($key,$value)=split(/=/, $item);
  $data{$key}=$value;
}

You could then access the data in each line of the file via the %data hash and do whatever you need to do before going on to the next line.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

2. Shell Programming and Scripting

AWK Command parse a file based on string.

AWK Command parse a file based on string. I am trying to write a shell script to parse a file based on a string and move the content of the file to another file. Here is scenario. File content below Mime-Version: 1.0 Content-Type: multipart/mixed; ... (2 Replies)
Discussion started by: aakishore
2 Replies

3. Shell Programming and Scripting

Use awk or sed to parse delimited string

Hi I am trying to figure out the best way to search a long log file and print out certain information. For example if I had a line in a log file delimited by ampersand first_name=mike&last_name=smith&zip_code=55555&phone=555-5555&state=ma&city=boston and I only wanted to search for and... (3 Replies)
Discussion started by: mstefaniak
3 Replies

4. Shell Programming and Scripting

Search string and parse

Input file 0792 to 2450 iadmssql7: Copy: CNJ R1: Replication volumes: Replication set: RSet 1 Replication size: 200.00GB SAN Info: 200.00GB DGC VRAID CX4-960 LUN 17 (17) RPA Port WWN Ctrl ... (0 Replies)
Discussion started by: greycells
0 Replies

5. Shell Programming and Scripting

Parse string

Hi, I need to parse a string, check if there are periods and strip the string. For example i have the following domains and subdomains: mydomain.com, dev.mydomain.com I need to strip all periods so i have a string without periods or domain extensions: mydomain, devmydomain. I use this for... (12 Replies)
Discussion started by: ktm
12 Replies

6. Shell Programming and Scripting

How to parse a string into variables

I'm working in korn shell and have a variable which contains a string like: aa_yyyymmdd_bbb_ccc_ddd.abc. I want to treat the _ and . as delimiters and parse the string so I end up with 6 values in variables that I can manipulate. My original plan was to use var1=`echo $sting1 | cut -c1-c2` but... (9 Replies)
Discussion started by: aquimby
9 Replies

7. Shell Programming and Scripting

Parse String Using Sed

Hi, I am wondering if there's a simpler way to extract the second occurrence of a word enclosed in that matches my search criteria. Sample Input is as follows: Error installing feature - com.er.nms.cif.ist.NoMatchingUpgra Error installing feature -... (4 Replies)
Discussion started by: racbern
4 Replies

8. Shell Programming and Scripting

Parse String from a Variable

Hello, Is there a quick way to parse the values from a variable? The variable has the following sample input: TA= The values of the TA variable is not fixed/hardcoded Basically I need to get the IV_Test and PF_SAPP_FWK values. I created a script that first use sed to remove ,... (3 Replies)
Discussion started by: racbern
3 Replies

9. Shell Programming and Scripting

how to parse this string

I want to get filenames from the following input. How can I parse this in bash. input data ------------------------------------------------------------------- path=/aaa/bbb/filename1;/aaa/filename2;/aaa/bbb/ccc/ddd/filename3 -------------------------------------------------------------------... (13 Replies)
Discussion started by: hcliff
13 Replies

10. Shell Programming and Scripting

String parse question

I have a string of data that looks like this: private.enterprises.954.1.1.1.1.1.2618 \(OctetString\): U private.enterprises.954.1.1.1.1.2.2618 \(OctetString\): 2618 I am trying to parse the string to only return the values after the ":". Ex from above "U" and "2618". Any suggestions? (5 Replies)
Discussion started by: mnreferee
5 Replies
Login or Register to Ask a Question