Using awk to match strings and outputing their corresponding values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to match strings and outputing their corresponding values
# 1  
Old 05-01-2014
Using awk to match strings and outputing their corresponding values

Hi

I will appreciate it if you can help me out. I have a file that contains this data

Code:
System Load: 3244 card: 1903 CPU: 6% card: 1904 CPU: 6% card: 1905 CPU: 28% card: 1906 CPU: 28% card: 1907 CPU: 36% card: 1908 CPU: 37%

I need to manipulate and output this as

Code:
system_load:3244 card1903:6 card1904:6 card1905:28 card1906:28 card1907:36 card1908:37

I have tried to use this awk script i have below

Code:
data=`cat data_file`

system_load=`echo $data | awk '{print $6}'`                           

card1903_p=`echo $data | awk '{print $10}'`
card1903=`echo $card1903_p | nawk -F "%" '{print $1}'`

card1904_p=`echo $data | awk '{print $14}'`
card1904=`echo $card1904_p | nawk -F "%" '{print $1}'`
......

echo "system_load:$system_load card1903:$card1903 card1904:$card1904 ..."

The problem i have is, this just uses the position of the value but i need the script to be able to match the string as in card: 1903 and pick the CPU value 6.

So in case the positions of the string changes the values will not be mismatched.

I will greatly appreciate your help in doing this

Thanks

Moderator's Comments:
Mod Comment CODE tags also for data samples please..

Last edited by Scrutinizer; 05-01-2014 at 02:30 PM.. Reason: CODE tags
# 2  
Old 05-01-2014
You could try combining everything in one awk. Would something like this work?
Code:
awk '{s="system_load:"$3; for(i=5; i<=NF; i+=4) s=s OFS "card" $i ":" $(i+2)+0; print s}' file

# 3  
Old 05-01-2014
Code:
perl -lne 's/System Load/System_load/;
  @A = split(/ /, $_);
  $S = ($A[0] . $A[1]);
  for($i = 2; $i <= $#A; $i++)
    {if($A[$i] eq "card:") {$S .= (" card" . $A[++$i] . ":" . ($A[($i + 2)] + 0))}};
  print $S' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to match file1 and extract specific tag values

File2 is tab-delimeted and I am trying to use $2 in file1 (space delimeted) as a search term in file2. If it is found then the AF= in and the FDP= values from file2 are extracted and printed next to the file1 line. I commented the awk before I added the lines in bold the current output resulted. I... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 Replies

4. Shell Programming and Scripting

awk base lookup of best match strings

Hi, I'm new to scripting and unable to find out a way to perform the below task. Request help in finding out a way to accomplish this. File one consists of some numbers/string which i need to lookup against file 2 and fetch the best match results in output. If best match is not present in... (3 Replies)
Discussion started by: suraj016
3 Replies

5. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

6. Shell Programming and Scripting

Match values in awk

Having trouble modifying the below awk to match $1 of sample.txt to $1 of data.txt and if there is a match, then $2 of data.txt is copied to $7 of sample.txt. Thank you :). awk 'NR==FNR{A=$1; next} A {$1=$1 " " A}1' data.txt sample.txt > output.txt Desired Output: Data... (3 Replies)
Discussion started by: cmccabe
3 Replies

7. Shell Programming and Scripting

Print strings that match pattern with awk

I have a file with many lines which contain strings like .. etc. But with no rule regarding field separators or anything else. I want to print ONLY THE STRING from each line , not the entire line !!! For example from the lines : Flow on service executed with success in . Performances... (5 Replies)
Discussion started by: black_fender
5 Replies

8. Shell Programming and Scripting

awk strings search + print next column after match

Hi, I have a file filled with search strings which have a blank in between and look like this: S. g. Ehr. o. Jg. v. d. Chijs g. Ehr. Now i would like to search for the strings and it also shall return the next column after the match. awk -v FILE="search_strings.txt" 'BEGIN {... (10 Replies)
Discussion started by: sdf
10 Replies

9. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

10. UNIX for Dummies Questions & Answers

Help removing strings from one file that match any of the values in a second file.

Hello, I have a file that lists a few hundred values. Example: abca abcb abcc abcd I have a 2nd file with a few thousand lines. I need to remove every line from the 2nd file that contains any of the values listed in first file. Example of strings to delete: line1 *abca* end of... (1 Reply)
Discussion started by: upstate_boy
1 Replies
Login or Register to Ask a Question