Suppose I have one file like:  apple  | red |   2 apple  | green |   3 orange  | yellow |  3 apple   | yellow | 1   Now I need following output as (irrespective of color value should get third number summed):  apple | red | 6 orange | yellow | 3   I tried something like below:  tail -n 4 $firstfile| while IFS=, read -r f2col1 f2col2     do         match1=$(echo $f2col2)         fruit1=$(echo $f2col1)         if [ "${fruit1}" == "${fruit1}" ]; then             match3=`expr ${match3} + ${match1}`   which doesn't seem to be right after second addition.   Could somebody give me better idea?
124264
I have below inputs with huge number of rows  11|ABCD|19900101123123445455|555|AAA|50505050|0000009030 11|ABCD|19900101123123445455|555|AAA|50505050|0000000199 13|ABCD|201803010YYY66666666|600|ETC|20180300|0000084099 11|ABCD|19900101123123445455|555|AAA|50505050|0008995001   And I need to get below output  11|ABCD|19900101123123445455|555|AAA|50505050|9004230 13|ABCD|201803010YYY66666666|600|ETC|20180300|84099   I have been trying with below awk but having too limited knowledge with arrays.  cat test|awk -F"|" '{ a[$1]++;b[$2]++;c[$3]++;d[$4]++;e[$5]++;f[$6]+=$6 }; END { for (i in a); print i, f[i]}'   I need to sum last column of column number 6 and print all first 5 columns, which are separated by pipe and last 6th column as sum of 6th column.
2867
I have 2 large files (3000 columns, 15000 rows) of the following format  file1 (tab-separated):  1/0 0/0 0/0 0/0 1/1 0/0 1/1 0/1 0/0   file2 (tab-separated):  3 5 2 1 7 10 3 4 3   I'd like to combine the values from the first column of each file with a ":" separator, then move on to the second, third, etc. columns.  Desired output (tab-separated):  1/0:3 0/0:5 0/0:2 0/0:1 1/1:7 0/0:10 1/1:3 0/1:4 0/0:3   Efficiency isn't critical, so any language is fine. I apologize if this has been asked before.
113295
I am trying the contents of an array into a file with each element of the array in a new line in the file.  IFS=$'\n'    echo "${mtches[@]}" &gt; sample1.txt     The content of mtches is "qwe" and " asd". But the sample1.txt file contains qwe asd in a single line. Why is it not taking IFS value into the picture?
60067
I have 2 files. The first, fileA looks like  TCONS_00000066  XLOC_000030     -       u       q1:XLOC_000030|TCONS_00000066|0|0.000000|0.000000|0.000000|0.000000|- TCONS_00000130  XLOC_000057     -       u       q1:XLOC_000057|TCONS_00000130|0|0.000000|0.000000|0.000000|0.000000|- TCONS_00000395  XLOC_000206     -       u       q1:XLOC_000204|TCONS_00000393|0|0.000000|0.000000|0.000000|0.000000|-   FileB looks like:  &gt;TCONS_00000001 gene=XLOC_000001 AGATGAGCTGGTGGGGATGCTCTAAGAGAACGAGAGAAGCACAGAGCAGATAAACCACACCCACAGGCAC CACCGTCCTTGTTGGTAATGAAGAAGACGAGACGACGACTTCCCCACTAGGAAACACGACGGAGGCGGAG ATGATCGACGGCGGAGAGAGCTACAGAAACATCGATGCCTCCTGTCCAATCCCCCCATCCCATTCGGTAG TTGGATTGAAGACTACCGAATAAGAGAAGCAGGCAGGCAGACAAACCCTTGAACCAAGGAGTCCTCGCTG AGGAAGCTTTGGATCCACGACGCAGCTATGGCCTCCCCGCCCACCAGGCCGCCAGCCACAACCAGCTGAC TAGGTCGCATGCATCATCAGATTTCAATCTCCCTTCGTTCCCTGTCCCTAATCCAATACCAATAGGGAGC AATCAGCTGCTCCTCGACGGCGAGGGAGATGTCGTCGGCCGCGGGCCAAGACAACGGAGATACCGCTGGG GACTACATCAAGTGGATGTGCGGCGCCGGTGGCCGTGCGGGCGGCGCCATGGCCAACCTCCAGCGCGGCG TTGGCTCCCTCGTCCGTGACATTGGCGACCCCTGCCTCAACCCATCCCCCGTTAAGGGGAGCAAAATGCT CAAACCGGAAAAATGGCACACATGTTTTGATAATGATGGAAAGGTCATAGGTTTCCGTAAAGCCCTAAAA TTCATTGTCTTAGGGGGTGTGGATCCCACTATTCGAGCTGAAGTTTGGGAATTTCTTCTTGGCTGCTATG CCTTGAGTAGTACCTCAGAGTATAGGAGGAAACTAAGAGCTGTTAGAAGGGAAAAATATCAAATTTTAGT TAGACAGTGCCAGAGCATGCACCCAAGCATTGGTACAGGTGAGCTTGCTTACGCTGTTGGATCAAAGCTA   Now, fileA contains selected transcript numbers in the first column and fileB contains sequences of all transcripts. I want to scan fileB for the first column of fileA and print the trailing sequences of matching transcripts along with the transcript number.
81981
In python   re.sub(r"(?&lt;=.)(?=(?:...)+$)", ",", stroke )    To split a number by triplets, e.g.:   echo 123456789 | python -c 'import sys;import re; print re.sub(r"(?&lt;=.)(?=(?:...)+$)", ",",  sys.stdin.read());'  123,456,789   How to do the same with bash/awk?
9163
I can read the numbers and operation in with:  echo "First number please" read num1 echo "Second number please" read num2 echo "Operation?" read op   but then all my attempts to add the numbers fail:  case "$op" in   "+")     echo num1+num2;;   "-")     echo `num1-num2`;; esac   Run:  First number please 1 Second mumber please 2 Operation? +   Output:  num1+num2   ...or...  echo $num1+$num2;;  # results in: 1+2       ...or...  echo `$num1`+`$num2`;;  # results in: ...line 9: 1: command not found   Seems like I'm getting strings still perhaps when I try add add ("2+2" instead of "4").
104222
I'm trying to have a small function that I then call.  To keep it simple I just want to validate that 12 is divisible by 2,3 and 4  The code is:  divisible_by () {   under_test=12   from=2   to=4   divisible=0   for ((check=from; check&lt;=to; check++)) {     echo "check= $check"     if [ $under_test % $check -ne 0 ]; then       divisible=1     fi     }   return $divisible }  x=divisible_by if [ $x -eq 0 ]; then   echo "true" else   echo "false" fi   Currently I get   ./5_divisible_by_1_to_10.sh: line 16: [:  divisible_by: integer expression expected false   I'm also finding it hard to pass the number in as a parameter but maybe it's related.
77738
cat file_1  my colour is red my rose is red my colour is blue my rose id blue  cat file_2  red blue  cat output_file should be my colour is red my colour is blue   here i am using   cat file_2 | while read line;do cat file_1 | grep "$line" | head -1;done   here i am trying to get the top most line containing the pattern "red" and "blue" which is present in the file_2  is there any other way  to do , as fast as possible, while loop is taking time
12267
I am making the check for update script for my theme  I have 2 text files. First one is called "current.txt" and contains the current version. There is 4.1.1 string in that text file.  Second one is called "latest.txt" and contains the latest version. There is 4.2 string in this text file.  So here is the code  echo "Checking update"; x=$(cat ./current.txt) y=$(cat ./latest.txt) if [ "$x" -eq "$y" ] then        echo There is version $y update else        echo Version $x is the latest version fi   What it mean is if current.txt is NOT the same with latest.txt then it will say "there is version 4.2 update". If not, it will say "version 4.1.1 is the latest version"  But when I try to run it. I get this error  Checking update ./test.sh: line 4: [: 4.1.1: integer expression expected Version 4.1.1 is the latest version   So what am I doing wrong with this?
25813
I have two files.  I want to match the 1st column of first file to 1st column of second file. If there is a match I'd like to print the 2st column of second file, if not 0.  I have tried awk (awk 'FNR==NR{a[$1]=$0;next} {print a[$1]}' 2.txt 1.txt) but it is not working.  F1.txt  A B C D E F G H I J   F2.txt  A  0.5 E   1 H   0.5 J    1   Desired_output  A    0.5 B    0 C    0 D    0 E    1 F    0 G    0 H    0.5 I    0 J   1
70837
As a part of this script, I need to be able to check if the first argument given matches the first word of file. If it does, exit with an error message; if it doesn't, append the arguments to the file. I understand how to write the if statement, but not how to use grep within a script. I understand that grep will look something like this  grep ^$1 schemas.txt   I feel like this should be much easier than I am making it.  I'm getting an error "too many arguments" on the if statement. I got rid of the space between grep -q and then got an error binary operator expected.   if [ grep -q ^$1 schemas.txt ] then         echo "Schema already exists. Please try again"         exit 1 else         echo "$@" &gt;&gt; schemas.txt fi
38090
I am trying to write an if statement to test whether there are any files matching a certain pattern. If there is a text file in a directory it should run a given script.  My code currently:  if [ -f /*.txt ]; then ./script fi   Please give some ideas; I only want to run the script if there is a .txt in the directory.
26898
I have this script to divide a file over a 100000 or even more in 50000 lines.  desc()                                                     {                                                       echo $1 $2|awk '{d=$1;}                                 BEGIN {a=1;b=0}                                            { f=d"0"a;                                                while ((getline x&lt;d)&gt;0)                                   { print x&gt;&gt;f".txt"                                        if (b++&gt;=50000)                                            {                                                         close(f);b=0;a++;f=(a&lt;10)?d"0"a:f=d""a                }                                                  }                                                   }                                                   END {print "Terminado ... ! "}'                         }                                                      if [ -f $1 ]; then                                              desc $1 $2                                      fi                                                        When I execute it I got this error message:  sh-3.2$ parte.sh pa.txt                                  : command not found                                      '/parte.sh: line 2: syntax error near unexpected token ` '/parte.sh: line 2: `desc()                              sh-3.2$                                                    Can anyone help to solve it?
76754
the function awk '!seen[$0]++' print the new occurrences in an input pipe.  I am however unable to add a pipe after. E.g  my_function_with_ouput | awk '!seen[$0]++' | while read j do echo $j done   Doesn't produce any output.
62514
File1:  123 234 345 456   File2:  123 234 343 758   Expected output: File3:  TRUE TRUE FALSE FALSE   so the code should compare two files and print 'TRUE' if it matches otherwise it should print 'FALSE' in the new file. Could anyone please provide the solution for this?
17881
I'm trying to retrieve the the matching lines from a file, however the file that has the "index" or list of matches to retrieve also has duplicate entries and I want to print out the duplicates as well.  Example:  File 1 (index file or list of strings to match)  abc tgf abc ggg aaa fff   File 2 (what I want to retrieve for each of the matches in file 1)  Fred   1   3   abc Amy    2   4   ggg Dan    5   6   tgf Mike   9   1   fff Steve  2   1   aaa   My desired result would be (order is not important, just that I match every line):  Fred   1   3   abc Dan    5   6   tgf Fred   1   3   abc Amy    2   4   ggg Steve  2   1   aaa Mike   9   1   fff   I'm familiar and have tried grep as follows, but it does not print the duplicate entries.  grep -Fwf file1.txt file2.txt  Fred   1   3   abc Amy    2   4   ggg Dan    5   6   tgf Mike   9   1   fff Steve  2   1   aaa
55899
I have written a small script to count number of occurrences of logs in a particular file. The following is my code:   #!/bin/sh count=1 while read line do     found=$( grep M $line )     OCCURENCE=$( grep M /log.txt)     if [ "$OCCURENCE" ]; then         echo $count")" $line &gt;&gt; filesave.txt     fi     count=$((count+1)) done &lt; log1.txt   but I am getting this error:  ./count.sh ./count.sh: syntax error at line 10: `found=$' unexpected