How can I use grep to get a substring in Linux? [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a string such as follows:
{"projects":{"1633045839405":
{"name":"goal2","created":"2012-11-10T19:10:06Z","modified":"2012-11-10T19:10:06Z","customMetadata":{}},"1976708868731":{"name":"goal1","created":"2012-11-11T11:35:12Z","modified":"2012-11-11T11:35:12Z","customMetadata":{}}}}
I want to use grep to get the substring 1633045839405.
How can i do this with grep?
Thanks!

You probably want to use AWK or Perl instead of grep for this sort of operation.
For example, in AWK you could do something like this:
awk -F\" '/ *regular expression here* / {print $4}'

If the data is structured (like in your case, it is json), you'll only make your life harder with regex. Use a scripting language that has a parser for it, for example python or ruby.

Related

Using Awk, Cut and sed in the same pipe? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
We are doing a Linux workshop for college, and I was looking for a way to demonstrate using awk, sed and cut in the same pipe. I ve been thinking of using them in a apache server context (apache logs file), but is there other contexts I can use awk and sed and cut in?
here is one use
assume we want to convert all some vowels to uppercase sort some words based on the length
given file
$ cat file
apple
pear
banana
$ sed 'y/aeiu/AEIU/' file | awk '{print length "\t" $0}' | sort -n | cut -f2
pEAr
ApplE
bAnAnA
sed can be replaced with tr as well.

Two string compare after grep [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I got the following line
2020-10-17 14:55:39,586 INFO [http-bio-exec-60] [] [D88E13F571A51598613FAA078A215326.server.host.com.:9991] [some.package.Class] TEST_STRING - RSI: 506B48ECADC4BE0CEBF7C7D33D036B67.server.host.com.:9991
I do grep "D88E13F571A51598613FAA078A215326" and got the line above. Is there a way to run a command after grep to check if D88E13F571A51598613FAA078A215326 and 506B48ECADC4BE0CEBF7C7D33D036B67 are equal?
Thanks.
This will work if you already know the first pattern:
PATTERN=D88E13F571A51598613FAA078A215326
grep "\[$PATTERN.*RSI: $PATTERN" input_file
I got the following line, that's not a good way to start here:
Your line contains quite some information, and as mentioned in the comments, you might parse it using Perl or awk or any other tool, but I would advise otherwise: how do you get this line (I guess it's an output of some process)? You might ask the author of that other process to alter the output in such a way that it's easier for you do parse it and do the comparison you're aiming for.
Pipe grep output into this Perl one-liner, which splits the line on non-word characters and prints it if fields 13 and 23 are identical as strings:
echo '2020-10-17 14:55:39,586 INFO [http-bio-exec-60] [] [D88E13F571A51598613FAA078A215326.server.host.com.:9991] [some.package.Class] TEST_STRING - RSI: 506B48ECADC4BE0CEBF7C7D33D036B67.server.host.com.:9991' | \
grep 'D88E13F571A51598613FAA078A215326' | \
perl -F'/\W+/' -lane 'print if $F[12] eq $F[22];'

How to cut string="Domain_12345_20180821230101.dat" into 12345_20180821 in Bash [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 4 years ago.
Improve this question
Provided that this sting is dynamic. If it is as Domain_1234_20180821230101.dat then I want 1234_20180821. How can I do that?
I.e., when Domain_12_20180821230101.dat then I want output as 12_20180821
A primitive solution that works for the examples you gave would be:
grep -oP '[0-9]+_[0-9]{8}' <<< "$string"
This will extract any substring consisting of a variable-length number, followed by an underscore, followed by an 8-digit number if present, else return nothing.
You can find more infos to help you understand this command and adjust it to your requirements if necessary under grep --help and https://www.regular-expressions.info/tutorial.html.

Replacing one number in a text and cloning it with a new value [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
I need to change some numbers in a rule, like 192.168.1.2 to 192.168.1.x, where x is a value between 3-254. So'll have multiple lines, one with the value 192.168.1.2, other with the value 192.168.1.3, and so on.
Well, I've no clue how to do that.
If someone know of a program or some way to do using a script in linux, please let me know.
Here's one way using GNU sed. Simply replace X with the value you wish to use.
sed 's/\(\([0-9]\{1,3\}\.\)\{3\}\)[0-9]\{1,3\}/\1X/g' file.txt
Test:
echo "192.168.1.4" | sed 's/\(\([0-9]\{1,3\}\.\)\{3\}\)[0-9]\{1,3\}/\1X/g'
Result:
192.168.1.X
Another option, using awk to avoid the hairy regex route (nothing wrong with regexes in general, but sometimes they can make your eyes bleed...):
awk -F. '{printf "%s.%s.%s.x\n",$1,$2,$3}'

Words from HEX alphabet [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm looking for words that can be created from hex alphabet [0-9a-f]+
like C0FFEE, DEFACED
We also can use lisoz, where 1=l,i; 5=s; 2=z; 0=o etc.
It can be used in memory addressing or IPv6 address, so it will be funny and much easier to remember.
There is nice list here: http://www.nsftools.com/tips/HexWords.htm
but I'm sure we can think out something that is not there or invent new words :)
$ grep "^[0-9a-flo]*$" /usr/share/dict/words |
tr lo 10 | awk '{print length, $0}' |
sort -n | cut -f2- -d' ' | tail -n5
c0ffee1eaf
defaceab1e
effaceab1e
f1eeceab1e
c01db100ded
Wikipedia has an article on the more popular ones:
http://en.wikipedia.org/wiki/Hexspeak
Here's a handful.
0ff1ce
decaf
ed1f1ce
There are well over a thousand valid 3-or-more-letter words, but many of them are barely recognisable, such as 50c1a112ab1e (socializable). Sticking to just 0=o, things are much cleaner. Here are some highlights:
accede
beaded
bedded
b0bbed
c0bbed
c0ffee
decade
dec0de
deface
d0bbed
d0decade
d00dad
efface
facade

Resources