Strange problem with inotifywait, while do and ffmpeg - while-loop

Linux Mint 20.3 up to date.
I'd like to convert and rename with ffmpeg some files which appear in a folder.
When not using the ffmpeg line everything works fine (note ffmpeg is commented out here)
`
#!/bin/bash
rm flist.txt
watchdir=/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/
startdate=54
starttime=32
while read -r fullpath
do
echo "fullpath '$fullpath'" >> flist.txt
lenght=${#fullpath}
echo 'lenght ' $lenght >> flist.txt
basedate=${fullpath:lenght-startdate:4}${fullpath:lenght-startdate+5:2}${fullpath:lenght-startdate+8:2}
basetime=${fullpath:lenght-starttime:2}${fullpath:lenght-starttime+3:2}${fullpath:lenght-starttime+6:2}
echo basedate "'$basedate'" >> flist.txt
echo basetime "'$basetime'" >> flist.txt
newfilename="/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/test2/recordings-convert/"$basedate"-"$basetime"-doorcam".mkv
# ffmpeg -y -i $fullpath -c:v copy -movflags +faststart $newfilename &> /dev/null
done < <(inotifywait -m -r "$watchdir" --format '%w%f' -e create -e moved_to | grep '\.dav$' --line-buffered)
This does produce the desired result without ffmeg conversion:
$cat flist.txt
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/00/00.01.33-00.01.55[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '000133'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/00/00.29.26-00.29.48[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '002926'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/00/00.33.09-00.33.32[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '003309'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/02/02.23.35-02.23.56[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '022335'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/02/02.27.19-02.27.40[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '022719'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/04/04.08.22-04.08.43[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '040822'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/06/06.50.14-06.50.40[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '065014'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/06/06.50.43-06.51.28[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '065043'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/06/06.56.56-06.58.02[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '065656'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/09/09.18.31-09.19.09[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '091831'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/09/09.47.54-09.55.00[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '094754'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/09/09.55.00-09.55.50[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '095500'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/10/10.20.23-10.20.44[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '102023'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/10/10.24.24-10.24.44[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '102424'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/11/11.00.39-11.01.34[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '110039'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/13/13.26.22-13.26.53[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '132622'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/16/16.00.09-16.01.04[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '160009'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/16/16.38.46-16.39.15[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '163846'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/16/16.56.05-16.56.30[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '165605'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/16/16.56.30-16.57.05[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '165630'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/19/19.24.56-19.25.33[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '192456'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/19/19.51.13-19.52.00[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '195113'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/19/19.52.37-19.53.10[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '195237'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.05.29-20.06.02[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '200529'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.26.44-20.27.38[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '202644'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.35.20-20.35.41[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '203520'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.38.57-20.39.17[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '203857'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.40.49-20.41.11[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '204049'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.44.27-20.44.47[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '204427'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.49.09-20.49.46[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '204909'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.51.09-20.51.58[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '205109'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/22/22.33.25-22.33.46[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '223325'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/22/22.38.48-22.39.10[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '223848'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/22/22.50.14-22.50.35[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '225014'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/23/23.04.43-23.05.03[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '230443'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/23/23.08.18-23.08.40[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '230818'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/23/23.19.51-23.20.11[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '231951'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/23/23.23.27-23.23.48[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '232327'
`
If I remove the comment and do the ffmpeg thing it fails for some files because $fullpath is wrong:
$cat flist.txt
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/00/00.01.33-00.01.55[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '000133'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/00/00.29.26-00.29.48[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '002926'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/00/00.33.09-00.33.32[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '003309'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/02/02.23.35-02.23.56[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '022335'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/02/02.27.19-02.27.40[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '022719'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/04/04.08.22-04.08.43[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '040822'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/06/06.50.14-06.50.40[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '065014'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/06/06.50.43-06.51.28[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '065043'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/06/06.56.56-06.58.02[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '065656'
fullpath 'edia/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/09/09.18.31-09.19.09[M][0#0][0].dav'
lenght 117
basedate '20220611'
basetime '091831'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/09/09.47.54-09.55.00[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '094754'
fullpath '1/dav/09/09.55.00-09.55.50[M][0#0][0].dav'
lenght 41
basedate '][0#][].'
basetime '095500'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/10/10.20.23-10.20.44[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '102023'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/10/10.24.24-10.24.44[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '102424'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/11/11.00.39-11.01.34[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '110039'
fullpath 'edia/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/13/13.26.22-13.26.53[M][0#0][0].dav'
lenght 117
basedate '20220611'
basetime '132622'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/16/16.00.09-16.01.04[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '160009'
fullpath 'dia/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/16/16.38.46-16.39.15[M][0#0][0].dav'
lenght 116
basedate '20220611'
basetime '163846'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/16/16.56.05-16.56.30[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '165605'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/16/16.56.30-16.57.05[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '165630'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/19/19.24.56-19.25.33[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '192456'
fullpath 'edia/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/19/19.51.13-19.52.00[M][0#0][0].dav'
lenght 117
basedate '20220611'
basetime '195113'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/19/19.52.37-19.53.10[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '195237'
fullpath 'edia/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.05.29-20.06.02[M][0#0][0].dav'
lenght 117
basedate '20220611'
basetime '200529'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.26.44-20.27.38[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '202644'
fullpath 'edia/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.35.20-20.35.41[M][0#0][0].dav'
lenght 117
basedate '20220611'
basetime '203520'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.38.57-20.39.17[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '203857'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.40.49-20.41.11[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '204049'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.44.27-20.44.47[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '204427'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.49.09-20.49.46[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '204909'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/20/20.51.09-20.51.58[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '205109'
fullpath 'edia/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/22/22.33.25-22.33.46[M][0#0][0].dav'
lenght 117
basedate '20220611'
basetime '223325'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/22/22.38.48-22.39.10[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '223848'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/22/22.50.14-22.50.35[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '225014'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/23/23.04.43-23.05.03[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '230443'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/23/23.08.18-23.08.40[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '230818'
fullpath '/media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/23/23.19.51-23.20.11[M][0#0][0].dav'
lenght 119
basedate '20220611'
basetime '231951'
fullpath 'media/mark/d1d75ed6-f3a5-433b-9090-d95191275f6c/download/camera/2022-06-11/001/dav/23/23.23.27-23.23.48[M][0#0][0].dav'
lenght 118
basedate '20220611'
basetime '232327'
So why does it fail when ffmpeg is not commented out?
Follow-up: found this post
https://unix.stackexchange.com/questions/241535/problem-with-ffmpeg-in-bash-loop
I think this relates to my problem but I don't know how to change my code accordingly.
Edit2, I think I found the solution
ffmpeg -y -i $fullpath -c:v copy -movflags +faststart $newfilename < /dev/null &> /dev/null;

Related

Why does the value type declared in this loop keeps the value from the previous iteration?

First time posting a question here. Sorry if the title needs work. :)
Consider the following code (not the original code, but a simplified version):
Imports System.Threading.Tasks
Module Module1
Sub Main()
' just a sample array to loop in parallel.forEach.
' the sample parallel loop does not consume for this demonstration.
Dim things() As String = {"thing1", "thing2"}
Dim parallelLoopBody = Sub(s As String)
For i As Integer = 1 To 10
Dim j As Integer
For k As Integer = 1 To 10
j += (k * i)
Next
Console.WriteLine(j)
Next
End Sub
' when debugging the original code I set max 1 for easier debugging purposes
Dim po As New ParallelOptions With {.MaxDegreeOfParallelism = 1}
Parallel.ForEach(things, po, parallelLoopBody)
End Sub
End Module
This should produce the following output, as written above:
55
165
330
550
825
1155
1540
1980
2475
3025
55
165
330
550
825
1155
1540
1980
2475
3025
But when explicitly declaring j = 0 and running the code
55
110
165
220
275
330
385
440
495
550
55
110
165
220
275
330
385
440
495
550
When I stepped through the code I realized that the variable j was "keeping" the value from the last iteration of the loop.
But I would expect j to be 0 each time, because it is a value type and I am declaring it in the loop body.
I was able to get the results I expected by setting j to 0 explicitly.
Can someone explain why this happens?

Convert Number to IP Address (v4)

I'm trying to convert a four digit number into an IP Address. For example:
0001 ---> *.192.1.01
0011 ---> *.192.11.01
0111 ---> *.192.111.01
1111 ---> *.196.87.01
3458 ---> *.205.130.01
I believe the subnet mask is 255.255.192.0.
I would greatly appreciate any suggestions on the best way to do this in vb.net.
Other info:
This is for a simple pinger program where the user inputs a four digit number (the ID of the physical site that they wish to ping.) The IP Addressing scheme is simple, the 2nd & 3rd octet are used as the site number, and the fourth octet is used as the device at the site. I didn't design this scheme, hence why I'm unsure on how to get vb.net to understand it.
What I've Tried:
I thought about doing it the following way, which is extremely crude. However this would only work up to *.192.255.01 as I don't know how to split the number between the two octets once it goes over 255 in octet 3.
Private Sub btnStartPing(sender As Object, e As EventArgs) Handles btnStartPing.Click
Dim Octet1 As Integer = *
Dim Octet2 As Integer = 192
Dim Octet3 As Integer = txtSiteID.text
Dim Octet4 As Integer = 01
Dim CompleteIP As String = ""
CompletIP = Octet1 & "." & Octet2 & "." & Octet3 & "." & Octet4
'PING CompleteIP
end sub
Solution:
Dim var1 As Integer = Fix(192 + (NumericUpDown1.Value / 256))
Dim var2 As Integer = Fix((NumericUpDown1.Value Mod 256))
MsgBox("Your IP address is: " & "10." & var1 & "." & var2 & "." & "200")
End Sub
You'll need to split the number in two. Get the first half of the bits and add them to 192 and the second half of the bits are directly used.
Dim number As Integer
number = 1
Console.WriteLine("*.{0}.{1}.01", 192 + ((number And &HFF00) >> 8), number And &HFF) ' *.192.1.01
number = 11
Console.WriteLine("*.{0}.{1}.01", 192 + ((number And &HFF00) >> 8), number And &HFF) ' *.192.11.01
number = 111
Console.WriteLine("*.{0}.{1}.01", 192 + ((number And &HFF00) >> 8), number And &HFF) ' *.192.111.01
number = 1111
Console.WriteLine("*.{0}.{1}.01", 192 + ((number And &HFF00) >> 8), number And &HFF) ' *.196.87.01
number = 3458
Console.WriteLine("*.{0}.{1}.01", 192 + ((number And &HFF00) >> 8), number And &HFF) ' *.205.130.01
Take the number and split it in two
3458 = 0x0D82
0x0D
0x82
Then add 192 to the first part
0x0D + 192 = 205
0x82 = 130
This should make it
Public Function StartPing(txtSiteId As String) As String
Dim SiteId As Integer = Integer.Parse(txtSiteId)
'backslash performs integer division (no fractionary part)
'will throw an error when SiteId results in values greater than 255
'type Byte allows only values from 0 to 255
Dim Octet2 As Byte = 192 + (SiteId \ 256)
'Mod gets rest of division
Dim Octet3 As Byte = SiteId Mod 256
Return String.Format("*.{0}.{1}.01", Octet2, Octet3)
End Function

Simple rot13 encoder in vb.net

I am looking for a simple way to encode an inputted text into Rot13. I am hitting a brick wall at the stage of being able to separate out words into individual characters and integers so that I can change each one and output the result. I can do it with single letters using a simple if statement listed bellow but if anyone can help with a way of doing it for whole words I would be very appreciative.
If kInput = "a" then kOutput = "n"
Thanks, Kai
Looks like people are giving good answers to this but here's my try at it.
Dim input As String = "This is a Test!! Guvf vf n Grfg!!"
Dim result As StringBuilder = New StringBuilder()
For Each ch As Char In input
If (Not Char.IsLetter(ch)) Then
result.Append(ch)
Continue For
End If
Dim checkIndex As Integer = Asc("a") - (Char.IsUpper(ch) * -32)
Dim index As Integer = ((Asc(ch) - checkIndex) + 13) Mod 26
result.Append(Chr(index + checkIndex))
Next
Console.WriteLine(result.ToString())
EDIT: improved to remove need for uppercase check. This will properly handle case and special characters with only 1 if statement inside the loop.
It seems like you're making this way harder than it has to be. No need to separate words, etc, and definitely no need for a large If/Else block:
Public Function Rot13(ByVal input As String) As String
Dim result As Char() = input.ToCharArray()
For i As Integer = 0 To result.Length - 1
Dim temp As Integer = Asc(result(i))
Select Case temp
Case 65 to 77, 97 To 109 'A - M
result(i) = Chr(temp + 13)
Case 78 to 90, 110 To 122 'N - Z
result(i) = Chr(temp - 13)
End Select
Next i
Return New String(result)
End Function
Note that this was entered directly into the browser window and is completely untested.
Just call it once to encode, call it again to decode.
Private Function ROT13_Encode(ByVal Input As String) As String
Dim chrs As Char() = Input.ToCharArray()
Dim ReturnString As String = ""
Dim CharInt As Integer
For Each Chr As Char In chrs
CharInt = Asc(Chr)
If CharInt >= 65 And CharInt <= 77 Then 'A-M
CharInt += 13
ElseIf CharInt >= 78 And CharInt <= 90 Then 'M-Z
CharInt -= 13
ElseIf CharInt >= 97 And CharInt <= 109 Then 'a-m
CharInt += 13
ElseIf CharInt >= 110 And CharInt <= 122 Then 'm-z
CharInt -= 13
End If
ReturnString &= ChrW(CharInt)
Next
Return ReturnString
End Function

extract columns with awk

I have some text files as follows
293 800 J A 0 0 162
294 801 J R - 0 0 67
295 802 J P - 0 0 56
298 805 J G S S- 0 0 22
313 820 J R T 4 S- 0 0 152
I would like to print column4 if column5 is empty.
desired output
>filename
ARP
I used the following code. But this code prints only the filenames.
awk '{
if (FNR == 1 ) print ">" FILENAME
if ($5 == "") {
printf $4
}
}
END { printf "\n"}' *.txt
Here's one way using GNU awk:
awk 'BEGIN { FIELDWIDTHS="5 4 2 3 3 2 7 4 3" } FNR==1 { print ">" FILENAME } $5 == " " { sub(/ $/, "", $4); printf $4 } END { printf "\n" }' file.txt
Result:
>file.txt
ARP
This is not an elegant solution by any means and it is specific to this file.
You can do something like this
cut -c1-15 yourtext | awk '$5 {print $4}'
where 15 is the number of characters including column 5.
I do strongly agree with steve's suggestion to use an better alternative for your files. Or at least put a dummy/error value instead of leaving columns blank.
awk '{if(substr($0,15,1)~/ /)printf("%s",$4);}' your_file
tested below:
> cat temp
293 800 J A 0 0 162
294 801 J R - 0 0 67
295 802 J P - 0 0 56
298 805 J G S S- 0 0 22
313 820 J R T 4 S- 0 0 152
> awk '{if(substr($0,15,1)~/ /)printf("%s",$4);}' temp
ARP>
This is a starting point assuming the variations in column numbers stay the same.
awk '$5 !="" && NF<=8 {printf $4}END{print "\n"}' data.txt
yields
ARP
you can graft on the parts to display the filename.

Extract helix residues from DSSP with awk

I would like to extract helix(H) residues from DSSP files .
1CRN.dssp
31 37 A K H < S+
32 38 A V H < S+
33 39 A F H >< S-
34 40 A G G >< S+
35 41 A K G > S+
1GB5.dssp
113 242 B G H 3>>S+
114 243 B I H <45S+
115 244 B L H X45S+
116 245 B S H 3<5S+
117 246 B K T >X5S+
I want to save the output in the following format.
>1CRN
KVF
>1GB5
GILS
How can I do this with awk? Your suggestions would be appreciated!
It's the 'H' in the 5 th column that indicates "helix(H) residues"?
awk '{
if (FNR == 1 ) print ">" FILENAME
if ($5 == "H") {
printf $4
}
}
END { printf "\n"}' file
output
>tstDat.txt
KVF
IHTH