Use sed to append text to filenames if text not already present


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use sed to append text to filenames if text not already present
# 8  
Old 11-13-2012
Inspired by Alister's code,

Assuming '@' is unused in your pdf name the very last substitution may be saved

Code:
sed 's/ label\.pdf"/@.pdf"/g; s/href="\([^"@]*\)@*\.pdf"/href="\1 label.pdf"/g' yourfile

But this code also assumes that the label you are inserting are the same than those initially present in the file (as per your example)

ah .... but in this case it could just be written

Code:
sed 's/ label\.pdf"/.pdf"/g; s/href="\([^"]*\)\.pdf"/href="\1 label.pdf"/g' yourfile

... ok finally my assumption was stupid then .... Smilie

Last edited by ctsgnb; 11-13-2012 at 05:56 PM.. Reason: Reinventing the wheel ... ;)
This User Gave Thanks to ctsgnb For This Post:
# 9  
Old 11-13-2012
Quote:
Originally Posted by ctsgnb
Inspired by Alister's code,

Assuming '@' is unused in your pdf name the very last substitution may be saved

Code:
sed 's/ label\.pdf"/@.pdf"/g; s/href="\([^"@]*\)@*\.pdf"/href="\1 label.pdf"/g' yourfile

But this code also assumes that the label you are inserting are the same than those initially present in the file (as per your example)

ah .... but in this case it could just be written

Code:
sed 's/ label\.pdf"/.pdf"/g; s/href="\([^"]*\)\.pdf"/href="\1 label.pdf"/g' yourfile

... ok finally my assumption was stupid then .... Smilie
I was just wondering about the @, there do appear to be @ signs in some pdfs, but I got the picture, it's simply first removing all my labels from those where they exist and and then putting them back to all.

I was trying too hard to find a way to only find the ones I needed to change.
Thanks again.
# 10  
Old 11-13-2012
Quote:
Originally Posted by ctsgnb
But this code also assumes that the label you are inserting are the same than those initially present in the file (as per your example)

ah .... but in this case it could just be written ...
Indeed. Your solution is simpler and just as effective. Nicely done, ctsgnb.

Regards,
Alister
# 11  
Old 11-13-2012
Quote:
Originally Posted by alister
Indeed. Your solution is simpler and just as effective. Nicely done, ctsgnb.

Regards,
Alister
@Alister,

Your solution has an advantage (depending on the intended result) : if let's say the existing label_name are called 'LABEL1' , your solution allow to insert a new label_name, let's say 'LABEL2' but keeping the orinigal 'LABEL1' unchanged (by the update/rollback substitution) in that case the code i proposed would not work.

But this would still be limited to 2 different labels on a same line, then, a new convention of label_name should be setup to make label pattern matching more easy and code a more generic solution.
# 12  
Old 11-15-2012
Hi guys, thanks for your earlier input. Unfortunately, it's a little more complicated than I first thought. There are normally 2 references to the pdf file name to fix up on a single line. However, there is a path component that defines what the label should be if it is missing. For example correct versions of the filenames in the href are

'xxx_fact_sheets/blabla fact sheet.pdf'
'yyy_briefing_papers/blabla briefing paper.pdf'


So, the the 'label' I referred to earlier is either "fact sheet" or "briefing paper" and this label is defined based a part of the path prior to the file name.

What I need to handle is cases like this

'xxx_fact_sheets/blabla.pdf'
'yyy_briefing_papers/blabla.pdf'


so that these end up like the top version. There are normally 2 instances on the same line, so greed matching is not possible.

xxx and yyy are always known. I cannot therefore reduce the label.pdf to .pdf in your examples earlier and then add a known label as it will have to be taken from the two words prior to the s/bla. It seems that the rule is

- there is a _ before the two words and they are separated by _, but xxx and yyy may also contain _
- there is an 's' at the end of the second word immediately before the /

Also, the same pattern needs to ensure that it does not add the same label again if it's run again through the same script.


Any ideas?

---------- Post updated at 09:08 PM ---------- Previous update was at 08:30 PM ----------

Think I found how to do it using the first to search the xxx_fact_sheets variants and remove the fact sheets, then to add it. I can do the same with briefing papers.

Code:
/xxx_fact_sheets/ {s/ fact sheet\.pdf/.pdf/gi}
s/\(xxx_fact_sheets\/[^\.]*\).pdf/\1 fact sheet.pdf/gi


Last edited by adb; 11-15-2012 at 09:35 PM..
# 13  
Old 11-16-2012
Could you please provide
Code:
cat yourfile

or
Code:
head yourfile

so we can see how looks your input file and find an appropriate way to parse it
# 14  
Old 11-16-2012
I can't post the whole file - it's confidential, but below are 3 examples. The last one has 2 instances on the same line. The first needs ' fact sheet' before the pdf. The second needs ' briefing paper' and the last ' fact sheet' in both instances. They all need to survive multiple passes through sed.

