LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-16-2012, 01:28 PM   #1
ShiGua
LQ Newbie
 
Registered: Nov 2012
Posts: 5

Rep: Reputation: Disabled
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!
 
Old 11-16-2012, 02:35 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,192

Rep: Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469
Quote:
Originally Posted by ShiGua View Post
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.
As with your other thread, post what you've written/tried so far, and tell us where you're stuck, and we'll be happy to help. However, this sounds very much like a homework question, and as a rule, you're not going to get someone here to do it for you.

If you show you've put some effort into things, and tell us where you're stuck, you're much more likely to get a favorable response.
 
Old 11-16-2012, 04:33 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
but I don't know how to reference the file by line.
Take a look at the SED addressing options. Examples:

Code:
#print lines by number (3 thru 8):
sed -n '3,8p' filename

#print lines by content (begin with AAA and end with CCC):
sed -n '/AAA/,/CCC/p' filename

#print lines containing at least one number:
sed -n '/[0-9]/p' filename
Everything you ever wanted to know about SED:
http://www.grymoire.com/Unix/Sed.html
 
1 members found this post helpful.
Old 11-16-2012, 04:42 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
another good trick here---found by Google using "sed print line)
http://www.linuxforums.org/forum/pro...using-sed.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using a variable in sed to pick a specific line from a text file, bash Sunvic Linux - Newbie 10 08-12-2012 11:03 AM
[SOLVED] Bash script to parse a file to get a set of line between a specific characters venkatrg Linux - Newbie 5 12-24-2010 06:55 AM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 01:44 PM
Comparing file sizes using a bash script. IanChristie Programming 5 12-19-2003 10:14 PM


All times are GMT -5. The time now is 09:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration