LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-21-2012, 01:01 PM   #16
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by druuna View Post
Hi,

Have a look at this
Code:
#!/bin/bash

awk -F"/" '
/\// { gsub(/[ ]+/,"",$0) }
{
  if ( $4 != "" ) {
    OUTFILE = $4
  } else {
    print $0 > OUTFILE
  }
}' testing-split.csv
I'm assuming that Toronto MDS_GW2 is not unique, so I used /\// which looks for a forward slash (which needs to be escaped, it has special meaning in awk). I also believe $3 should be $4.
when i tried to run that i got the following:

Code:
-bash-3.2$ ./split.sh
awk: cmd. line:6: (FILENAME=testing-split.csv FNR=1) fatal: expression for `>' redirection has null string value

-bash-3.2$ awk -f split.sh
awk: split.sh:3: awk -F"/" '
awk: split.sh:3:           ^ invalid char ''' in expression
 
Old 03-21-2012, 01:08 PM   #17
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Remove the double quotes around /\//
Code:
/\//  vs  "/\//"
They are not there in my code, but are in your code.....

EDIT: Or just copy/paste my code......
 
Old 03-21-2012, 01:12 PM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I notice that you are on an older version of bash, which version and derivative (ie. gawk, mawk, etc) of awk are you using?
 
Old 03-21-2012, 01:13 PM   #19
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Remove the double quotes around /\//
Code:
/\//  vs  "/\//"
They are not there in my code, but are in your code.....

EDIT: Or just copy/paste my code......
UI am using your code. i copied and pasted the code right from here...
 
Old 03-21-2012, 01:32 PM   #20
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
So I notice that you are on an older version of bash, which version and derivative (ie. gawk, mawk, etc) of awk are you using?
Awk 3.1.5
 
Old 03-21-2012, 02:05 PM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... seems to run just fine for me too

What happens if you run the following at the command line:
Code:
awk -F" */ *" '{if ( $4 != "" )OUTFILE = $4;else print > OUTFILE}' testing-split.csv
Also I assume there is more than just the header line in the file.
 
Old 03-21-2012, 02:12 PM   #22
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
hmmm ... seems to run just fine for me too

What happens if you run the following at the command line:
Code:
awk -F" */ *" '{if ( $4 != "" )OUTFILE = $4;else print > OUTFILE}' testing-split.csv
Also I assume there is more than just the header line in the file.
Not sure why it wont work for me. I even typed it out and also just pasted the 1 liner... Nothing
 
Old 03-21-2012, 02:14 PM   #23
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by mikes88 View Post
Not sure why it wont work for me. I even typed it out and also just pasted the 1 liner... Nothing
WOW it works! i opened the file with the data and apparently theres a space at the top... so how do you get it to skip that space?
 
Old 03-21-2012, 02:44 PM   #24
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
??? more information please. You opened what file? the only way a space could be presented is if it were in your original file.
 
Old 03-21-2012, 03:03 PM   #25
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
??? more information please. You opened what file? the only way a space could be presented is if it were in your original file.
in the original data file testing-split.csv the dumb output program puts a space at the top oof the file so instead of teh data starting on line 1 it starts on line 2. That space was the issue and giving off that error. Just wondering how i can get awk to skip the first line or even just remove it before running.
 
Old 03-21-2012, 03:13 PM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
NR>1{...}
 
Old 03-22-2012, 02:36 AM   #27
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by mikes88 View Post
in the original data file testing-split.csv the dumb output program puts a space at the top oof the file so instead of teh data starting on line 1 it starts on line 2. That space was the issue and giving off that error. Just wondering how i can get awk to skip the first line or even just remove it before running.
Try this:
Code:
#!/bin/bash

awk -F"/" '/\// { gsub(/[ ]+/,"",$0) ; OUTFILE = $4 }
NR > 2 { print $0 > OUTFILE }
' testing-split.csv
Hope this helps.
 
1 members found this post helpful.
Old 03-22-2012, 09:24 AM   #28
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Hi,
Try this:
Code:
#!/bin/bash

awk -F"/" '/\// { gsub(/[ ]+/,"",$0) ; OUTFILE = $4 }
NR > 2 { print $0 > OUTFILE }
' testing-split.csv
Hope this helps.
Replace

Code:
awk -F"/" '
/\// { gsub(/[ ]+/,"",$0) }
{
  if ( $4 != "" ) {
    OUTFILE = "cpu-"$4".csv"
  } else {
    print $0 > OUTFILE
  }
}' cpu.csv
with that new code then?
 
Old 03-22-2012, 10:07 AM   #29
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by mikes88 View Post
Replace [old] with that new code then?
Yes.
 
Old 03-22-2012, 10:14 AM   #30
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Hi,
Yes.
works like a charm! once again thank you Druuna.
 
  


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
Split a large hard drive into smaller drives jayhall Ubuntu 4 12-06-2011 11:26 AM
Split large file in several files using scripting (awk etc.) chipix Programming 14 10-29-2007 11:16 AM
how to sort text file and split into smaller files michaeljoser Linux - Software 8 10-19-2007 01:50 AM
Split a large file and get the names of output files using Perl Sherlock Programming 25 02-02-2007 12:43 PM
Compress and split a big sized file into smaller files hicham007 Programming 3 07-28-2005 08:56 PM

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

All times are GMT -5. The time now is 08:05 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