Comparing to specific line in file bash script | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Comparing to specific line in file bash script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-16-2012
ShiGua ShiGua is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 16 December 2012, 1:42 PM EST
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Comparing to specific line in file bash script

Hi,

I have a value stored in x and I need to compare it to the numbers in every other line of a file. The file contains alternating lines of numbers and letters:

aaaa1111
AAAAAAAA
bbbb2222
BBBBBBBB
cccc3333
CCCCCCCC

I need to compare x to the numbers in every other line without the first four characters of that line. So I want to compare x to 1111, then to 2222, then 3333. This is to be the condition in an if statement:

if [ $x = (line 1 without first four characters) ]
or
if [ $x = (line 3 without first four characters) ]

I know I would have to use something along the lines of = $(sed something) or maybe cut, but I don't know how to reference the file by line.

Let me know if I need to be more clear. Thanks for any help!
Sponsored Links
    #2  
Old 11-16-2012
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 9 October 2014, 3:02 PM EDT
Location: East Coast
Posts: 611
Thanks: 1
Thanked 176 Times in 162 Posts
If you have access to Perl, you could try something like this:


Code:
perl -lane '$x = 2222;print "Match Found: $_ -> $1" if /.*?($x)/' file

Match Found: bbbb2222 -> 2222

Sponsored Links
    #3  
Old 11-16-2012
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 October 2014, 5:34 AM EDT
Location: San Jose, CA, USA
Posts: 4,882
Thanks: 182
Thanked 1,641 Times in 1,392 Posts
Here is a bash (or ksh) script that will do what you want entirely in shell and using awk:

Code:
x=${1:-2222}
odd=1
while read data
do      if [ $odd = 1 ] && [ "x$x" = "x${data#????}" ]
        then    echo match found in "$data"
        fi
        odd=$((1 - odd))
done < input

awk -v x="$x" 'NR%2{if(x==substr($0,5)) printf("match found in %s\n",$0)}' input

The first line in this script uses the 1st argument to your script as the value to match, but defaults to "2222" if the 1st argument is missing or is an empty string.
The Following User Says Thank You to Don Cragun For This Useful Post:
ShiGua (11-16-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script for inserting line at specific place in file barrydocks Shell Programming and Scripting 9 02-10-2011 05:37 AM
Comparing a number in a text file with a specific value Fitch UNIX for Dummies Questions & Answers 2 01-28-2011 03:49 AM
Korn/bash Script to monitor a file a check for specific data Thales.Claro Shell Programming and Scripting 4 02-02-2010 06:39 AM
How to read the value from a specific line and column BASH f_o_555 Shell Programming and Scripting 5 01-12-2010 07:57 AM
A script that read specific fileds from the 7th line in a file samura Shell Programming and Scripting 2 02-27-2009 05:26 AM



All times are GMT -4. The time now is 05:36 AM.