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
# 1  
Old 11-13-2012
Use sed to append text to filenames if text not already present

I have some html with hrefs that contain local links to pdf filenames. These filenames should have standardised names, i.e. there should be a label prior to the ".pdf" filename suffix. There can be many of these links on a single line of text and some may already have the label. For example

Code:
href="dir/blaba.pdf"... href="dir/blabla label.pdf" ... href="dir/dingdangdong.pdf"

I want the result to end up as

Code:
href="dir/blaba label.pdf"... href="dir/blabla label.pdf" ... href="dir/dingdangdong label.pdf"

i.e. the text ' label' gets preprended to the filename before the .pdf.

It has to be done with sed. I have tried

Code:
/dir\/[^l]*abel.pdf/! {s/\(dir\/[^\.]*\).pdf/\1 label.pdf/gi}

but that will add an extra ' label' to the middle of the 3.

Any suggestions on how to achieve this?

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 11-13-2012 at 04:21 AM.. Reason: code tags
# 2  
Old 11-13-2012
Is it always the 2nd href tag you are looking for as source? How do you specify your source filename to take?

Maybe like this:
Code:
root@woelp:/home/zaxxon# sed 's/[^l][^a][^b][^e][^l]\.pdf/& label.pdf/g' infile
href="dir/blaba.pdf label.pdf"... href="dir/blabla label.pdf" ... href="dir/dingdangdong.pdf label.pdf"

# 3  
Old 11-13-2012
Quote:
Originally Posted by zaxxon
Is it always the 2nd href tag you are looking for as source? How do you specify your source filename to take?

Maybe like this:
Code:
root@woelp:/home/zaxxon# sed 's/[^l][^a][^b][^e][^l]\.pdf/& label.pdf/g' infile
href="dir/blaba.pdf label.pdf"... href="dir/blabla label.pdf" ... href="dir/dingdangdong.pdf label.pdf"

The filenames can be anywhere in the line, there can be any number.

Your example doesn't give the wanted output. I want

Code:
href="dir/blaba label.pdf"... href="dir/blabla label.pdf" ... href="dir/dingdangdong label.pdf"

You've added label.pdf after the existing pdf.
# 4  
Old 11-13-2012
Quote:
Originally Posted by adb
It has to be done with sed.
Why?

Regards,
Alister
# 5  
Old 11-13-2012
Quote:
Originally Posted by alister
Why?

Regards,
Alister
That's the tool being used for all the other conversions being made.
# 6  
Old 11-13-2012
Code:
sed 's/ label\.pdf"/\
/g; s/href="\([^"]*\)\.pdf"/href="\1 label.pdf"/g; s/\n/ label.pdf"/g'

Assume that any occurrence of label.pdf" concludes an href attribute's value. Convert all such occurrences to a newline, so that those href attributes are temporarily broken and not seen by the function that will insert the label. Insert the label into the remaining href attributes whose value is a pdf file. Undo the newline conversion to restore the pre-existing labels.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 11-13-2012
Quote:
Originally Posted by alister
Code:
sed 's/ label\.pdf"/\
/g; s/href="\([^"]*\)\.pdf"/href="\1 label.pdf"/g; s/\n/ label.pdf"/g'

Assume that any occurrence of label.pdf" concludes an href attribute's value. Convert all such occurrences to a newline, so that those href attributes are temporarily broken and not seen by the function that will insert the label. Insert the label into the remaining href attributes whose value is a pdf file. Undo the newline conversion to restore the pre-existing labels.
Brilliant. I figured that was what was needed, but just wasn't able to get there...
Many thanks Alister
Antony
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