Sponsored Content
Top Forums Shell Programming and Scripting Bash to verify each line in input for specific pattern Post 302997609 by cmccabe on Wednesday 17th of May 2017 09:03:49 AM
Old 05-17-2017
Bash to verify each line in input for specific pattern

In the bash below the out put of a process is written to input. What I am trying to do is read each line in the input and verify/check it for specific text (there are always 6 lines for each file and the specific text for each line is in the description). There will always be 6 lines in each specific file in input, however the file number can vary. In this example there are 3 specific files (each color block is a file with 6 lines), but the next time there may only be two. If each line in the file is a match to description then the file is verified/good, but if it does not then the file is not.

I hope the below is a start and have commented each line. Thank you Smilie.

input
Code:
Start import validation creation: Wed May 17 06:55:34 CDT 2017
/home/cmccabe/Desktop/validate/file1.txt found expected header
/home/cmccabe/Desktop/validate/file1.txt found expected order of fields
/home/cmccabe/Desktop/validate/file1.txt R_Index is a number
/home/cmccabe/Desktop/validate/file1.txt PopFreqMax is valid
/home/cmccabe/Desktop/validate/file1.txt Quality is a character
/home/cmccabe/Desktop/validate/file1.txt HGMD and Sanger are valid
/home/cmccabe/Desktop/validate/file2.txt found expected header
/home/cmccabe/Desktop/validate/file2.txt found expected order of fields
/home/cmccabe/Desktop/validate/file2.txt R_Index is a number
/home/cmccabe/Desktop/validate/file2.txt PopFreqMax is valid
/home/cmccabe/Desktop/validate/file2.txt Quality is a character
/home/cmccabe/Desktop/validate/file2.txt HGMD and Sanger are valid
/home/cmccabe/Desktop/validate/file3.txt found expected header
/home/cmccabe/Desktop/validate/file3.txt found expected order of fields
/home/cmccabe/Desktop/validate/file3.txt R_Index is a number
/home/cmccabe/Desktop/validate/file3.txt PopFreqMax is valid
/home/cmccabe/Desktop/validate/file3.txt Quality is a character
/home/cmccabe/Desktop/validate/file3.txt HGMD and Sanger are valid
End import validation creation: Wed May 17 06:55:34 CDT 2017

Code:
#!/bin/bash
while read line; do   # read each line in input
    if  echo "$line" | grep -q "Found expected header"; then echo "LINE IS GOOD"            # read line 1 
    if  echo "$line" | grep -q "Found expected order of fields"; then echo "LINE IS GOOD"   # read line 2
    if  echo "$line" | grep -q "R_Index is a number"; then echo "LINE IS GOOD"              # read line 3
    if  echo "$line" | grep -q "PopFreqMax is valid"; then echo "LINE IS GOOD"              # read line 4
    if  echo "$line" | grep -q "Quality is a character"; then echo "LINE IS GOOD"           # read line 5
    if  echo "$line" | grep -q "HGMD and Sanger are valid"; then echo "LINE IS GOOD"        # read line 6
    fi
done < file
   file="home/cmccabe/Desktop/validate/input"   # define path to input
   string="LINE IS GOOD"                        # define string to look for in each line
   count=$(grep -c "$string" "$file")           # count string occurences
               if [[ count -gt 6 ]]; then       # if count = 6
                    echo "$string has occurred 6 times"  # string is in each file x times
                    echo "FILENAME is verified"          # specific file is verified or good
               fi                    
                 else
                    echo "FILENAME not verified"         # specific file not verified
                 fi

Description
Code:
1="Found expected header"
2="Found expected order of fields"
3="R_Index is a number"
4="PopFreqMax is valid"
5="Quality is a character"
6="HGMD and Sanger are valid"


Last edited by cmccabe; 05-17-2017 at 10:04 AM.. Reason: added description
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

merge columns into one line after a specific pattern

Hi all, im a linux newbie, plz help! I have a file - box -------- Fox-2 -------- UF29 zip42 -------- zf-CW SNF2_N Heli_Z -------- Fox -------- Kel_1 box (3 Replies)
Discussion started by: sam_2921
3 Replies

2. Shell Programming and Scripting

Bash Script verify user input is not empty and is equal to a value

I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry. I am able to test the input against 1,2, and 3, but when I test agains an... (4 Replies)
Discussion started by: spartiati
4 Replies

3. Shell Programming and Scripting

Added new line before a specific pattern problem asking

Input file: Sample1 Type pattern 842 3150 Sample1 Type range 842 3150 Sample1 Type pattern 842 1127 Sample1 Type option 842 1127 Sample1 Type length 1483 1603 Sample1 Type pattern 1483 1603 Sample1 Type length 1698 1747 Sample1 Type option 1698 1747 Sample1 Type length 1868 1935 Sample1... (13 Replies)
Discussion started by: patrick87
13 Replies

4. Programming

Print specific pattern line in c++

Input file: @HWI-BRUNOP1_header_1 GACCAATAAGTGATGATTGAATCGCGAGTGCTCGGCAGATTGCGATAAAC +HWI-BRUNOP1_header_1 TNTTJTTTETceJSP__VRJea`_NfcefbWe Desired output file: >HWI-BRUNOP1_header_1 GACCAATAAGTGATGATTGAATCGCGAGTGCTCGGCAGATTGCGATAAAC >HWI-BRUNOP1_header_2... (10 Replies)
Discussion started by: cpp_beginner
10 Replies

5. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

6. Shell Programming and Scripting

Help to just print out specific line from an input file

Hi, I have a file which contains 2,500,500,432 lines. Can I know what command I should type in order just print out particular line from the input file? eg. I just wanna to see what is the contents at line 522,484,612. Thanks for advice. (3 Replies)
Discussion started by: perl_beginner
3 Replies

7. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

8. Shell Programming and Scripting

Replace the line with specific pattern

Hello All I'm trying to change one string from a file contening this patern: xxxx-xxxx 4 numbers - end 4 other numbers This is a sample of the file: LDR 00679 am a2200205 4500 =001 3617 =008 030219s2000\\\\xxx|||||\||||\00|\0\spa\d =020 \\$a0211-1942 =041 \\$aCastellà =093 ... (5 Replies)
Discussion started by: ldiaz2106
5 Replies

9. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

10. Shell Programming and Scripting

Need to cut a specific pattern from a line

Hello, I need to cut a specific pattern from a line irrespecitve of knowing field place. I am aware to cut field if you know the place of the field, but for me The sting place varies. 1468129514436,0,something_error,Non HTTP response code: java.net.URISyntaxException,Non HTTP response... (5 Replies)
Discussion started by: mirwasim
5 Replies
All times are GMT -4. The time now is 12:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy