LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-07-2006, 07:34 AM   #1
Vrajgh
Member
 
Registered: Aug 2005
Posts: 68

Rep: Reputation: 33
bash history - replacing arguments


hello

I have a directory containing mostly pdfs which I have gathered from all over the internet. I now want to sort them into directories in a more structured mannar based on their contents. To do this, I am running pdftotext to preview the contents then moving them to another dir based on what I read.

Code:
ls
pdftotext afilename.pdf | less
mv afilename.pdf adir
Now, I am particularly lazy and am trying to work out a fast way to repeat this process for different files. (Even though the length of time I have spent trying to work this out is longer that it would have taken me to do it anyway...)

The main aim is to take the second line and replace the first argument with a different file name.

Much reading as lead me to this route:
Code:
!-2:s/afilename.pdf/anewfilename.pdf/
However, this has 2 flaws.
1) I don't want to have to type the old argument
2) I don't really want to have to type all of the new argument either. Bash's Tab completion doesn't seem to work at the end of a string like that.

The first problem can be partly overcome with:
Code:
M-1 M-C-y
Which will yank the first argument of the previous command. It doesn't solve the second one though.

This seems a very indirect route to perform this substitution. Is there a better solution? Perhaps the s/ operator can take a regexp that refers to the argument rather than a string?
 
Old 03-07-2006, 08:47 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I don't have pdf2txt installed, so I tried pdf2dsc which produces a postscript document containing structure information.
ex:
%!PS-Adobe-3.0
%%Title: (LinuxSysCalls.fm)

So a sed command '/Title/s/.*(\(.*\)\.fm)/\1/' could be used to extract the title.
You could put a command like this in a bash loop for every pdf file.

I just tried this line:
pdf2dsc LexAnalysis.pdf /dev/stdout | head | sed -n '/Title/s/.*(\(.*\).fm)/\1/p'
LexAnalysis

This isn't the best example since in this case, the Title and Filename match.
Opps! The .fm isn't in another pdf document I tested, so another sed command may be needed. And some have spaces before or after and in the title.

Here is a sed script that handles these three cases.
Code:
#n
2 {
  s/%%Title:.*( *\(.*\)\.fm *)/\1/
  s/%%Title:.*( *\(.*\) *)/\1/
  s/ /_/g
  p
  q
}
Often, you will run into cases you haven't considered, so using a sed script allows you to make additions later without changing your main bash script.
 
Old 03-07-2006, 08:48 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Sorry, accidentally double posted when the wireless started acting up.

Last edited by jschiwal; 03-07-2006 at 08:54 AM.
 
Old 03-07-2006, 11:35 AM   #4
Vrajgh
Member
 
Registered: Aug 2005
Posts: 68

Original Poster
Rep: Reputation: 33
Thanks for your reply, but I think you've missed the point slightly. My question is more general. The fact that I was going through PDF documents is almost beside the point but was important to provide context. (although you might have pdftotext installed, note that it is written in full, part of xpdf I think)

Say, for example, that I had just typed a long string at the bash prompt with lots of arguments and filenames, pipes and whatnot:

Code:
$ a.out -lasd filename | otherprog | fmt | less
So, a fairly general command but something I'm not wanting to type more than once. If I wanted to do that to a lot of files in a completly automatic way it could be achieved easily with a for loop:

Code:
$ for FILE in * ; do a.out -lasd $FILE | otherprog | fmt | less ; done
which would result in only typing it once.

In this case, I needed to intervene in the middle to decide what I was going to do with the files so a loop wouldn't be that helpful. I was tring to recall that line interactively from bash's history and alter one argument. I could do this by hitting the up arrow a couple of times and then moving the cursor with the arrow keys. Seeing as there are some interesting ways to yank arguments from previous lines in the history, I thought there might be a means to easily substitute the nth argument with a different value. My s/../../ substitution in my first post was part of a command to bash's history rather than part of a sed script.

In the absence of a quick and dirty method to substitute an individual argument, the method I'd probably use in future would be to write a sing line script to wrap the whole line into a single command and pass the argument to the script in place of the filename.
 
Old 03-08-2006, 02:36 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You don't need to type the rest of the line, just the part that changes.
!!-2:s/afilename/anewfilename/

I thought that you wanted to extract the title of the pdf from the file itself and use that as the name. That would allow you to perform this operation automatically.
 
  


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
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM
logname: no login name, -bash: [: too many arguments da_kidd_er Linux - General 1 10-27-2004 02:09 PM
[bash-scripting]functions + arguments hylke Programming 14 10-05-2004 01:48 AM
bash arguments stuckinhell Programming 6 08-13-2004 05:10 AM
bash scripting - referring to external arguments into loops linsson Linux - General 2 07-23-2004 12:24 PM

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

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