logo
down
shadow

Bash script to compare 2 files with different length strings


Bash script to compare 2 files with different length strings

By : user2955044
Date : November 22 2020, 10:33 AM
help you fix your problem I have two files I am trying to compare the strings in each line by line. File1 only contains a 6 character string prefix while File2 contains a 12 character string. How can I loop through the File2 to find strings that start with the 6 characters from File1 and output those to a file? , This awk one-liner does what you want:
code :
awk 'NR==FNR{a[$0];next}{for(i in a)if(substr($0,1,6)==i)print}' file1 file2
002379ED6212
005964AB3379
005964DB5496


Share : facebook icon twitter icon
bash script syntax to compare strings as integers

bash script syntax to compare strings as integers


By : user3318256
Date : March 29 2020, 07:55 AM
I hope this helps . I have a bash script I am trying to run through directories to the end of the line, for example if I have 4 directories in a row it will go down /d1/d2/d3/d4 until it finds files or other directories. If d3 has a file, it will stop there. I was doing this by counting the lines ls prints to a file and if there's 1 directory use that name in a cd command. My file is: , Put spaces around command names like [[ and [.
For example:
code :
if [[ "$ITEMCOUNT" -eq "$ONE" ]]
then
if [[ "$ITEMCOUNT" -eq "$ONE" ]]; then
if [ $DIRCOUNT == "1" ]; then
if [ "$DIRCOUNT" == "1" ]; then  # Or just 1 (no quotes around it)
how to compare strings in bash shell script

how to compare strings in bash shell script


By : Mohamad Hajjar
Date : March 29 2020, 07:55 AM
This might help you I'm new to bash scripting and i want to compare two strings, here's my code
code :
COMPARE_RESULT=sudo php /home/xxx/compareMD5.php
COMPARE_RESULT=`sudo php /home/xxx/compareMD5.php`
Bash/shell script: create four random-length strings with fixed total length

Bash/shell script: create four random-length strings with fixed total length


By : Arpana S
Date : March 29 2020, 07:55 AM
I wish this help you $RANDOM will give you a random integer in range 0..32767. Using some arithmetic expansion you can do:
code :
remaining=10

for i in {1..3}; do
    next=$((RANDOM % remaining)) # get a number in range 0..$remaining
    echo -n "$next "                 
    ((remaining -= next))
done
echo $remaining
repeat() {
    for ((i=0; i<$1; i++)); do
        echo -n $1
    done
    echo                                         
}

repeat 3
333
How to tell if two strings of any length are anagrams using bash, awk or any using command or script?

How to tell if two strings of any length are anagrams using bash, awk or any using command or script?


By : yangjingv587
Date : March 29 2020, 07:55 AM
Hope that helps The string or word can be of any length. , This may work for you:
code :
# function to cleanup a given argument by doing this:
# 1. Remove all alphanumerics
# 2. Convert to all lowercasing all characters 
# 3. Sorting all characters
# 4. Stripping all newlines 
prep() {
   fold -w1 <<< "${1//[^[:alnum:]]/}" | tr '[:upper:]' '[:lower:]' | sort | tr -d '\n'
}

# function to check if given 2 arguments are anagrams
isAnagram() {
   a=$(prep "$1")
   b=$(prep "$2")
   [[ $a = $b ]] && echo "yes" || echo "no";
}
isAnagram "William Shakespeare" "I am a weakish speller"
yes

isAnagram "Madam Curie" "Radium came"
yes

isAnagram "cat" "act"
yes

isAnagram "cat" "cot"
no
Why is my bash script not able to compare these strings?

Why is my bash script not able to compare these strings?


By : José Ángel Meza
Date : March 29 2020, 07:55 AM
I hope this helps . I don't think you are doing what you think you are doing in the if. Parentheses in bash just runs the command inside in a subshell. So you are trying to run the two arguments as commands. (The $($f) bit will expand $f, try to run it, and then substitute the result, expecting that to be a command.)
To compare strings, you probably want to do something like
code :
if [ "$f" = "$f_ghost" ]
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org