How to write a number on a binary file with bash [duplicate] - bash

This question already has answers here:
How to write integer to binary file using Bash? [duplicate]
(3 answers)
Closed 3 years ago.
I need to write the size of a file in a binary file using bash. But I want it to stay on 4 bytes:
The length of the file is 224128 bytes. So if I do
echo -n -e '224128' > output.bin
it will compute 6 bytes, but I want it to occupy 4 bytes. How can I do?
I don't know if I made myself clear.
Thank you very much in advance!

Perl to the rescue:
perl -we 'print pack "N", shift' 224128
To check the output, you can use e.g. xxd:
$ perl -we 'print pack "N", shift' 224128 | xxd
00000000: 0003 6b80 ..k.
And indeed,
$ echo $((16#36b80))
224128
If you need different endianness, check pack.

Related

How can I delete all lines after a specific string from a number of files [duplicate]

This question already has answers here:
removing lines between two patterns (not inclusive) with sed
(6 answers)
How do you run a command eg chmod, for each line of a file?
(9 answers)
Closed 7 months ago.
I have n files, like:
file1:
Hannah
Lars
Test 123
1aaa
2eee
file2:
Mike
Charly
Stephanie
Earl
Test 123
3ccc
4ddd
5eee
I want to remove all rows after "Test 123" from all n files.
The number of rows to delete varies between files.
There's a very similar question How can I delete all lines before a specific string from a number of files in which sed -i.bak '1,/Test 123/d' file* works perfectly, but how can I do it for all lines after a specific string?
Thanks!
Despite the comments and closed question, nothing in other threads worked. Figured out a solution:
for FILENAME in * ; do sed -i.bak '/Test 123/q' $FILENAME ; done

How to perform arithmetic on every row of a file via piping [duplicate]

This question already has answers here:
Bash Script- adding numbers to each line in file
(4 answers)
Closed 12 months ago.
Lets say we have a simple file data.txt which has the following content:
1
2
3
Is there a command to perform arithmetic on every row value of the file via piping? I'm looking for something like cat data.txt | arit "+10" which would output:
11
12
13
The best thing I know is performing arithmetic by using bc for individual values, for example echo "1+10" | bc, but I wasn't able to apply this to my own example which contains many values with trailing newlines.
You could use awk:
awk '{print $1+10}' data.txt
My first impulse is to use sed. (Hey, it's the tool I always reach for.)
cat data.txt | sed 's/$/+10/' | bc
EDIT: or like so
sed 's/$/+10/' data.txt | bc

storing result of awk in shell script [duplicate]

This question already has answers here:
How to split a string into an array in Bash?
(24 answers)
How do I split a string on a delimiter in Bash?
(37 answers)
Closed 3 years ago.
I am new to shell script and I have started it today itself. Can anyone tell me how to store and retrieve the output of this command:
cat /proc/cpuinfo | grep "MHz" | awk '/cpu MHz/ {print $4}'
This command returns just four decimal numbers but I am struggling to store these four numbers.
I have tried to store them in array but it has not been fruitful for me. Please help!!

How to print symbol using variable with code of that symbol? [duplicate]

This question already has answers here:
Conversion hex string into ascii in bash command line
(7 answers)
Closed 3 years ago.
I need to output a symbol using variable that contains code of that symbol.
I already understood that in my code example bash thinks that i'm giving it a string. So according to specificator it outputs "4".
symbolValue=41
printf '%c' $symbolValue
I expect outputing of "A" symbol in fixed code. Please help me with it.
Could you please try the following.
symbolValue=41
echo "$symbolValue" | xxd -p -r
# OR
xxd -p -r <<< "$symbolValue"
From man xxd:
-p | -ps | -postscript | -plain
output in postscript continuous hexdump style. Also known as
plain hexdump style.
-r | -revert
reverse operation: convert (or patch) hexdump into binary. If
not writing to stdout, xxd writes into its output file without
truncating it. Use the combination -r -p to read plain hexadecimal
dumps without line number information and without a particular
column layout. Additional Whitespace and line-breaks are
allowed anywhere.

cat or grep a html file to find specific text [duplicate]

This question already has answers here:
How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?
(9 answers)
Extract lines between two patterns from a file [duplicate]
(3 answers)
Closed 4 years ago.
I am trying to use bash to parse and HTML file using grep.
The HTML won't change so I should be able to find the text easy enough.
The HTML will be like this, and I just want the number which will change each time the file changes:
<div class="total">
900 files inspected,
28301 offenses detected:
</div>
grep -E '^<div class="total">.</div>' my_file.html
Ideally I just want to pull the number of offenses so in the example above it would be 28301. I would like to assign it to a variable also.
Am I close?
you can do a simple
a=$(grep -oP '(\d+)(?=\soffenses\sdetected)' abc);echo $a
will give:
28301
-o only gives the matching part of the line
-P uses perl regular expression in regex
abc is the name of the file
(\d+)(?=\soffenses\sdetected) in this reges we are just using positive lookahead to capture the require digits that are followed by a particular word
If you have GNU grep and GNU sed, you can do:
$ cat file | xargs | grep -Po '<div class=total>\K(.*?)</div>' | sed -E 's/<\/div>//; s/, /\n/'
900 files inspected
28301 offenses detected:
If you have ruby available:
$ ruby -e 'puts readlines.join[/(?<=<div class="total">).+(?=<\/div>)/m].gsub(/^[ \t]+/m,"")' file
900 files inspected,
28301 offenses detected:

Resources