LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-11-2011, 04:05 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Rep: Reputation: 2
delimiter separate string to array


Hi, I attempted to separate the text output by some software into array, but I can't find any pattern or function, I tried cut with space, but doesn't seem to work, partly because I can't find the position:
Code:
|  .                                  DR        0  Mon Nov  7 02:05:42 2011|
|  ..                                 DR        0  Mon Nov  7 02:05:42 2011|
|  12.05.08.rtf                        A     2890  Mon May 12 01:31:43 2008|
|  7[1][1].3CHCH  annoying shit.txt                   A      616  Mon Nov 27 07:19:18 2006|
|  app_online_en.pdf                   A  2176553  Mon Jun  2 19:54:15 2008|
|  Course Enrollment Form.pdf          A    69019  Fri Sep 15 23:51:23 2006|
|  Criterion Games                     D        0  Tue Aug 30 20:18:59 2011|
|  CyberLink                           D        0  Sat Jul 17 13:51:33 2010|
|  Dear Admission administrators.doc      A    24576  Tue May 20 09:19:54 2008|
|  Default.rdp                        AH        0  Fri Nov  7 22:52:12 2008|
|  desktop.ini                       AHS      556  Fri Aug 26 20:37:00 2011|
Each line is divided by "|" and "|" at the end, the elements are filename, attribute, size and date.
For example, i tried: FILENAME=$(echo "$dir1" | cut -d' ' -f1), but i got a blank string.
Thanks,
Ted
 
Old 11-11-2011, 04:35 AM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
I copy/pasted your sample data into a file named stuff.

Code:
$ gawk 'BEGIN {RS = "[|]?\n?[|]"}; {filename = $1; attribute = $2; size = $3; date = $4 " " $5 " " $6 " " $7 " " $8; print filename, attribute, size, date}' stuff

. DR 0 Mon Nov 7 02:05:42 2011
.. DR 0 Mon Nov 7 02:05:42 2011
12.05.08.rtf A 2890 Mon May 12 01:31:43 2008
7[1][1].3CHCH annoying shit.txt A 616 Mon Nov 27
app_online_en.pdf A 2176553 Mon Jun 2 19:54:15 2008
Course Enrollment Form.pdf A 69019 Fri Sep 15
Criterion Games D 0 Tue Aug 30 20:18:59
CyberLink D 0 Sat Jul 17 13:51:33 2010
Dear Admission administrators.doc A 24576 Tue May 20
Default.rdp AH 0 Fri Nov 7 22:52:12 2008
desktop.ini AHS 556 Fri Aug 26 20:37:00 2011
I think that's what you want, but there are many ways this program can break. For example, if the file was generated on Windows the line endings will be different, breaking my record separator regex.
 
1 members found this post helpful.
Old 11-11-2011, 04:53 AM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
Thanks,
but how would i go about printing them out in html form instead of saving them?
Code:
	array=$(echo "$stuff" | gawk 'BEGIN {RS = "[|]?\n?[|]"}; {filename = $1; attribute = $2; size = $3; date = $4 " " $5 " " $6 " " $7 " " $8;}')
	
	echo -n "<tr><td><a><img src=\"../style/fex/$EXT.png\" /></a></td>
	<td style=\"width:270px\"><a><font color=\"black\">$filename</font></a></td>
	<td>$attribute</td><td>$size</td>
	<td>$date<img style=\"cursor:pointer; width:15px; height:15px;\" src=\"$IMG\" /></a>"
	echo -n "</td></tr>"
Thanks in advance.
Ted
 
Old 11-11-2011, 05:16 AM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by ted_chou12 View Post
Thanks,
but how would i go about printing them out in html form instead of saving them?
That wasn't asked for in your original post. I get the idea that there is a much larger shell script we need to see from you. The program I provided should not be considered robust or reliable without some testing.

If this were my project, I would seriously consider doing it entirely in Gawk.

