awk find which column contains string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk find which column contains string
# 1  
Old 09-04-2016
awk find which column contains string

i want to search columns in a variable and any column contains a particular string, i want to print the value of that column

Code:
[skys ~]$ echo "${USEDMEMORY}"
memTotalReal=3925908 memAvailReal=1109812 memBuffer=242676 memCached=641628
[skys ~]$ 
[skys ~]$ echo "${USEDMEMORY}" | awk '/memAvailReal/'

so what im trying to do here is, find the column that contains the string "memAvailReal", then, output the value of "memAvailReal", so the desired output should be:

Code:
1109812

i dont want to hardcode a specific column number to grab because these strings may be in different columns.
# 2  
Old 09-04-2016
Hello SkySmart,

Could you please try following and let me know if this helps.
Code:
echo "memTotalReal=3925908 memAvailReal=1109812 memBuffer=242676 memCached=641628" |  awk '{match($0,/memAvailReal=[0-9]+/);A=substr($0,RSTART,RLENGTH);sub(/.*=/,X,A);print A}'

Output will be as follows.
Code:
1109812

EDIT: Adding one more solution on same too.
Code:
echo "memTotalReal=3925908 memAvailReal=1109812 memBuffer=242676 memCached=641628" |  awk '{sub(/.*memAvailReal=/,X,$0);sub(/ .*/,X,$0);print}'

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-04-2016 at 01:10 PM.. Reason: Adding one more solution successfully too now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-04-2016
Quote:
Originally Posted by RavinderSingh13
Hello SkySmart,

Could you please try following and let me know if this helps.
Code:
echo "memTotalReal=3925908 memAvailReal=1109812 memBuffer=242676 memCached=641628" |  awk '{match($0,/memAvailReal=[0-9]+/);A=substr($0,RSTART,RLENGTH);sub(/.*=/,X,A);print A}'

Output will be as follows.
Code:
1109812

EDIT: Adding one more solution on same too.
Code:
echo "memTotalReal=3925908 memAvailReal=1109812 memBuffer=242676 memCached=641628" |  awk '{sub(/.*memAvailReal=/,X,$0);sub(/ .*/,X,$0);print}'

Thanks,
R. Singh
thank you so much! this works perfectly

anyway to get rid of the echo? and have one command do it all? something like this:

Code:
awk -v USEDMEMORY="${USEDMEMORY}"  '{sub(/.*memAvailReal=/,X,$0);sub(/ .*/,X,$0);print}'

# 4  
Old 09-04-2016
Quote:
Originally Posted by SkySmart
thank you so much! this works perfectly
anyway to get rid of the echo? and have one command do it all? something like this:
Code:
awk -v USEDMEMORY="${USEDMEMORY}"  '{sub(/.*memAvailReal=/,X,$0);sub(/ .*/,X,$0);print}'

Hello SkySmart,

Could you please try following and let me know if this helps you(haven't tested though,but it should work).
Code:
VAL="memTotalReal=3925908 memAvailReal=1109812 memBuffer=242676 memCached=641628" 
awk -vval="$VAL"  'BEGIN{sub(/.*memAvailReal=/,X,val);sub(/ .*/,X,val);print val}'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-04-2016
Quote:
Originally Posted by RavinderSingh13
Hello SkySmart,

Could you please try following and let me know if this helps you(haven't tested though,but it should work).
Code:
VAL="memTotalReal=3925908 memAvailReal=1109812 memBuffer=242676 memCached=641628" 
awk -vval="$VAL"  'BEGIN{sub(/.*memAvailReal=/,X,val);sub(/ .*/,X,val);print val}'

Thanks,
R. Singh
thank you! it works.
# 6  
Old 09-04-2016
Non-AWK solution.

Code:
# in the middle
$ echo "memTotalReal=3925908 memAvailReal=1109812 memBuffer=242676 memCached=641628" | sed '/^.*memAvailReal=/ s///;s/ .*$//'
1109812
# at the start
$ echo "memAvailReal=1109822 memTotalReal=3925908 memBuffer=242676 memCached=641628" | sed '/^.*memAvailReal=/ s///;s/ .*$//'
1109822
# at the end
$ echo "memTotalReal=3925908 memBuffer=242676 memCached=641628 memAvailReal=1109842 " | sed '/^.*memAvailReal=/ s///;s/ .*$//'
1109842

As you can see it works wherever the required parameter is in the line. I am making the assumption that you would pipe the output of the process generating the line directly into the above sed command.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 7  
Old 09-05-2016
With a recent shell:
Code:
TMP=${USEDMEMORY#*memAvailReal=}
echo ${TMP%% *}
1109812

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. Shell Programming and Scripting

awk to find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

3. Shell Programming and Scripting

Count occurrence of string in a column using awk

Hi, I want to count the occurrences of strings in a column and display as in example below: Input: get1 345 789 098 get2 567 982 090 fet4 777 610 632 get1 800 544 230 get1 600 788 451 get2 892 321 243 get1 673 111 235 fet3 789 220 278 fet4 768 222 341 output: 4 get1 345 789... (7 Replies)
Discussion started by: aydj
7 Replies

4. Shell Programming and Scripting

Column string matching in awk

Hello, I want pick up rows from input with conditions: 1) col2 is 2 replicate of col1 joint with "/" 2) col3 is a joint string as replicate of each side of the "/" symbol 3) col2 not equal to col3 input: TG TG/TG TG/TGG C C/C C/CG C C/G CA/CA C C/C CA/CA AG AG/AG... (3 Replies)
Discussion started by: yifangt
3 Replies

5. Shell Programming and Scripting

Find String and add to next column

Dear All, I have file input input01.txt 14193 40 153 14208 40 168 14283 45 243 14298 40 258 14313 41 273 14328 44 288 ... ...input02.txt 499815.5 9886300.0 14208 94.2 1957.1 499815.5 9886300.0 14208 314.2 2101.0 ... (2 Replies)
Discussion started by: attila
2 Replies

6. Emergency UNIX and Linux Support

awk cut column based on string

Using awk I required to cut out column contain word "-Tag" regardles of any order of contents and case INsensitive -Tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical Please Guide ...... --Shirish Shukla ---------- Post updated at 05:58 AM... (15 Replies)
Discussion started by: Shirishlnx
15 Replies

7. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

8. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies

9. UNIX for Dummies Questions & Answers

find the string in the 55th column [awk]

hi people, i need a help!!!! i'm trying use a awk command, but i dont have success!.. i need find the string 'FL' in 55th column in a length file of 2.4Gb!!! Attempts: awk -F"\t" ' { if ($55 ~ /^F/) print $0} ' FILE awk -F"\t" ' { if ($55 ~ /FL/) print $0} ' FILE awk -F"\t" ' { if... (7 Replies)
Discussion started by: gandarez
7 Replies

10. Solaris

find the string in the 55th column [awk]

hi people, i need a help!!!! i'm trying use a awk command, but i dont have success!.. i need find the string 'FL' in 55th column in a length file of 2.4Gb!!! Attempts: awk -F"\t" ' { if ($55 ~ /^F/) print $0} ' FILE awk -F"\t" ' { if ($55 ~ /FL/) print $0} ' FILE awk -F"\t" ' { if... (5 Replies)
Discussion started by: gandarez
5 Replies
Login or Register to Ask a Question