LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-05-2016, 11:57 PM   #1
pimpant
LQ Newbie
 
Registered: Nov 2016
Posts: 1

Rep: Reputation: Disabled
Exclamation loop iterateration problem while reading .txt file


I got .txt file which its content is
-
5742060626,Ms.Pimpan Tantivaravong,Female
5742065826,Ms.Kaotip Tanti,Female
5742066666,Ms.Punbun Tan,Female
-
I create an interface script to add list in this file
First, I have to compare the input id with the exitsting id in a list.
I use cut command to read only 1st column of .txt file.
But,I got a problem when I am trying to compare it.

Here is my code.

-
!/bin/bash
#
datafile='student-2603385.txt'
while read p;
do

if [ "$id" == (echo $p | cut -d, -f1) ]
then
echo 'duplicate id'

fi
done <$datafile
-

could anyone suggest me, how should I do ?
Thank you
 
Old 11-06-2016, 01:22 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
Couple of things:

1. Please use [code][/code] tags around the code and data

2. Semi-colon after 'p' not required if 'do' is on next line

3. 'id' is never set

4. Prefer [[]] over [] see here for reasons

5. () places commands in a sub-shell, whereas, $() will return the value of the commands to the current shell

6. Get into the habit of quoting all variables until you know what you are doing and when it is desired to not have them quoted

7. Instead of cut you could also use parameter substitution

8. Another alternative would be to set IFS prior to 'read' so that the values are split for you
 
Old 11-06-2016, 02:54 PM   #3
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,465

Rep: Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606
Quote:
8. Another alternative would be to set IFS prior to 'read' so that the values are split for you
ifs works great for CSV files
-- NOT the same as your issue but an example

recently i hacked this together
Code:
#!/bin/bash
INPUT=aa1.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }

while read name size lat long
 do

echo " Location "$name" "Sol/Ceres" "
echo  "  { "
echo  "    LongLat [ $long  $lat 0 ] "
echo  "   Size  $size  "
echo  "   Type  "AA" "
echo  "   } "

 done < $INPUT
 IFS=$OLDIFS
reads through the csv line and pasts parte into a new file

to extract a few sections in a csv line and formatting into what would look like a xml file

from this
Code:
"Abellio","Ceres",32,33.2,293.09,"Planetocentric  +East  0 - 360","Approved","Dec 4, 2015","Gaul god of the apple tree.",
"Achita","Ceres",40,25.82,65.96,"Planetocentric  +East  0 - 360","Approved","Sep 21, 2015","Nigerian god of agriculture.",
to this
Code:
 Location "Abellio" "Sol/Ceres" 
  { 
    LongLat [ -293.09  33.2 0 ] 
   Size  32  
   Type  "AA" 
   } 
 Location "Achita" "Sol/Ceres" 
  { 
    LongLat [ 65.96  25.82 0 ] 
   Size  40  
   Type  "AA" 
   }
 
  


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
Reading .txt file in C btam125 Programming 16 11-06-2015 08:21 AM
Writing and Reading from a txt file. Sunny001 Programming 7 08-08-2009 11:21 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 06:11 PM
reading a txt file vidyashankara Linux - Software 2 06-22-2006 07:52 PM

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

All times are GMT -5. The time now is 08:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration