LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-22-2009, 11:45 PM   #1
tuxtutorials
Member
 
Registered: Dec 2008
Location: New York
Distribution: RedHat, Solaris
Posts: 68

Rep: Reputation: 16
Help with awk or sed.


Hi, I am stuck on the following issue with awk and need help. I currently have data that looks like this:


Directory of /home/admin/slap/test

06/09/2009 08:15 AM 954,880 bin.fla
02/09/2009 09:48 AM 173,905 bin.swf
03/23/2009 11:04 AM 2,168 bin.xml
06/09/2009 08:15 AM 830,464 dnd.fla
02/05/2009 12:47 PM 175,001 dnd.swf
02/05/2009 11:13 AM 1,835 dnd.xml
06/09/2009 08:15 AM 755,712 mcq.fla
06/09/2009 08:15 AM 46,843 mcq.swf
06/09/2009 08:15 AM 2,706 mcq.xml


Directory of /home/admin/slap/test2

06/09/2009 08:16 AM 954,880 bin.fla2
02/09/2009 09:47 AM 173,905 bin.swf2
03/23/2009 11:04 AM 2,168 bin.xml2
06/09/2009 08:17 AM 830,464 dnd.fla2
02/05/2009 12:48 PM 175,001 dnd.swf2
02/05/2009 11:10 AM 1,835 dnd.xml2
06/09/2009 08:16 AM 755,712 mcq.fla2
06/09/2009 08:14 AM 46,843 mcq.swf2
06/09/2009 08:12 AM 2,706 mcq.xml2

I need to somehow run through this file and extract out "Directory of /home/admin/slap/test" and append it to each file in the 5th field.

So the end result would be something like:

Directory of /home/admin/slap/test/bin.fla
Directory of /home/admin/slap/test/bin.swf
Directory of /home/admin/slap/test/bin.xml
etc....

for each block of files. So the next block would be

Directory of /home/admin/slap/test2/bin.fla2
Directory of /home/admin/slap/test2/bin.swf2
Directory of /home/admin/slap/test2/bin.xml2

Not sure if awk would be the right tool or if this is even possible. Any advice would be great. This was a originally a directory list of all files and we need to somehow reconstruct back to a full directory listing.

This is what I have tried so far, just confused on how to append to each file in each block.

#!bin/awk -f
#

/Directory of.*/ {
x = $3 #Set field $ into variable x
print $x
}



Thanks
 
Old 07-23-2009, 02:43 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You missed the part of code which prints $5 preceded by the directory line:
Code:
awk '/^Directory of/{string=($0 "/")} $5!=""{print string $5}' file
 
Old 07-23-2009, 02:49 AM   #3
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
Post A way to begin

Something I've made at work:

#!/usr/bin/awk -f
BEGIN {
print "Starting and searching for ",searchingfor
workingdirectory="non"
}
#If we find "Directory of", we finished the directory
/Directory of/ { workingdirectory="non" }
#We print the result if we are in a directory
{ currentline=$0
if (currentline ~ searchingfor && workingdirectory !="none") { print workingdirectory,$0 } }
#Each time we find a directory, we store its name in workingdirectory
/Directory of/ { workingdirectory=$4 }


You'll have to make a lot of changes of course.
 
Old 07-23-2009, 03:26 PM   #4
tuxtutorials
Member
 
Registered: Dec 2008
Location: New York
Distribution: RedHat, Solaris
Posts: 68

Original Poster
Rep: Reputation: 16
Thanks for the reply's will muck around with some of these solutions and try to get it working. P.S sorry for the double post.
 
  


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
sed or awk ilo Programming 1 08-22-2008 10:38 AM
SED/AWK help bioinformatics_guy Linux - Newbie 1 08-12-2008 05:07 AM
awk and/or sed linux2man Linux - General 7 01-22-2007 10:02 AM
Sed and Awk Gins Programming 7 04-19-2006 10:32 AM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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