lsof, grepping output of [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 7 years ago.
Improve this question
To count open epubs I used this:
# - determine how many epubs are open -
NUMBER_OF_OPEN_EPUBS=0
while read -r LINE ; do
if [ "$(echo $LINE | rev | cut -c1-5 | rev)" = ".epub" ]; then
NUMBER_OF_OPEN_EPUBS="$(($NUMBER_OF_OPEN_EPUBS+1))"
fi
done < <(lsof | grep "\.epub")
# --- end determine how many epubs are open ---
and it always worked. But I wanted to extend it to fb2 files (similar to epubs) as well so I got an fb2 for testing and couldn't make it work. To illustrate the underlying problem in it's simplest form:
With 2 files, /test.epub & /test.fb2 open in fbreader in seperate windows, in bash, in lxterminal, under Ubuntu 14.04 LTS and plain Openbox:
me#nu:~$ lsof | grep "\.fb2" | tr -s " "
me#nu:~$ lsof | grep "\.epub" | tr -s " "
fbreader 28982 me 12r REG 8,5 346340 8375 /test.epub
me#nu:~$
Why doesn't lsof see the fb2? In practical terms, I suppose I could use ps, which exhibits no prejudice against fb2 files (and incidentally proves grep isn't to blame) instead, but why does lsof snub fb2 files?
==================
P.S. I edited this to put it in proper context here, even though, thanks to Mr.Hyde, it is solved already. The question as stated reflects an implied and unexamined assumption which turns out to be false. See answer.

Hyde's comment was the clue I needed. So he should get the credit. I'm not clear how that works yet, but I've lurked this site enough to know it is important to y'all.
So, right, if fbreader keeps one file open but not the other, as Hyde suggested, the question would be why. I was assuming the file type was the important thing, but once I looked at it that way the possibility was obvious and I tested it and the issue isn't type but file size. I've only found one fb2 to test my script with and it happens to be smaller than most of my epubs. I found a small epub and it behaved the same way. Presumably if the file is small enough fbreader just stores the whole thing in memeory but can't for a larger file. So, mea culpa, the problem as stated is spurious. Bottom line from a scripting pov is I need to use
ps -eo args
instead of
lsof
because ps sees the file as an argument to a command that started an open process rather than an open file and that is probably more to the point anyway. Thanks, gentles.

Related

Why does 'ls' recurse into folders? [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 1 year ago.
Improve this question
Why does ls always recurse into folders when I use a wildcard on it (I'd rather that it didn't do this and instead just showed me all items in the directory starting with m and nothing else)?
$ ls
boot/ etc/ lost+found/ mnt/ proc/ run/ srv/ tmp/ var/ init* lib32# libx32# dev/ home/ media/ opt/ root/ snap/ sys/ usr/ bin# lib# lib64# sbin#
/ $ ls m*
media:
mnt:
c/ d/ e/ wsl/
$ alias ls
alias ls='ls -FAh --color=auto --group-directories-first'
This question is off-topic here, and should be migrated to Unix & Linux or Super User; answering community-wiki for the OP's benefit, but expecting this to be closed).
ls isn't recursing. Instead, it's parsing the command line that it's given as an instruction to list the contents of the media directory.
The important thing to understand about UNIX in general is that commands don't parse their own command lines -- whatever starts a program is responsible for coming up with an array of C strings to be used as its command line argument, so a single string like ls m* can't be used.
The shell thus replaces ls m* with an array ["ls", "media"] (when media is the only match for m*).
Because ls can't tell the difference between being given media as the name of a directory to list, and being given media as the result of expanding a glob, it assumes the former, and lists the contents of that directory.
Why does ls always recurse into folders when I use a wildcard on it
It's according to the specifications if the wildcard globbing matches a directory.
From The Open Group Base Specifications Issue 7, 2018 edition:
For each operand that names a file of type directory, ls shall write the names of files contained within the directory as well as any requested, associated information.
You can however override this default behavior by using the -d option:
Do not follow symbolic links named as operands unless the -H or -L options are specified. Do not treat directories differently than other types of files. The use of -d with -R or -f produces unspecified results.

Which tools to use to list pricing for all lineageOS ROMs? [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 3 years ago.
Improve this question
I am going to buy a new smartphone which shall run lineageOS. Because the decision about the right smartphone is always guided by the pricing of the phone I would like to generate a pricing list containing all devices supported by lineageOS.
To make is super clear: This question is not about an exact step by step instruction. It is about finding the right technologies/tools to get the work done.
Which tools to use to list pricing for all lineageOS ROMs?
The only tools that I know to use to generate such a list are ´grep´ and ´wget´ in a ´bash´ environment. This is not the most efficient way to get the work done and I hope someone else can show up more suitable tools. Nevertheless here is my receipt to generate the list:
Using wget to download the devices homepage or the lineageOS statistics web service
Using awk and/or grep to filter out of the homepages source code a plain list of all devices
Using a bash for-loop calling wget for every device string into the restFull-API (is this really the right name for that technologie?) of idealo or amazon. This may look like this:
for device in $DEVICES; do wget https://www.idealo.de/preisvergleich/MainSearchProductCategory.html?q=$device > $device.html
Using grep to find the line with the device's price by filtering for the first search item. This may as ugly as this:
grep -A999999 pageContent-wrapper device.html | grep -m1 -A2 ">price-prefix" | grep "€"
Using cut to extract the price itself from the line
Output a well formatted list by using something like this:
echo $device $price
 Here we go, using a proper HTML parser and xpath :
while read device; do
printf '%s %s\n' "$device" $(
saxon-lint --html --xpath '(//div[#class="offerList-item-priceMin"])[1]/text()' \
"https://www.idealo.de/preisvergleich/MainSearchProductCategory.html?q=$device"
)
done < <(
saxon-lint --html --xpath '//a[starts-with(#href, "/model")]/text()' \
https://stats.lineageos.org/
)
Check saxon-lint
 Output
m8 128,22 €
bacon 1,89 €
riva 224,99 €
cancro 8,35 €
klte
j7eltexx
t0lte
wt88047
i9300 35,58 €
mido 558,00 €
...
 Note
Testing a5y17lte (by example) via https://www.idealo.de/preisvergleich/MainSearchProductCategory.html?q=a5y17lte there's no result.
The site is not reliable, an another example: bacon 1,89 € is not a phone :D
Another working tool is xidel :
xidel -e '//a[starts-with(#href, "/model")]/text()' https://stats.lineageos.org/

fswatch to watch only a certain file extension [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 2 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am using fswatch and only want it triggered if a file with extension .xxx is modified/created etc. The documentation and the second reference below indicate that:
All paths are accepted by default, unless an exclusion filter says otherwise.
Inclusion filters may override any exclusion filter.
The order in the definition of filters in the command line has no effect.
Question: What is the regular expression to use to exclude all files that do not match the .xxx extension?
References:
Is there a command like "watch" or "inotifywait" on the Mac?
Watch for a specific filetype
Platform:
MacOS 10.9.5.
I'm fswatch author. It may not be very intuitive, but fswatch includes everything unless an exclusion filter says otherwise. Coming to your problem: you want to include all files with a given extension. Rephrasing in term of exclusion and inclusion filters:
You want to exclude everything.
You want to include files with a given extension ext.
That is:
To exclude everything you can add an exclusion filter matching any string: .*.
To include files with a given extension ext, you add an inclusion filter matching any path ending with .ext: \\.ext$. In this case you need to escape the dot . to match the literal dot, then the extension ext and then matching the end of the path with $.
The final command is:
$ fswatch [options] -e ".*" -i "\\.ext$"
If you want case insensitive filters (e.g. to match eXt, Ext, etc.), just add the -I option.
You may watch for changes to files of a single extension like this:
fswatch -e ".*" -i ".*/[^.]*\\.xxx$" .
This will exclude all files and then include all paths ending with .xxx (and also exclude files starting with a dot).
If you want to run a command on the file change, you may add the following:
fswatch -e ".*" -i ".*/[^.]*\\.xxx$" -0 . | xargs -0 -n 1 -I {} echo "File {} changed"

Throw away all text after a blank line in bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Using bash, I want to throw away all of the text in a file after the first blank line. The blank line is used as a delimiter between records, and I only want the first record in the file. Unfortunately, the number of lines per record can change depending on what the record refers to exactly, so I can't just keep the first n lines as a global solution.
[EDIT] Here is a solution that works:
qstat -f > out.tmp
grep -A90 -B0 $1 out.tmp > out2.txt
awk '/^$/{exit}{print}' out2.txt
rm out.tmp out2.txt
where $1 points to the name of the text file to be analyzed (passed as an argument to the script that I'm writing). Thanks.
You can do this with pretty much any generic text processing tool, e.g.:
awk '$0==""{exit}{print}'
awk '/^$/{exit}{print}'
sed '/^$/q'

Unix header and footer matching patterns condition [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
I need to move my text files from processing folder to backup folder by reading the files in directory, each text file contains header, footer and other records. before moving to backup I need check that header should start with 01 and footer should start 99. If the condition satisfies i should move otherwise skip the current file and continue with other files. How to write a condition to check the 1st line should strat with 01 and last line should start with 99.
Please help me..Thanks in advance.
Sreeni
Try below:
cat file | head -1 | grep "^01" #check the first line start with 01
cat file | tail -1 | grep "^99" #check the last line start with 99
If "^" doesn't work just replace it with "/>". Both mean starting with.
you can use awk to do it, first write a awk script,e.g. t.awk
NR==1{if($1~/^01/)print}
END{if($1~/^99/)print}
and then,use awk -f t.awk your_file_name
hope to help you.

Resources