LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2013, 01:06 PM   #1
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Rep: Reputation: Disabled
bash script - awk split field on specific delimiter ignore spaces.


I have an excel spreadsheet that i'm dropping into a csv file for manipulation. The spreadsheet has 12 columns, one of which is a date/time entry that is in the format "
Code:
...,Jul 23 2013  10:30am,..."
. I've been trying to awk -F"," split the fields and assign them to variables via while read loop. I need to validate the different fields. Even with the -F option i don't get a single variable (myLastUpdate) with the whole date and time, it breaks off at the whitespace.

Example row:
Code:
123.123.123.123,authoritative,ns.where.com,where.com,Jul 23 2013  10:30am,Mr Dumb guy,(201)555-1212,dguy@where.com,Books for Dumbies Inc,New York NY,entry11,entry12
what do i have to do to get the date (and other fields with whitespace in them) to be assigned to a single variable.

I have
Code:
awk -F"," '{print $1, $2,..., $12}' ${InFile} | \
while read myip mytype myns mydom mylupdt ... my12fld
do
  <test per field for valid entries>
done
I have changed reading the full line then assigning the variables via a cut line. So this is just a Can it be done question.

Last edited by oly_r; 07-23-2013 at 01:24 PM.
 
Old 07-23-2013, 01:31 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You're sending the raw awk output through stdin, which read then breaks up and stores one white-space delimited "word" at a time. Other than removing the commas, awk isn't really doing anything for you..

So why bother with awk at all when, with the proper IFS setting, read can do the splitting for you?

Code:
wile IFS=, read -ra fields ; do

    echo "The date is ${fields[4]}"

done <"$InFile"
Of course you can use individual variable names as well, as in your original.

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001

string manipulation
arrays

Last edited by David the H.; 07-23-2013 at 01:39 PM. Reason: bit of rewording
 
1 members found this post helpful.
Old 07-24-2013, 09:49 AM   #3
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
You're sending the raw awk output through stdin, which read then breaks up and stores one white-space delimited "word" at a time. Other than removing the commas, awk isn't really doing anything for you..

So why bother with awk at all when, with the proper IFS setting, read can do the splitting for you?
I have to admit, it was habit.

Quote:
Originally Posted by David the H. View Post
Code:
wile IFS=, read -ra fields ; do

    echo "The date is ${fields[4]}"

done <"$InFile"
Of course you can use individual variable names as well, as in your original.

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001

string manipulation
arrays
Ok, so i have converted it over to:

Code:
while IFS=, read -ra fields
   do
      myip=${fields[0]}
      mytype=${fields[1]}
      myns=${fields[2]}
      mydom=${fields[3]}
      mylupdt=${fields[4]}
<snip>
      if [[ test for field0 ]];then for ((i=0;i<=11;i++));do printf "${fields[${i}]}," >> field0_error.txt;done;echo >> field0_error.txt;fi
      if [[ test for field1 ]];then dothis;fi
      if [[ test for field2 ]];then dothis;fi
      if [[ test for field3 ]];then dothis;fi

  done
This does what i need and i get out of the "habit".
Thanks.
 
  


Reply



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] Awk script to look for string, show value in field 2, if not present print zero Perseus Programming 12 10-06-2011 03:40 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] bash script is reprinting part of a field from an awk statement ?? SharpyWarpy Linux - General 13 04-22-2010 03:58 AM
Bash or PHP: Split csv file based on field value? guest Programming 4 02-06-2009 12:57 AM
awk run script on field before output Geneset Programming 3 08-26-2008 04:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 PM.

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