LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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


Closed Thread
  Search this Thread
Old 03-27-2018, 06:18 PM   #1
gappel23
LQ Newbie
 
Registered: Mar 2018
Posts: 2

Rep: Reputation: Disabled
I want to compare line by line, but only in the 2nd column from a file using bash


What I'm trying to do is read in a file, then compare the elements.
Say I have this file:
JOHN SMITH
JOHN SMITH
JIM JOHNSON

I want to be able to see if SMITH equals SMITH, if it does, I want to change the name of the second one, then store them both. Then I'll take the second SMITH and compare it to JOHNSON, if they don't equal, that's fine, store the names somewhere and move on.

I have this, but I kind of wanted to use awk, I just can't figure it out:
while read -a line
do
if [ ${line[1]} == ${line[1]} ]
then
echo -e "${line[1]}" >> names5.txt
else
echo "Not equal."
fi
done < names4.txt

Last edited by gappel23; 03-27-2018 at 06:19 PM.
 
Old 03-27-2018, 07:20 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Duplicate post. Reported for closure.
 
1 members found this post helpful.
  


Closed Thread

Tags
array, awk, bash, files, loops



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] bash read a file line by line and give percentage of task completed dunryc Programming 2 06-13-2016 03:38 PM
How to compare two files line by line and print the line which is same. nancypriyanjali Programming 9 05-30-2011 10:04 PM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:57 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration