Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  14] [ 0]  / answers: 1 / hits: 30290  / 3 Years ago, fri, october 15, 2021, 2:04:14

I have a code snippet that I am using to parse through a log file and print information I need.



for i in $(cat ~/jlog/"$2"); do
grep "$1" ~/jlog/"$2" |
awk '/([a-zA-Z0-9.]+/ {print $7}'
done;


The problem is when I enter input in, it displays the answer multiple times:



(1.3.51.0.1.1.10.10.30.48.2084865.2084839/1.2.840.113619.2.284.3.17454802.933.1401109176.280.1)
(1.3.51.0.1.1.10.10.30.48.2084865.2084839/1.2.840.113619.2.284.3.17454802.933.1401109176.283.1)
(1.3.51.0.1.1.10.10.30.48.2084865.2084839/1.2.840.113619.2.80.977011700.14346.1401109696.2)
(1.3.51.0.1.1.10.10.30.48.2084865.2084839/1.2.840.113619.2.80.977011700.14346.1401109706.51)
(1.3.51.0.1.1.10.10.30.48.2084865.2084839/1.2.840.113619.2.80.977011700.14346.1401109758.100)
(1.3.51.0.1.1.10.10.30.48.2084865.2084839/1.2.840.113619.2.80.977011700.14346.1401109773.149)
(1.3.51.0.1.1.10.10.30.48.2084865.2084839/1.2.840.113619.2.80.977011700.14346.1401109810.198)
(1.3.51.0.1.1.10.10.30.48.2084865.2084839/1.2.840.113619.2.80.977011700.14346.1401109818.247)


Is there any way I can trim this so I can only have the first series of data display once. I only need 1.3.51.0.1.1.10.10.30.48.2084865.2084839 to print out once.



I tried to change it to this as well, but Bash does not like it:



for i in $(cat ~/jlog/"$2"); do
grep "$1" ~/jlog/"$2" |
awk '/([a-zA-Z0-9.]+/' |
awk -F'[(/]' ' {print $2, exit}'
done;


Then tried this:



for i in $(cat ~/jlog/"$2"); do
grep "$1" ~/jlog/"$2" |
awk -F'[(/]' '/([a-zA-Z0-9.]+/ {print $2, exit }'
done;

More From » bash

 Answers
6

Try this,



for i in $(cat ~/jlog/"$2"); do
grep "$1" ~/jlog/"$2" |
awk '/([a-zA-Z0-9.]+/ {print $7; exit}'
done;


exit in the awk command exits after printing the first match.



OR



Just pipe the output of for command to the below awk command,



for .... | awk -F'[(/]' '{print $2;exit}'

[#24584] Saturday, October 16, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amacal

Total Points: 457
Total Questions: 102
Total Answers: 116

Location: Thailand
Member since Thu, Apr 22, 2021
3 Years ago
;