Parsing string with grep
If the line prefix is always the same, simply use sed and replace the prefix with an empty string:
sed 's/\[INFO\] Total Time: //'
Assuming that the time is always the last thing in a line after a colon, use the following regex (replace each line with everything after the colon):
sed 's/^.*: \(.*\)$/\1/'
Shell scripting using grep to split a string
Using substitution with sed
:
echo $myVAR | sed -E 's/(.*)#{3}(.*)/\1/'
>>> firstword
echo $myVAR | sed -E 's/(.*)#{3}(.*)/\2/'
>>> secondword
# saving to variables
myFIRST=$(echo $myVAR | sed -E 's/(.*)#{3}(.*)/\1/')
mySECOND=$(echo $myVAR | sed -E 's/(.*)#{3}(.*)/\2/')
parsing strings using either grep,awk or sed
sed version:
sed -e 's/\([^ ]*\).*Job\(ID\)\? \([0-9]\+\).*/\3 \1/g'
or with extended regex as pointed out by @spasic:
sed -E 's/^(\S+).*Job(ID)? ([0-9]+).*/\3 \1/'
Parsing unstructured text file with grep
Assuming you have more than one "Summarized attack:" in your input file this may be what you're looking for:
$ cat tst.awk
/^Summarized attack:/ {
prt()
atk = ($3 ~ /^4/ ? $3 : 0)
cnt = 0
}
atk { cnt++ }
END {
prt()
print "TOTAL", tot
}
function prt() {
if ( atk ) {
cnt -= 2
print atk, cnt
}
tot += cnt
}
.
$ awk -f tst.awk file
Grep / search for a string in a csv, then parse that line and do another grep
$ awk -v key=KEY1 -F, '$1==key{f=1} ($1!~/^ *$/)&&($1!=key){f=0} f{print $3}' file
TRACKINGNUMBER1-1
TRACKINGNUMBER1-2
TRACKINGNUMBER1-3
TRACKINGNUMBER1-4
TRACKINGNUMBER1-5
glennjackman helpfully points out that by using a "smarter" value for FS
the internal logic can be simpler.
awk -v key=KEY1 -F' *,' '$1==key{f=1} $1 && $1!=key{f=0} f{print $3}' file
-v key=KEY1
assign the value KEY1
to the awk variable key
-F' *,'
assign the value *,
(which is a regular expression) to the awk FS
variable (controls field splitting)
$1==key{f=1}
if the first key of the line is equal to the value of the key
variable (KEY1
) then assign the value 1
to the variable f
(find our first desired key line)
$1 && $1!=key{f=0}
if the first field has a truth-y value (in awk a non-zero, non-empty string) and the value of the first field is not equal to the value of the key
variable assign the value 0
to the variable f
(find the end of our keyless lines)
f{print $3}
if the variable f
has a truth-y value (remember non-zero, non-empty string) then print the third field of the line
Parsing only first regex match in a line with several matches
How about using a ^
start anchor and restricting character set used:
grep -o '^[A-Za-z]1[A-Za-z]*1'
See this Bash demo or Regex Pattern at regex101
If you expect more digits or other characters in between, go with this
grep -oP '^[A-Za-z]1.*?[A-Za-z]1'
The lazy matching requires perl compatible mode. For not at line start, go with this
grep -oP '^.*?\K[A-Za-z]1.*?[A-Za-z]1'
\K
resets beginning of the reported match and is a PCRE feature as well.
parsing results in bash
Without a sample file, it is hard to write a working example. But I see, both values are from the same text file and the same line. Therefore I would use awk to do it:
$ cat text
service1;some_other_text;2.3.4
service2;just_some_text;55.66
$awk -F ";" '{printf "%s:%s\n", $1, $3}' test
service1:2.3.4
For a JSON file, it would be easier if you can use jg (e.g. apt-get install jg):
$ cat test.json
[
{
"name": "service1",
"customfield_10090": "1.2.3"
},
{
"name": "service2",
"customfield_10090": "23.3.2"
}
]
$jq '.[] | .name + ":" + .customfield_10090' test.json | sed 's/"//g'
service1:1.2.3
service2:23.3.2
The sed is necessary to eliminate the quotes.
Related Topics
Generic Printing Using a Usb Port
Implementation of Function Execve (Unistd.H)
Convert a Base64 Ldif File to Plaintext (For Import)
Text Encoding Between Linux and Windows
Linux Kill Process Using Timeout in Milliseconds
How to Split My File into Multiple Files
Google Suggest Query Using Curl
Having an Issue Passing Variables to Subshell
Errors While Trying to Build Gdb for Arm
Bluez: Setting Local Address to Be Private and Non-Resolvable
Can't Increment a 0-Padded Number Past 8 in Busybox Sh
Invalid Choice: 'Kernel_Add_Dts' in Yocto Build
Building Subversion 1.5.4 on Debian: Could Not Find Library Containing Rsa_New
How to Clone a Git Repository Which Is Present on a Remote Linux Server into Windows
How to Increase a Date Within a Loop in Bash
Aosp Build Error: Unrecognized Module Type "Hidl_Package_Root"
How to Create a Folder with a Folder Name Containing Spaces in Linux