Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-16-2012
Registered User
 
Join Date: Oct 2012
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
Registered User
 
Join Date: Oct 2007
Location: East Coast
Posts: 308
Thanks: 0
Thanked 69 Times in 67 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
Registered User
 
Join Date: Jul 2012
Location: San Jose, CA
Posts: 1,485
Thanks: 62
Thanked 535 Times in 468 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 10:52 PM.