logo
down
shadow

Bash command to compare more than two files


Bash command to compare more than two files

By : tomchappy
Date : November 15 2020, 06:54 AM
Does that help I have a load of txt files formatted as below. I would like to find out lines of text that are common in all the files. I know I can do this with two files in bash with comm but can I do it with more than two files (I have 204 of them) , How about this loop?:
code :
sort < file1 > fileresult
for i in {2..204}; do
    comm -12 fileresult <(sort < file$i) > fileresult.tmp
    mv fileresult.tmp fileresult
done
awk 'FNR==1{i++}
     {a[$0]++}
     END{
         for(x in a){
              if (a[x]==i){print x;}
          }
        }
     ' file{1..204}


Share : facebook icon twitter icon
Bash compare output rather than command

Bash compare output rather than command


By : Notebook Test
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Trying to create a script to read a remote file and check the md5 checksum and alert if a mismatch yet getting an error I can't understand. , Try;
code :
if [ "$LOCALMD5" == "$REMOTEMD5" ]
bash, compare the output of a command

bash, compare the output of a command


By : Andely.C
Date : March 29 2020, 07:55 AM
To fix the issue you can do at least in my case wc -l filename does output 32 filename being 32 the number of lines. so you must strip of the filename after the line count. You could change your code from
code :
if [ $(wc -l $f) -lt 2 ]
if [ $(wc -l $f | cut -f1 -d' ') -lt 2 ]
if [ $(wc -l < $f) -lt 2 ]
I need to compare files in 2 directories and be able to tell how long each set of files has been the same in Bash

I need to compare files in 2 directories and be able to tell how long each set of files has been the same in Bash


By : Jesse Kim
Date : March 29 2020, 07:55 AM
Hope that helps Here's a rough draft of what a function for this purpose could look like. Note that I'm only writing names (in eval-safe quoted form) of files that have been identical for longer than minAge seconds -- sending email is an exercise for the reader.
code :
findConsistentlyIdenticalFiles() {
  local dirA=$1 dirB=$2 storeLoc=$3 minAge=$4
  local fnA fnB fnStore fn currentTime storedTime

  mkdir -p -- "$storeLoc" || return

  # in very new bash, faster alternative is: printf -v currentTime '%(%s)T' -1
  # ...the following command using date is the older/slower/compatible alternative:
  currentTime=$(date +%s)

  # for each file in dirA...
  while IFS= read -r -d '' fnA; do

    # strip the directory name...
    fn="${fnA#$dirA}"; fn="${fn#/}"

    # and calculate the name of a corresponding file in dirB
    fnB=${dirB}/${fn}

    # if a file in dirA doesn't exist in dirB, or the dirB copy differs, clear our flag
    if [[ ! -e $fnB ]] || ! cmp -s "$fnA" "$fnB"; then
      rm -f -- "$storeLoc/$fn"
      continue
    fi

    fnStore=$storeLoc/$fn
    if [[ -e $fnStore ]]; then
      # if we already have a marker, check whether it's over minAge seconds old
      storedTime=$(<"$fnStore")
      if (( storedTime < currentTime - minAge )); then
        printf '%q\n' "$fn"
      fi
    else
      # whereas if we don't have a marker, populate it with the current time
      mkdir -p -- "${fnStore%/*}"
      printf '%s\n' "$currentTime" >"$storeLoc/$fn"
    fi
  done < <(find "$dirA" -type f -print0)

  # ...also, prune any store contents that don't have corresponding dirA contents
  while IFS= read -r -d '' fnStore; do
    fn="${fnStore#$storeLoc}"; fn="${fn#/}"
    [[ -e $dirA/$fn ]] || rm -f -- "$fnStore"
  done < <(find "$storeLoc" -type f -print0)
}
findConsistentlyIdenticalFiles dirA dirB .cmptimes "$(( 3600 * 14 ))"
How To Compare Number Of Lines In 2 Files Using IF/Then Bash Command

How To Compare Number Of Lines In 2 Files Using IF/Then Bash Command


By : Loriann
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I would like to compare the line counts of two separate files. While I have tried using wc -l in the comparison, I'm struggling to get it working properly. , Could you please try following and let me know if this helps you.
code :
if [ "$(wc -l < file1.txt)" -eq "$(wc -l < file2.txt)" ]; then echo 'Match!'; else echo 'Warning: No Match!'; fi
cat file1.txt
I
am
Cookie

cat file2.txt

I
am
Cookie
if [ "$(wc -l < file1.txt)" -eq "$(wc -l < file2.txt)" ]; then echo 'Match!'; else echo 'Warning: No Match!'; fi
Warning: No Match!
cat file1.txt
I
am
Cookie
cat file2.txt
I
am
Cookie
if [ "$(wc -l < file1.txt)" -eq "$(wc -l < file2.txt)" ]; then echo 'Match!'; else echo 'Warning: No Match!'; fi
Match!
Bash Script Compare and Run a Command

Bash Script Compare and Run a Command


By : Hastorow
Date : March 29 2020, 07:55 AM
it fixes the issue I have a shell script that gives me a txt file containing certain numbers. For instance, " 48 347 345 221 1029 3943 1245 7899 " . It only contains one line. , Using awk you can try this:
code :
awk '{for(i=1;i<=NF;i++)if($i > 500)exit 1}' text.txt || ./command.sh
Related Posts Related Posts :
  • Security of su root in bash script
  • how to replace a string at a specific position in a csv file using bash
  • how to delete duplicate lines in a text file in unix bash?
  • Compare a variables in bash
  • How do I use sed on a string?
  • Using bash variables for range in sed
  • convert a DURATION HH:MM:SS to seconds in bash
  • Shell make system paths click-able when printed out by my shell script
  • I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements
  • AWK between 2 patterns - first occurence
  • Nginx on docker - daemonize and attach to bash
  • Delete or Replace nth line in a file after a pattern match using shell scripts
  • Actual return code for SCP
  • How to exit a bash function that expects a return
  • Bash script to split file into two files while changing output file during processing
  • Pass command line arguments via sbatch
  • variable substitution (what do you call/name this method)?
  • Call function from another shell script
  • How to Grep and Replace With Exclusions
  • shell command to remove characters after a special character in bash/shell
  • How do I run a script for 1st working day of every month in cron?
  • Bash Unexpected End of File Error After Installing RVM
  • Nested Loop with increment on inner loop?
  • video orientation detection in bash
  • Interrupt sleep in bash with a signal trap
  • Want file rename script to add number at end with fixed length
  • What's the DRYest way to replace a file with a symlink in bash
  • How to run Bitcoin's autogen.sh file?
  • Renaming files in folder and subfolders using a list- Bash Script/Unix
  • If It is a directory append text
  • What's wrong with my WHILE-DO loop, or why my script can't find the file I'm giving it on a plate?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org