Searching content of a variable using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching content of a variable using awk
# 1  
Old 09-08-2016
Searching content of a variable using awk

so i'm running a variation of the following command a few times in my script:

Code:
echo "${TOTALRunning}" | awk -F"[. ]" '/'"${PROCSEARCH}"'/ {print $2}' | tr '\n' '|'

unfortunately i cant paste the content of the variable TOTALRunning into this thread for security reasons.

what i want to do is combine the three piped commands into one.

also, I want the "tr" part of the command to run only IF there are MORE than ONE lines are found matching whatever the pattern is that is stored in the variable PROCSEARCH.

any ideas?
# 2  
Old 09-08-2016
Hello SkySmart,

Could you please try following, though couldn't test it as sample Input_file was not there.
Code:
echo "${TOTALRunning}" | awk -F"[. ]" '/'"${PROCSEARCH}"'/ {Q=Q?Q ORS $2:$2} END{gsub("\n","|",Q);print Q}'  
 OR
echo "${TOTALRunning}" | awk -F"[. ]" '/'"${PROCSEARCH}"'/ {Q=Q?Q "|" $2:$2} END{print Q}'

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-08-2016 at 03:30 AM.. Reason: Added one more solution too now. Removed Input_file from codes thanks to SkySmart for same.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-08-2016
Code:
echo "${TOTALRunning}" | awk -F"[. ]" -vP="${PROCSEARCH}" /$0~P/ {printf("%s|",$2)}END{print "\n"}'

This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 09-08-2016
It would be good if you could supply a short made up example snippet of the values being used, just to make sure.
# 5  
Old 09-08-2016
Quote:
Originally Posted by RavinderSingh13
Hello SkySmart,

Could you please try following, though couldn't test it as sample Input_file was not there.
Code:
echo "${TOTALRunning}" | awk -F"[. ]" '/'"${PROCSEARCH}"'/ {Q=Q?Q ORS $2:$2} END{gsub("\n","|",Q);print Q}'  Input_file
OR
echo "${TOTALRunning}" | awk -F"[. ]" '/'"${PROCSEARCH}"'/ {Q=Q?Q "|" $2:$2} END{print Q}'  Input_file

Thanks,
R. Singh
i would like to combine everything into one awk code. is something like this possible:

Code:
awk  -v val="${TOTALRunning}" -F"[. ]" '/'"${PROCSEARCH}"'/ {Q=Q?Q ORS $2:$2} END{gsub("\n","|",Q);print Q}'

There is no inputfile. The content of the variable "TOTALRunning" is produced by a different script.
# 6  
Old 09-08-2016
Hello SkySmart,

I give second to Zaxxon like at least sample Input_file(with a raw/dummy data, which looks similar to actual one) could be pasted to post.

Thanks,
R. Singh
# 7  
Old 09-08-2016
Maybe you can't show us the actual value assigned to TOTALRunning, but surely you could show us some representative strings that would emulate its value and:
  1. tell us what operating system you're using,
  2. what shell you're using,
  3. and what output you hope would be produced from those sample assignments.
We need to know:
  1. whether the string that has been assigned starts with a - or contains any \ characters (that will significantly affect the output produced by echo in some shells on some operating systems),
  2. whether we are processing a single line of input containing \n escape sequences or multiple lines of input separated by actual <newline> characters,
  3. whether the value assigned to that variable contains complete lines or the last line assigned to that variable is missing the line terminating <newline> character, and
  4. whether you really want all <newline> characters in the output to be translated to pipe symbols if more than one line is selected for output or you want all but the last line of output to be separated by pipe symbols and the last line of output to be terminated by a <newline> character no matter how many lines are selected for output.
If you aren't willing to give us a clear idea of what your input looks like and what output you're trying to produce, I have no confidence that anything I might suggest has any chance of doing what you want done.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for variable string

Hi Guys, So im trying to search for the most recent occurance of a string in the most recently updated .log file in a certain directory. The string i am searching for is a value, now if this value is greater than 800 i would like an email sent out with some text (blah blah blah). This is what... (7 Replies)
Discussion started by: hello_world
7 Replies

2. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

3. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

4. UNIX for Dummies Questions & Answers

searching a variable in environment

all, i am trying to search a variable in unix account. variable TEST_LEVEL=500 , i am getting this value if i grep in env .if i try to see the file where it is defined , i do get 1 file . i commented thsi variable in that file and did relogin, but still i see TEST_LEVEL=500 in thye... (3 Replies)
Discussion started by: deepakiniimt
3 Replies

5. Shell Programming and Scripting

setting a variable by searching within a file

Hi, I am trying to set a variable to be used in later scripting, and am having some difficulty. I want to look in a file called scan.info and find the line that says "variable ok". Then I want to cut the number at the beginning of that line and assign that number as a variable so that later... (4 Replies)
Discussion started by: garth6@hotmail.
4 Replies

6. UNIX for Dummies Questions & Answers

Searching for a string variable

Hi ... I have a string variable STR = "This is a test message" I have a file abc.txt that I am searching for the occurence of the string STR ... I am using the command in a script cat abc.txt | grep $STR It identifies each space as a seperator and prints word by word. How to... (2 Replies)
Discussion started by: mattrix
2 Replies

7. Shell Programming and Scripting

searching content of files in the current and sub directories

Hi I was wondering why command 2 doesn't work like command 1 below. 1. find . -exec grep "test" '{}' \; -print 2. ls -R | grep "test" I am trying to search "test" from all the files in the current and sub directories. What's wrong with my command 2? Thanks in advance for your help (4 Replies)
Discussion started by: tiger99
4 Replies

8. Shell Programming and Scripting

Content of Content of a variable!

I got a sample BASH script like this : $ cat test MYVAR=$1 DUMMY1="This is tricky" DUMMY2=24 echo $ $ ./test DUMMY1 ./test: line 5: This is tricky: syntax error in expression (error token is "is tricky") **I was expecting the output as "This is tricky", ah! but no luck **But... (2 Replies)
Discussion started by: jaduks
2 Replies

9. UNIX for Dummies Questions & Answers

searching for content of files

Hi, This question may be quite newbish. I've stored a few files on my Unix system and am wondering how to search for their contents (i.e. I input the keyword and get a list of files with this keyword) I'd then like to put it on my website (php). I thought of find and grep, but am not... (19 Replies)
Discussion started by: Aretai
19 Replies

10. Shell Programming and Scripting

Searching and Removing File Content

Hi, I am trying to search a character in a file and remove it from that file.... My file looks something like this: test1.txt ckj12300_00|123|var1|10.2 ckj00200_12|444|var2|11.2 ckj00200_14|4556|var3|33.5 c00200_00_000|4558|var4|33.5 ckj00200_14|4553|var5|33.5... (7 Replies)
Discussion started by: rkumar28
7 Replies
Login or Register to Ask a Question