Replacing punctuation marks with the help of sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing punctuation marks with the help of sed
# 1  
Old 08-27-2014
Replacing punctuation marks with the help of sed

Code:
#!/bin/bash


a=(*.pdf)

punct=((~`!@#$%^&*()_-+=|\[]{};':",./<>?))

for (( i =0; i < ${#a[i]}; i++ ))
do
sed -ri 's/$punct//g' ${a[i]}
done

I cannot use the above code, can you help me in removing all punctuation marks from file name except file extension. The idea is that once all punctuation are removed then [pdf] can be appended with [.pdf] to save the extensions.
# 2  
Old 08-27-2014
Hello Ambijat,

Please find attached the example for removing the punctuations in lines.

Code:
echo "A~\`\!@BCDEFGH" | sed 's/[[:punct:]]//g'

Output will be as follows. You can take referance from same example and then apply in your code.

Code:
ABCDEFGH


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-27-2014
If you want to verify before running the code, comment the mv part and include echo "{j}"
Code:
for i in *.pdf
do
 FNAME=${i%.*}
 EXTN=${i##*.}
 BNAME=$(tr -dc '[:alnum:]' <<<${FNAME})
 j="${BNAME}.${EXTN}"
 mv "${i}" "${j}"
done

This User Gave Thanks to SriniShoo For This Post:
# 4  
Old 08-27-2014
Quote:
Originally Posted by RavinderSingh13
Hello Ambijat,

Please find attached the example for removing the punctuations in lines.

Code:
echo "A~\`\!@BCDEFGH" | sed 's/[[:punct:]]//g'

Output will be as follows. You can take referance from same example and then apply in your code.

Code:
ABCDEFGH

Thanks,
R. Singh
Thanks! RavinderSingh, but could you please explain the echo part--- fairly a novice on that.
Rgs!

---------- Post updated at 11:29 AM ---------- Previous update was at 11:25 AM ----------

Quote:
Originally Posted by SriniShoo
If you want to verify before running the code, comment the mv part and include echo "{j}"
Code:
for i in *.pdf
do
 FNAME=${i%.*}
 EXTN=${i##*.}
 BNAME=$(tr -dc '[:alnum:]' <<<${FNAME})
 j="${BNAME}.${EXTN}"
 mv "${i}" "${j}"
done

Thanks! Srini but a bit tough to follow-- what you mean comment on mv... and what does this <<< mean. I will grateful to you for your explanation on these.

Oh does it mean adding # before mv and put echo to see output in terminal ! Yes thanks I got it if that is the case.
rgs!
# 5  
Old 08-27-2014
Hello ambijat,

echo is having text which has punctuation in it, as per your request you need to remove the punctuation in a string so I have taken an example of same string.
Then in sed part I have used s/ which tells sed to perform substitute operation, then I have used [[:punct:]] which will substitute all punctuations to s/[[:punct:]]// NONE.
Then at last s/[[:punct:]]//g stands for global, which means that all matching occurrences in the line would be replaced.

kindly let me know if you have any queries. Also please take a look to man sed too.

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-27-2014 at 03:26 AM..
# 6  
Old 08-27-2014
Code:
#!/bin/bash


a=(*.pdf)


for (( i =0; i < ${#a[i]}; i++ ))
do
echo ${a[i]} | sed 's/[[:punct:]]/_/g' | sed -e 's/_.pdf/.pdf/g'

done

Kindly, see I have tried to update it.

---------- Post updated at 11:50 AM ---------- Previous update was at 11:47 AM ----------

Quote:
Originally Posted by ambijat
Code:
#!/bin/bash


a=(*.pdf)


for (( i =0; i < ${#a[i]}; i++ ))
do
echo ${a[i]} | sed 's/[[:punct:]]/_/g' | sed -e 's/_.pdf/.pdf/g'

done

Kindly, see I have tried to update it.
Oh Thanks folks it does the trick my only mistake was to use _.pdf instead of _pdf thanks!

But the task is now how to direct the output to change the filename, can you suggest.

---------- Post updated at 11:57 AM ---------- Previous update was at 11:50 AM ----------

Code:
#!/bin/bash


a=(*.pdf)


for (( i =0; i < ${#a[i]}; i++ ))
do

b=`echo ${a[i]} | sed 's/[[:punct:]]/_/g' | sed -e 's/_pdf/.pdf/g'`

mv ${a[i]} $b
done

This is my final version and it works can you comment on it to make it better. once again thanks
rgs!
# 7  
Old 08-27-2014
Hello ambijat,

Untested but you can try awk also for same.

Code:
echo ${a[i]} | awk '{gsub(/[[:punct:]]/,X,$0); gsub("pdf",".pdf",$0);print $0}'


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding text from a variable using sed (Or awk) with punctuation

Hi All, I would have though this would have been simple, but... I have text in a variable that I need to insert into a bunch of other files... The text is simple: ... (2 Replies)
Discussion started by: joeg1484
2 Replies

2. Shell Programming and Scripting

Replacing using sed

hi Guys, I have a rar file which consists of 10 files. each file has a space in its file name. how can i replace all spaces with _ i can replace them using sed but the thing is i need to replace using a script and not command. can anyone help me out??:confused: (2 Replies)
Discussion started by: rajeshb6
2 Replies

3. Shell Programming and Scripting

after sed command file shows '????' question marks

Hi All, I am trying to replace a string in a oracle LDT file file extension .ldt. I am using the following command: sed "s/Test Number Viewer/Test Number 1/g" TEST_LIEN_FORM.ldt > TEST_LIEN_FORM.ldt.tmp mv TEST_LIEN_FORM.ldt.tmp TEST_LIEN_FORM.ldt But after the command, the file... (2 Replies)
Discussion started by: veena484
2 Replies

4. Shell Programming and Scripting

grep ignoring punctuation

I have a file xxx.txt containing winter_kool sugar_"sweet" Is there anyway i can grep xxx.txt for strings without using punctuations. for eg: `grep sugarsweet xxx.txt` should give output : sugar_"sweet" (2 Replies)
Discussion started by: jack_gb
2 Replies

5. UNIX for Dummies Questions & Answers

Problems with deleting punctuation and apostrophes

Dear All, I have a file which I want to get the list of frequency of each word, ignoring list of stop words and now I have problems which punctuations and " 's ". what I am doing is: sed 's///g' file01.txt > file01-clear.txt cat file01-clear.txt | tr "" ""| tr ' ' '\012' |sort |uniq -c... (3 Replies)
Discussion started by: A-V
3 Replies

6. Shell Programming and Scripting

replacing by sed

hi my input file has got >,,,, or >, or >,,,,,, there are independent number of commas after >.... i want the o/p as > only that is just to remove "," after">" another is: i want to replace the last line of the file and to replace it by "hello"...how to do?... any nice script plz help (2 Replies)
Discussion started by: Indra2011
2 Replies

7. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

8. What is on Your Mind?

Names/nicknames for certain punctuation

I know that forward slash and backslash are "whack" and "backwhack," and I know that a pound-sign or number sign is "crunch" and an exclamation point, "bang." What I would like to know is whether or not there's a popular nickname for the dollar sign. I call it "cash," but that may just be Yank... (6 Replies)
Discussion started by: SilversleevesX
6 Replies

9. Shell Programming and Scripting

replacing using sed

its again sed question. i have line - sed "s/$old/$new/g" "$f" > $TFILE && mv $TFILE "$f" working well if old="myoldfile" new="mynewfile" but if i want old="/home/shailesh/1test/" new="/home/shailesh/workspace/" it gives error like sed: -e expression #1, char 9: unknown option to... (2 Replies)
Discussion started by: shailesh_arya
2 Replies

10. Shell Programming and Scripting

whacky punctuation dealies

Say I have a command that looks like this: host=$(/usr/bin/host xxx.xxx.xxx.xxx) What is the significance of the $() I know what happens when I don't include them, and I know what happens when I do, but... Why doo it woik wit $() Sorry for the lame question :o (3 Replies)
Discussion started by: [MA]Flying_Meat
3 Replies
Login or Register to Ask a Question