Code:
<TABLE cellSpacing=1 cellPadding=3 border=0 bordercolor=gray width=450 align=left>
  <td width = 5%><A HREF="#" onClick="openWindow('../../../../public/content/_br_pubknowledge_fact_sheets/metaframe utility.pdf',900,600);"><IMG SRC="../../_LMSSHARED/media/small-PDF-icon.gif"border="0" align=right></a>
    <td><A HREF="#" onclick="openWindow('../../../../public/content/_br_pubknowledge_fact_sheets/metaframe utility.pdf',900,600);"style="text-decoration: none;">
        <div class="factsheetStyle">
      <b>Citrix MetaFrame Utility </b></div></font></td>
  </tr>

Code:
<tr>
    <td width = 5%><A HREF="#" onClick="openWindow('../../../../public/content/_br_pubknowledge_rescues_and_briefing_papers/Unix filter.pdf',900,600);"><IMG SRC="../../_LMSSHARED/media/small-PDF-icon.gif"border="0" align=right></a>
    <td><A HREF="#" onclick="openWindow('../../../../public/content/_br_pubknowledge_rescues_and_briefing_papers/Unix filter.pdf',900,600);"style="text-decoration: none;">
        <div class="factsheetStyle">
      <b>UNIX filter </b></div></font></td>
  </tr>

Code:
<TABLE cellSpacing=1 cellPadding=3 border=0 bordercolor=gray width=450 align=left>
<tr>
    <td width = 5%><A HREF="#" onClick="openWindow('../../../../public/content/_br_pubknowledge_fact_sheets/DeskTopBinder.pdf',900,600);"><IMG SRC="../../_LMSSHARED/media/small-FS-icon-v2.gif"border="0" align=right></a><td><A HREF="#" onclick="openWindow('../../../../public/content/_br_pubknowledge_fact_sheets/DeskTopBinder.pdf',900,600);"style="text-decoration: none;"><div class="factsheetStyle"><b>DeskTopBinder </b></font></td>
</tr>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to preserve the same text and modify the same text to append?

Hi, I have an issue modifying a file. Below is the content of the file. File name - a.txt SELECT LISTAGG(SCORE), LISTAGG(VB), LISTAGG(SCORE*12 + 3), LISTAGG(DOB) from T_CONCAT ; SELECT LISTAGG(SCORE), LISTAGG(VB), LISTAGG(SCORE*100 + 3), from... (1 Reply)
Discussion started by: Mannu2525
1 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

3. UNIX for Beginners Questions & Answers

Text not present in file

File 1: AAAAAA-100000 | ID: C110X01 AAAAAA-100000 | ID : P110X01 DDDDDD-8911694 | ID: P110X01 File2:- ------------------------------------------------------------------------ r1111111 | AAAAAA-100000 | 2011-06-16 11:47:33 +0530 (Fri, 16 Jun 2011) | 2 lines Changed paths: A... (9 Replies)
Discussion started by: vikassahalgaur
9 Replies

4. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

5. Shell Programming and Scripting

Search text and append using SED?

I have file . cat hello.txt Hello World I would like to append a string "Today " so the output is cat hello.txt Hello World Today I dont know which line number does the "Hello World" appears otherwise I could have used the Line number to search and append . (3 Replies)
Discussion started by: gubbu
3 Replies

6. UNIX for Dummies Questions & Answers

sed - append text to every line

Hi all I tried this on an old version of sed on NCR Unix MP-RAS: sed -e "s/$/nnn/" file1 >file2 This file (file1): the cat sat on the mat. the cat sat on the mat. the cat sat on the mat. becomes this (file2): the cat sat on the mat.nnn the cat sat on the mat.nnn nnn the... (3 Replies)
Discussion started by: jgrogan
3 Replies

7. Shell Programming and Scripting

Help needed in extracting text present between two headers in .txt file

Hi All, Please help me out in fllowing problem. I have text file which contains the data in following format. Contents of file.txt are setregid02 Test that setregid() fails and sets the proper errno values when a non-root user attemps to change the real or effective... (2 Replies)
Discussion started by: varshit
2 Replies

8. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

9. UNIX for Advanced & Expert Users

Searching filenames containing certain text???

Suppose there are multiple files containing certain text "abc". How to print the name of all such files with a single command from unix prompt? Thanks in advance (6 Replies)
Discussion started by: skyineyes
6 Replies

10. UNIX for Dummies Questions & Answers

using sed to append text to the end of each line

Anyone know how to use SED to append a comma to the end of each line example: field1,field2,field3,field4 If i Cat /textfile ---- How can i append the end of /textfile with a comman? (8 Replies)
Discussion started by: Redg
8 Replies
Login or Register to Ask a Question