sort or unique command bash [closed] - bash

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 4 years ago.
Improve this question
This is a file . I want to remove the repetition in the name of patch
[ppande#server-1 —]$egrep 'Patch[0-9].*.*:' content1
Patch1001 : snmp fixl.org
Patch1002 : dhcp tmp fix
Patch1003 : qemu-img-9.0.58
Patch001 : snmp fixl.org
Patch002 : dhcp installation
Patch003 : qemu
Patch004 : snmp fixl.org
I used 'sort -u' but here the order of the patch is changed . All I need is the output with out repetitions and order remains same , or in other words if there is a repetition the second/last occurrence must not be displayed .
[ppande#server-1 —]$egrep 'Patch[0-9].*.*:' content1 | sort -u -k3
Patch002 : dhcp installation
Patch1002 : dhcp tmp fix
Patch003 : qemu
Patch1003 : qemu-img-0.0.58
Patch1001 : snmp fixl.org
Patch001 : snmp fixl.org
Desired output:
Patch1001 : snmp fixl.org
Patch1002 : dhcp tmp fix
Patch1003 : qemu-img-9.0.58
Patch002 : dhcp installation
Patch003 : qemu

EDIT: Since oguzismail added same solution few secs before me so adding perl solution now if you are ok with it.
perl -aF': ' -lne 'print if ! $seen{$F[1]}++' Input_file
Could you please try following. You need not to use multiple commands along with awk here.
awk -F': ' '/Patch[0-9].*.*/ && !a[$2]++' Input_file

You can do that in a single awk command.
awk -F ':\\s*' '/^Patch[0-9]+\s*:/ && !a[$2]++' content1

Related

Obtain the hostname served by a Kubernetes Ingress [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 11 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
How to resolve the domain/hostname of a K8s Service, that a specific K8s Ingress is serving?
In the namespace foobar, I want to know where to connect for the service provided by the ingress.
kubectl --namespace foobar get ingress
returns the available ones, and
kubectl --namespace foobar describe ingress/bazbar
returns the details; I can match by name (e.g. barbaz) the one I'm targeting.
But how can I extrapolate the host (and, possibly, also the path) to then launch it in the browser with xdg-open?
The below should solve your query on getting domain per namespace.
The query below "get ingress" retrieves the domain details from all namespaces and using awk, it prints the 1st column which is the namespace and the 4th column which is the domain in the ingress, you can grep it further to filter down on particular namespace.
#To get all namespace and domain
kubectl get ingress --all-namespaces|awk '{print $1 " | " $4 }'
foobar | foobar.example.com
barfoo | barfoo.example.com
#To filter on namespace from all namespace
kubectl get ingress --all-namespaces|awk '{print $1 " | " $4 }'|grep -i foobar
foobar | foobar.example.com
#To get one namespace
kubectl get ingress -n <namespace-name>|awk '{print $1 " | " $4 }'
foobar | foobar.example.com

Get currency exchange rate using bash [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 last year.
Improve this question
I would like to display the current exchange rate from the currency euro to us dollar in the command line using bash shell script. I'm using the website market insider.
I saw on someone's blog to use wget command
wget -qO- https://markets.businessinsider.com/currencies/eur-usd
But how can I display only the rate? Desired output (example for current rate 1.1347) --> 1.1347 $
PS: I would prefer not to use API
Any help would be appreciated
Do this cleanly using their API:
#!/usr/bin/env bash
API='https://markets.businessinsider.com/ajax/'
ExchangeRate_GetConversionForCurrenciesNumbers() {
isoCodeForeign=$1
isoCodeLocal=$2
amount=$3
date=$4
cacheFile="/tmp/$date-$amount-$isoCodeForeign-$isoCodeLocal.json"
# Check if we have cached the result to avoid front-running the API
if ! [ -e "$cacheFile" ]; then
post_vars=(
isoCodeForeign="$isoCodeForeign"
isoCodeLocal="$isoCodeLocal"
amount="$amount"
date="$date"
)
method='ExchangeRate_GetConversionForCurrenciesNumbers'
IFS='&' url="$API$method?${post_vars[*]}"
curl -s -X POST "$url" > "$cacheFile"
fi
jq -r '.ConvertedAmountFourDigits' "$cacheFile"
}
getRateEURO_USToday() {
ExchangeRate_GetConversionForCurrenciesNumbers EUR USD 1 "$(date '+%Y-%m-%d')"
}
# Set LC_NUMERIC=C because the float format returned is using . as decimal
LC_NUMERIC=C printf 'The exchange rate for EUR to USD today is: %.4f\n' \
"$(getRateEURO_USToday)"

How to remove an invalidly-named file in Bash? [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 4 years ago.
Improve this question
I found a file named '.|rst412slp10lad10_noTopo.png', and I really want to remove this. what should I do?
I tried to use ls -al to get the info of the file then I got this:
ls: cannot access '.|rst412slp10lad10_noTopo.png': No such file or directory
total 0
drwxrwxrwx 1 jinshengye jinshengye 4096 Jun 5 17:10 .
drwxrwxrwx 1 jinshengye jinshengye 4096 Jun 5 16:58 ..
-????????? ? ? ? ? ? .|rst412slp10lad10_noTopo.png
What should I do?
Find the inode number(s) with
ls -li
And remove it with
find . -inum 1234 -delete
OK you can try deleting with quotes
rm ".|rst412slp10lad10_noTopo.png"
but that probably won't work, you may need to set rights and /or reboot (or more)... Have a look at this:
https://serverfault.com/questions/65616/question-marks-showing-in-ls-of-directory-io-errors-too?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

database login using shell scripting [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 5 years ago.
Improve this question
This is the Database entry of my application in server.properties file.
umpdb.driverClassName=org.mariadb.jdbc.Driver
umpdb.url=jdbc:mysql://10.66.11.44:3306/MT_SMS_CHN?useUnicode=true&characterEncoding=UTF-8
umpdb.username=stackuser
umpdb.password=stackpass
I want to print mysql -uuser -ppasswrod -hhostname dbname using linux command.
It means, I need output as below
mysql -ustackuser -pstackpass -h10.66.11.44 MT_SMS_CHN
Please help me for this.
Using awk
awk -F'[:=/?]' '/url/{
h=$6" "$8 # get host and database
}
/username/{
u=$2 # get username
}
/password/{
# print username, password, host and database
printf("mysql -u%s -p%s -h%s\n",u,$2,h);
# we got what we want, exit
# if your file contains more than 1 db config
# just comment below exit keyword
exit
}
' server.conf
Test Results:
$ cat server.conf
umpdb.driverClassName=org.mariadb.jdbc.Driver
umpdb.url=jdbc:mysql://10.66.11.44:3306/MT_SMS_CHN?useUnicode=true&characterEncoding=UTF-8
umpdb.username=stackuser
umpdb.password=stackpass
$ awk -F'[:=/?]' '/url/{h=$6" "$8}/username/{u=$2}/password/{printf("mysql -u%s -p%s -h%s\n",u,$2,h); exit}' server.conf
mysql -ustackuser -pstackpass -h10.66.11.44 MT_SMS_CHN

Name of the users which executed a command [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 9 years ago.
Improve this question
I want to get the name of the users which executed a command (for example cat).
fc -l will provide a list with the most recent commands executed by the current user but is there an way to find out the history for all users?
I read the manual but i could not find something that would help
Do you know any other commands which would do this job?
I also tried w and who
I found this solution: the super user will search in each dir from "home" in the .bash_history and make a grep on that file for that command. It will work but is this optimal?
Using awk =)
awk -v monitoredcmd=cat '
$1~"^#[0-9]{10,}\s*$"{
sub(/#/,"")
tmpdate=$1
}
$1==monitoredcmd{
"date -d #"tmpdate | getline date
close("date -d #"tmpdate)
print "command [" $0 "] by",
gensub(/\/home\/([^\/]+).*/, "\\1", "", FILENAME),
at,
date
}
' /home/*/.bash_history
Sample Output
command [cat file.txt] by sputnick mer. févr. 13 15:34:44 CET 2013
command [cat l.py] by sputnick mer. févr. 13 15:45:38 CET 2013
command [cat foobar.pl] by marc mer. févr. 13 15:47:54 CET 2013

Resources