find all substring between two char (...) delimiters using shell script [closed] - string

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I want find all substring between '(' and ')' delimiters using shell script
ex: a string (123).....(abc)...(0000).....
123
abc
0000
I want to extract all substring from the string and use grep like this:
grep -o -P "(?<=()(.*?)(?=))"
and how to assign the output values that match the conditions to the variables
x=123
y=abc
...
Thanks

As your question is confusing regarding the language to use, using bash:
echo "a string (123).....(abc)...(0000)..... <br/>" | grep -P "(?<=\()(.*?)(?=\))" -o
The same logic can be used in Python
import re
data = "a string (123).....(abc)...(0000)..... <br/>"
print(re.findall("(?<=\\()(.*?)(?=\\))", data))
Output
123
abc
0000
Explanation
(?<=\() : positive lookbehind for (
(.*?) : match everything in a non-greedy way and capture it.
(?=\)) : positive lookahead for )

strr='a string (123).....(abc)...(0000)..... <br/>'
strr=strr.split('(')
plist=[]
for s in range(1,len(strr)):
plist.append(strr[s].split(')')[0])
print(plist)

Related

Awk regix print params from URL request in access log [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I have an access log file containing the following data, I want to check how many times the &u={yyy} parameter appears and print the corresponding number.
192.168.1.1 [2022/07/10 20:00:00] GET /action?t=test&u=123&b=check
192.168.1.2 [2022/07/10 20:00:00] GET /action?t=test&u=122&b=check
192.168.1.1 [2022/07/10 20:00:00] GET /action?t=test&u=122&b=check
Resuls:
2 122
1 123
I would harness GNU AWK for this task following way, let file.txt content be
192.168.1.1 [2022/07/10 20:00:00] GET /action?t=test&u=123&b=check
192.168.1.2 [2022/07/10 20:00:00] GET /action?t=test&u=122&b=check
192.168.1.1 [2022/07/10 20:00:00] GET /action?t=test&u=122&b=check
then
awk 'match($0,"&u=[^&]*"){arr[substr($0, RSTART+3, RLENGTH-3)]++}END{for(i in arr){print arr[i],i}}' file.txt
gives output
2 122
1 123
Explanation: I use 2 string functions, first is match which does set RSTART, RLENGTH and its' return value is used as condition, so action is executed only if match was found. Action is simple increase of value of array under key based on match without 3 first characters (&u=). After all lines are processed I output value key pairs of arrays. Disclaimer: this solution assumes any order of output lines is acceptable.
(tested in gawk 4.2.1)
If the logfile always look the same:
cat logfile | awk -F\& '{print $2}'| uniq -c

Shell scipt to convert bit string to text value code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have 32 bit string of 0s. Each bit in the string represents some code, for e.g.
1000000000000000000000000000000 = ABC
0100000000000000000000000000000 = DEF
...
0000000000000000000000000000001 = XYZ
I have file of string bit, is there any way to shell script the output to code
for. e.g
1100000000000000000000000000000 should print ABC|DEF
Thanks for your inputs.
edit1:
#nullPointer : the 3 digit letters are example of human readable representation of the 32 bit representation.
In bash:
codes=("ABC" "DEF" ... "XYZ")
bitstring=1100000000000000000000000000000
outstring=""
for i in {0..31}; do
if [ "${bitstring:i:1}" = 1 ]; then
outstring+="|${codes[i]}"
fi
done
echo "${outstring#|}"

Match a pattern and then go to the next condition and print the details [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This is my demo file: Demo.txt
CP Used
----------------------------------- --------------
gyhjjjjjjjjjjjjj
gdhdhsdjjsdjsd
----------------------------------- --------------
list: 21305
DP Used
----------------------------------- --------------
asghjskkkkkkkkkkfe jfdkjcdf
ashdjdjksd
----------------------------------- --------------
list: 203899
Here I want to match DP and then match list and print the details.
Expected output is:
21305,"CP"
203899,"DP"
Parse a simple table with awk:
awk '$2=="Used"{x=$1}; $1=="list:"{print $2",\""x"\""}' Demo.txt
If column 2 contains Used then save content of column 1 to variable x.
If column 1 contains list: then print column 2 and content of variable x.
Output:
21305,"CP"
203899,"DP"
Same approach like Cyrus sir's with a little difference of using variables and using $NF for string list value as follows.
awk -v s1="\"" -v s2="," '/Used/{val=$1;next} /list:/{print $NF s2 s1 val s1;val=""}' Input_file

search for pattern and remove all lines [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have system logs where alarms are written. in my case i have lots of repeated alarms which i want to ignore and focus only on new alarms that might be exist.
sample alarm :
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- <0N CpocsSs7CircuitCat#040200000000009a|6501646464644309|6501646464c90000-6503010117c80000-1.0.3|a40200003e3d8fd5|0000000e|0000000000000000
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- |RC USSDString=*1234*#|MSISDN=93707678224|
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- |NF NOT: src=ERROR_APPLICATION sev=SEVERITY_MAJOR id=010117c800000001
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- de.siemens.advantage.in.featureframework.FeatureException: GenAcc> [0]theGenericAccess: No value available for SubsDMDB.Subscriber.LanguageID and type INTEGER
kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- at de.siemens.advantage.in.features.genericAccess.impl.DynamicAsciiBuffer$Handle.throwNotAvailableException(DynamicAsciiBuffer.java:1105)
--
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- <0N CpocsSs7CircuitCat#020200000000008a|6501646464644309|6501646464c90000-6501646464640000-1.1.1|a20200003cc
31dd2|0000000e|0000000000000000
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- |RC CdPA=173|CgPA=93705040139|
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- |NF NOT: src=ERROR_APPLICATION sev=SEVERITY_MAJOR id=6503010103c80016
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- Exception in flexible core (e.g. during logic execution):de.siemens.advantage.in.featureframework.FeatureExc
eption: Call.checkIfCcOperationIsAllowed(): operation Call.playAnnouncement() only allowed within an open call control dialog
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- at de.siemens.advantage.in.features.flexDTMF.actions.dtmfActions.impl.DTMFActionsController.playAnnounc
ementList(DTMFActionsController.java:360)
--
the above lines are related to one alarms. here i want to omit such alarm in my log file.
I have tried using grep -v 'RC USSDString' IN-201609201800.txt | more but this command removes only the line where the searched pattern grep -v 'RC USSDString' IN-201609201800.txt | more does exist, where i want to remove the entire lines of alarm where pattern is found.
Edit:
- I have added one more alarm separated by double dash
Assuming your alarms are multi line and two alarms are separated with each other by --.
awk -v RS="--" '{$1=$1} !/RC USSDString/' alarmfile
If you want to add , multiple string to be excluded from output then:
awk -v RS="--" '{$1=$1} !/string-1/ || !/string-2/' alarmfile
What you have to do :
grep -Ev 'pattern1|pattern2|pattern3' file

Linux/ unix duplicate names [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I need to do is, to check for duplicate domain names and find if there is some.
So far I tried many commands with grep, awk ,sort, uniq but couldn't work this out, I am feeling its very simple, but can't reach it.
P.s. If i use uniq -c I get a huge list of string in this file, and I see how many duplicates it has and which by number string it is.
adding 20 rows from the file I am using
1,google.com
2,facebook.com
3,youtube.com
4,yahoo.com
5,baidu.com
6,amazon.com
7,wikipedia.org
8,twitter.com
9,taobao.com
10,qq.com
11,google.co.in
12,live.com
13,sina.com.cn
14,weibo.com
15,linkedin.com
16,yahoo.co.jp
17,tmall.com
18,blogspot.com
19,ebay.com
20,hao123.com
The output I would like to see
> 2 google
> 2 yahoo
Thanks for help !
You could use something like this to get the output you want:
$ awk -F'[.,]' '{++a[$2]}END{for(i in a)if(a[i]>1)print a[i],i}' file
2 google
2 yahoo
With the input field separator to either . or ,, the first {block} is run for every row in the file. It builds up an array a using the second field: "google", "facebook", etc. $2 is the value of the second field, so ++a[$2] increments the value of the array a["google"], a["facebook"], etc. This means that the value in the array increases by one every time the same name is seen.
Once the whole file is processed, the for (i in a) loop runs through all of the keys in the array ("google", "facebook", etc.) and prints those whose value is greater than 1.
Given this file:
$ cat /tmp/test.txt
1,google.com
2,facebook.com
3,youtube.com
4,yahoo.com
5,baidu.com
6,amazon.com
7,wikipedia.org
8,twitter.com
9,taobao.com
10,qq.com
11,google.co.in
12,live.com
13,sina.com.cn
14,weibo.com
15,linkedin.com
16,yahoo.co.jp
17,tmall.com
18,blogspot.com
19,ebay.com
20,hao123.com
In a Perl 1 liner:
$ perl -lane '$count{$1}++ if /^\d+,(\w+)/; END {while (($k, $v) = each %count) { print "$v $k" if $v>1}}' /tmp/test.txt
2 yahoo
2 google

Resources