You can simply include the HTML codes as arguments in Gawk's print statement. String literals should be enclosed in double quotes. If you need to include double quotes in a string literal you can backslash escape them.
 
Old 11-11-2011, 05:40 AM   #5
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
hi, sorry for the misunderstanding, I am using smbclient to fetch folder directories from a windows computer, I believe this is the only circumstance that I will be using this code for. This code will appear on a webpage cgi-bin, folder. And the output will show up on the web page like a default directory browser for any webbrowser:

Code:
OUT="$(smbclient \\\\My-Computer\\'My Documents' -N >&1 <<SMB
ls
SMB)"
IFS="
"
##here i have just fetched the directory listing, and the response text is divided into array separted by an "enter" via IFS
dir=($OUT)

i=0
for dir1 in $dir[@} ; do
  if [$i -ge 1 ] ; then
     array=$(echo "$stuff" | gawk 'BEGIN {RS = "[|]?\n?[|]"}; {filename = $1; attribute = $2; size = $3; date = $4 " " $5 " " $6 " " $7 " " $8;}')
     echo -n "<tr><td><a><img src=\"../style/fex/$EXT.png\" /></a></td>
	<td style=\"width:270px\"><a><font color=\"black\">$filename</font></a></td>
	<td>$attribute</td><td>$size</td>
	<td>$date<img style=\"cursor:pointer; width:15px; height:15px;\" src=\"$IMG\" /></a>"
	echo -n "</td></tr>"
  fi

i=$(($i+1))
done
## here I skipped the first line because the first line of the smbclient shows up something like [Windows 7] [Workgroup] [my computer]...etc, so it starts from the second line.
Please let me know if it is still unclear.
Thanks,
Ted
 
Old 11-11-2011, 05:43 AM   #6
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
This is for extra info:
Code:
sh-3.1# smbclient "\\\\TED-PC\\My Documents" -N
Domain=[TED-PC] OS=[Windows 7 Professional 7600] Server=[Windows 7 Professional 6.1]
smb: \> ls
  .                                  DR        0  Mon Nov  7 02:05:42 2011
  ..                                 DR        0  Mon Nov  7 02:05:42 2011
  12.05.08.rtf                        A     2890  Mon May 12 01:31:43 2008
  7[1][1].3CHCH  annoying shit.txt                   A      616  Mon Nov 27 07:19:18 2006
  app_online_en.pdf                   A  2176553  Mon Jun  2 19:54:15 2008
  Course Enrollment Form.pdf          A    69019  Fri Sep 15 23:51:23 2006
  Criterion Games                     D        0  Tue Aug 30 20:18:59 2011
Thanks,
 
Old 11-11-2011, 05:55 AM   #7
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Exclamation

Quote:
Originally Posted by ted_chou12 View Post
hi, sorry for the misunderstanding, I am using smbclient to fetch folder directories from a windows computer, I believe this is the only circumstance that I will be using this code for. This code will appear on a webpage cgi-bin, folder. And the output will show up on the web page like a default directory browser for any webbrowser:
No. Just no. There ought to be an apache module for this functionality.

I'm sorry, but in light of this new information I don't feel qualified to help you find an adequate solution. If the Gawk program is helpful to you then I'm glad.
 
2 members found this post helpful.
Old 11-11-2011, 06:31 AM   #8
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
Thanks though.
 
  


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
[SOLVED] how to remove delimiter and append as one string greenpool Linux - Newbie 5 09-10-2011 09:26 PM
strtok behavior when character delimiter is the first char in string hajiman Programming 1 08-03-2011 08:14 PM
[SOLVED] Perl Array delimiter ashok.g Programming 8 12-23-2009 06:50 AM
MySQL splitting a string with a delimiter and taking the 1st value and update Lantzvillian Programming 7 02-01-2008 12:57 AM
Divide up lines with string delimiter elmu Programming 3 10-07-2005 09:48 AM

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

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