UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
The Best Selling PC Migration Utility.
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 23rd, 2003, 07:46 PM
hallian9 hallian9 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 4 hallian9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help needed in the following code!!!!!

Hi,
I need some help in the following question. I am trying to read from a file to create users but am stuck in reading the value in double quotes. Following is the file named myfile.txt I am trying to read and looks like.The values are separated by a single space and the one in double-quotes is a single value as it also contains spaces. One line represents one users information.

alc0010 500 "Aaron Lee Conyers" /home/alc0010 /bin/bash
ama0 2 "Test Al-ome" /ama0028 /usr/bin/bash

The following is the code I am trying to use to read the values from the file line by line

####################################
cat myfile.txt |
while IFS=" " read value1 value2 value3 value4 value5
do
......
###################################

But the value3 is the whole value in double quotes, but the space between just take the first word as the value3 instead of the whole in double quotes. Can any one help me out in how to solve this problem and what if there is more than one space between the values. I'll really appreciate for the help.
Thanks

Reply With Quote
  #2  
Old February 24th, 2003, 09:40 PM
zakj zakj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 52 zakj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 41 sec
Reputation Power: 6
This is not the most elegant solution, but it should work provided each input line conforms to the format of the examples you gave. Pipe your data file through this awk script, and then use IFS=":".
Code:
#!/usr/bin/awk -F\" -f

{
  gsub(" ", ":", $1)
  gsub(" ", ":", $3)
  print $1 $2 $3
}

Reply With Quote
  #3  
Old February 24th, 2003, 09:59 PM
AlCapone's Avatar
AlCapone AlCapone is offline
Mobbing Gangster
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Sep 2001
Location: "Best City" 2002 and 2003- Melbourne, Australia
Posts: 4,913 AlCapone User rank is Corporal (100 - 500 Reputation Level)AlCapone User rank is Corporal (100 - 500 Reputation Level)AlCapone User rank is Corporal (100 - 500 Reputation Level)AlCapone User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 36 m 31 sec
Reputation Power: 13
Send a message via ICQ to AlCapone Send a message via AIM to AlCapone Send a message via Yahoo to AlCapone
or this
cat myfile.txt | perl -n -e '/([\d\w]+) (\d+) "(.*?)" (.[^ ]+?) (.*)/; print $3,$/;'
__________________
And you know I mean that.

Reply With Quote
  #4  
Old February 25th, 2003, 08:18 AM
zakj zakj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 52 zakj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 41 sec
Reputation Power: 6
Let the war of escalation begin! Here is a C program which can handle quoted fields in any position; change the definition of DELIM to set the output delimiter.
Code:
#include <sys/types.h>
#include <sys/uio.h>
#include <stdio.h>
#include <unistd.h>

#define DELIM ':'
#define BUFFER 1024

int main(void)
{
  int i, insize, offset, quoted;
  char in[BUFFER], out[BUFFER];

  quoted = 0;

  while ((insize = read(0, in, BUFFER)) > 0) {
    offset = 0;
    for (i = 0; i < insize; ++i) {
      if (quoted) {
        if (in[i] == '"') {
          quoted = 0;
          --offset;
          continue;
        }
      }
      else {
        if (in[i] == '"') {
          quoted = 1;
          --offset;
          continue;
        }
        if (in[i] == ' ')
          in[i] = DELIM;
      }
      out[i - offset] = in[i];
    }
    write(1, out, insize - offset);
  }

  return 0;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Help needed in the following code!!!!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway