How to locate the gap as quickly as possible? [closed] - bash

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 6 years ago.
Improve this question
[root#test143 ~]# ping 8.8.8.8 | while read xx; do echo "$(date '+%Y-%m-%d %H:%M:%S'): $xx"; done
2016-06-30 15:51:41: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
2016-06-30 15:51:41: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=47 time=78.2 ms
2016-06-30 15:51:43: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=47 time=78.2 ms
2016-06-30 15:51:44: 64 bytes from 8.8.8.8: icmp_seq=4 ttl=47 time=78.3 ms
...
2016-06-30 15:57:58: 64 bytes from 8.8.8.8: icmp_seq=300 ttl=47 time=78.4 ms
I want to monitor the network interrupt(centos 6.5).
As mentioned above,we lost icmp_seq=2,How to locate the gap.

Search Stack again...
Say you have an input file named gaps
more gaps
icmp_seq=1
icmp_seq=2
icmp_seq=3
icmp_seq=5
icmp_seq=8
icmp_seq=10
You can make use of the below AWK
awk -F "=" '$2!=p+1{print p+1"-"$2-1}{p=$2}' gaps
4-4
6-7
9-9
explanations
-F "=" use = as the field separator
$2 is the second column from current input line
p is the previous value of the last line
so ($2!=p+1) is a condition : if $2 is different than previous value +1, then :
this part is executed : {print p+1 "-" $2-1} : print previous value +1, the - character and second columns + 1
{p=$2} is executed for each lines : p is assigned to the current 2nd column

Related

AWK or SED Replace space between alphabets in a particular column [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have an infile as below:
infile:
INM00042170 28.2500 74.9167 290.0 CHURU 2015 2019 2273
INM00042182 28.5833 77.2000 211.0 NEW DELHI/SAFDARJUNG 1930 2019 67874
INXUAE05462 28.6300 77.2000 216.0 NEW DELHI 1938 1942 2068
INXUAE05822 25.7700 87.5200 40.0 PURNEA 1933 1933 179
INXUAE05832 31.0800 77.1800 2130.0 SHIMLA 1926 1928 728
PKM00041640 31.5500 74.3333 214.0 LAHORE CITY 1960 2019 22915
I want to replace the space between two words by an underscore in column 5 (example: NEW DELHI becomes NEW_DELHI). I want output as below.
outfile:
INM00042170 28.2500 74.9167 290.0 CHURU 2015 2019 2273
INM00042182 28.5833 77.2000 211.0 NEW_DELHI/SAFDARJUNG 1930 2019 67874
INXUAE05462 28.6300 77.2000 216.0 NEW_DELHI 1938 1942 2068
INXUAE05822 25.7700 87.5200 40.0 PURNEA 1933 1933 179
INXUAE05832 31.0800 77.1800 2130.0 SHIMLA 1926 1928 728
PKM00041640 31.5500 74.3333 214.0 LAHORE_CITY 1960 2019 22915
Thank you
#!/bin/bash
# connect field 5 and 6 and remove those with numbers.
# this returns a list of new names (with underscore) for
# all cities that need to be replaced
declare -a NEW_NAMES=$(cat infile | awk '{print $5 "_" $6}' | grep -vE "_[0-9]")
# iterating all new names
for NEW_NAME in ${NEW_NAMES[#]}; do
OLD_NAME=$(echo $NEW_NAME | tr '_' ' ')
# replace in file
sed -i "s/${OLD_NAME}/${NEW_NAME}/g" infile
done

Bash- Sort a file based on a list in another file [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 6 years ago.
Improve this question
I know that similar questions have been asked about sorting a file by a specific column, but none of them seem to answer my question.
My Input file looks like
OHJ07_1_contig_10 0 500 130 500 500 1.0000000
OHJ07_1_contig_10 500 1000 180 500 500 1.0000000
OHJ07_1_contig_10 1000 1500 171 500 500 1.0000000
OHJ07_1_contig_10 1500 2000 79 380 500 0.7600000
OHJ07_1_contig_10 2000 2500 62 500 500 1.0000000
OHJ07_1_contig_10 2500 3000 96 500 500 1.0000000
OHJ07_1_contig_10 3000 3500 76 500 500 1.0000000
OHJ07_1_contig_10 3500 4000 87 500 500 1.0000000
OHJ07_1_contig_10 4000 4500 60 500 500 1.0000000
OHJ07_1_contig_10 4500 5000 64 500 500 1.0000000
OHJ07_1_contig_10 5000 5468 213 468 468 1.0000000
OHJ07_1_contig_100 0 500 459 500 500 1.0000000
OHJ07_1_contig_100 500 1000 156 500 500 1.0000000
OHJ07_1_contig_100 1000 1314 77 305 314 0.9713376
OHJ07_1_contig_1000 0 500 239 500 500 1.0000000
OHJ07_1_contig_1000 500 1000 226 500 500 1.0000000
OHJ07_1_contig_1000 1000 1500 238 500 500 1.0000000
OHJ07_1_contig_1000 1500 2000 263 500 500 1.0000000
The program that generated it, sorted alphanumerically based on the name in the first column, but I would like to sort it based on a list of names in another file, and keep all the other data. The other file has other information, like contig length in column 2 (this file was produced with samtools faidx).
OHJ07_1_contig_25270 888266 96530655 60 61
OHJ07_1_contig_36751 583964 120924448 60 61
OHJ07_1_contig_44057 504884 134192571 60 61
OHJ07_1_contig_21721 415942 87354744 60 61
OHJ07_1_contig_46339 411691 143341916 60 61
OHJ07_1_contig_44022 330441 133783765 60 61
Since each name has a different number of entries in the first file, what's the easiest way to deal with this? Preferably using bash
I haven't tried anything because I have literally no way to tackle this.
I would prepend each line of file that determines the order (from now on named index) with its line number, there is a way using awk , I used the answer written here https://superuser.com/questions/10201/how-can-i-prepend-a-line-number-and-tab-to-each-line-of-a-text-file to do this (assuming your index file is named index and data file is named data.txt):
awk '{printf "%d,%s\n", NR, $0}' < index > index-numbered
in this way you will have in index-numbered a correspondence between the arbitrary words order you decided and numbers.
you can then use a while on file to sort that replaces each first word with index line number, a comma and the rest of line (keeping name) , for example :
57,OHJ07_1_contig_46339 411691 143341916 60 61
in this way you will be able to sort using the first field, the number, which translates your arbitrary order in a numeric order.
The while which create a new data file with same numbers as above:
while read line
do
key=$(echo $line | cut -f1)
n=$(grep $key index-numbered | cut -d, -f1)
echo $n","$line >> indexed-data.txt
done < data.txt
Then you can simply sort your modified data file (indexed-data.txt) using sort and using the inserted line number as sort key :
sort -k1 -n -t, indexed-data.txt >sorted-data.txt
If you want to hide line numbers on final output you can filter each one out modifying preceding instructions with these :
sort -k1 -n -t, indexed-data.txt | cut -d, -f2 > sorted-data.txt
Your final output will be in file sorted-data.txt .
I'm sure this is not the best solution, maybe others can answer better than me.

shell script to check ping packet loss & avg latency on AIX

I'm trying to create script which will ping to remote server and displays only
1) % packet loss
2) average round trip latency in ms
for packet loss i created line
`ping -c 3 -s 14 x.x.x.x|grep packet|awk '{print $7}'|cut -d'%' -f1`
which only gives packet loss,
my problem starts when ip is not reachable output of line changes
and hence i'm not able to capture both
for reference i'm showing output of both scenarios
`/pefmephbir >ping -c 3 -s 14 10.9.50.225`
PING 10.9.50.225 (10.9.50.225): 14 data bytes
--- 10.9.50.225 ping statistics ---
**3 packets transmitted, 0 packets received, 100% packet loss**
`/pefmephbir >ping -c 3 -s 14 10.9.50.220`
PING 10.9.50.220 (10.9.50.220): 14 data bytes
22 bytes from 10.9.50.220: icmp_seq=0 ttl=63 time=0 ms
22 bytes from 10.9.50.220: icmp_seq=1 ttl=63 time=0 ms
22 bytes from 10.9.50.220: icmp_seq=2 ttl=63 time=0 ms
--- 10.9.50.220 ping statistics ---
**3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0/0/0 ms**
hope someone with expertise in scripting can help me out
TIA
Try this:
ping ... | awk '/packet loss/{x="Loss:" $7} /round-trip/{x="Trip:" $4} END{print x}'
If it sees a line with "packet loss" in it, it creates a string x with the packet loss percentage. If it sees a line with "round-trip" it overwrites the string x with the round trip time. At the end, it prints whatever the string x is set to.
In the light of your comments...
awk '/packet loss/ && /100/{x="Loss: " $7} /round-trip/{split($4,a,/\//);x="Ave: " a[2]} END{print x}'
So, now the "packet loss" line must also contain "100" before we pick it up. And the 4th field of the "round-trip" line is split into an array a[] using / as separator, then we take the second element of a[] as the average and store that in our output string x.
Using sed:
ping ... | sed -n -e 's/.*\(100% packet loss\).*/\1/p' \
-e 's_.*min/avg/max = [0-9]*/\([0-9]*\)/[0-9]*.*_\1_p'

In many lines of the same values that I want to be counted [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
main faile is:
785
785
788
788
883
883
883
921
921
921
921
921
921
925
925
I want to count the same values and write the results in a new file (as follows):
785 2
788 2
883 3
921 6
925 2
Thank you for your helps.
sort myFile.txt | uniq -c | awk '{ print $2 " " $1}' > myNewFile.txt
Edit: added sort and removed cat to take comments into account
And if you want only values which appear at least 4 times:
sort temp.txt | uniq -c | sort -n | egrep -v "^ *[0-3] " | awk '{ print $2 " " $1}'
Imagine your file is called t
You can do with:
cat t | sort -u | while read line #read each one element in sorted and uniqye
do
echo -n $line; # print element
cat t | grep ${line} | wc -l # read file, get only the specified and count
done
kent$ awk '{a[$0]++}END{for(x in a)print x, a[x]}' f
921 6
925 2
883 3
785 2
788 2
print only count >=4:
kent$ awk '{a[$0]++}END{for(x in a)if(a[x]>=4)print x, a[x]}' f
921 6

Shell output in Applescript

I was wondering how to extract only certain data from the output of a shell command in Applescript. I want to be able to only pass the IP address into the variable from a "ping -o" command like this:
do shell script "ping -o " & blockedURL
-- Set the IP to blockedIP --
set blockedIP to ..
but I receive this:
"PING example.com (192.0.43.10): 56 data bytes 64 bytes from
192.0.43.10: icmp_seq=0 ttl=239 time=101.587 ms
--- example.com ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev =
101.587/101.587/101.587/0.000 ms"
When I execute the ping command I receive a lot of data I dont need. Is there any way of being able to only recall the (192.0.43.10)?
set a to "PING example.com (192.0.43.10): 56 data bytes 64 bytes from 192.0.43.10: icmp_seq=0 ttl=239 time=101.587 ms
--- example.com ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 101.587/101.587/101.587/0.000 ms"
set text item delimiters to "("
set temp to text item 2 of a
set text item delimiters to ")"
set temp to first text item of temp
return temp
The above is a full applescript solution. You can also use the following to get the IP just using the shell ping -o www.google.com | cut -d'(' -f2|cut -d')' -f1 | head -n1 so in applescript it would look like this :
do shell script "ping -o " & blockedURL & " | cut -d'(' -f2 | cut -d')' -f1 | head -n1"

Resources