Reputation: 767
I have a large file. I want to retrieve the word that is exactly after this string: "PatterStr()."
Two sample lines:
PatterStr().123232424 hhhhh 9999. test, test32312
66666666698977. PatterStr().8888
The output should be:
123232424
8888
when I use grep the whole line will be printed And when two patterns are find in a line, both should be printed, for instance:
PatterStr().123232424 hhhhh 9999. test, test32312. PatterStr().11111111
66666666698977. PatterStr().8888
the correct result:
123232424
11111111
8888
Upvotes: 0
Views: 482
Reputation: 1517
With the help of ORS we get a "\n" after each statement on each line.
awk -F'[. ]' 'NR == 1{print $2 ORS $NF}NR == 2{print $NF}' file
123232424
11111111
8888
Upvotes: 0
Reputation: 31
You can reduce the output of grep with the option -o or --only-matching. This will print only the matched parts of a matching line. To suppress the output of PatterStr() you can use a LookBehind as described here.
cat bigfile | grep -Po '(?<=PatterStr\(\)\.)[\w]+'
Upvotes: 3
Reputation: 133428
Could you please try following.
awk '
{
while(match($0,/PatterStr\(\)\.[0-9]+/)){
value=substr($0,RSTART,RLENGTH)
sub(/.*\./,"",value)
print value
$0=substr($0,RSTART+RLENGTH)
value=""
}
}' Input_file
Output will be as follows.
123232424
11111111
8888
Explanation of above code: Adding detailed level of explanation for above code.
awk ' ##Starting awk program from here.
{
while(match($0,/PatterStr\(\)\.[0-9]+/)){ ##Starting while loop which has match function to match regex of PatterStr(). till all digits here.
value=substr($0,RSTART,RLENGTH) ##Creating variable value which has sub-string value of current line, startin point RSTART tioll RLENGTH.
sub(/.*\./,"",value) ##Substituting everything till DOT with NULL in variable value here.
print value ##Printing variable value here.
$0=substr($0,RSTART+RLENGTH) ##Setting rest of sub-string value starting from RSTART+RLENGTH to last of line of current line here.
value="" ##Nullify variable value here.
}
}' Input_file ##Mentioning Input_file name here.
Upvotes: 4
Reputation: 44
There are many ways how you can achieve this, you can for example do it with sed:
sed 's/ /\n/g' text-file.txt | sed -n 's/^PatterStr()\.\(.*\)/\1/p'
The first sed will split the content to separate lines by replacing space by new line, the second will match the lines with PatterStr(). and print what comes directly after it.
Upvotes: 0
Reputation: 1963
This line does what you need
grep 'PatterStr()' large-file | sed "s/ /\n/g" | grep 'PatterStr()' | cut -f2 -d\.
Output:
123232424
11111111
8888
Upvotes: 0