LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2017, 04:15 AM   #1
dhirendrs
Member
 
Registered: Nov 2009
Posts: 45

Rep: Reputation: 15
special charter at bunch of .csv i want to run script with for loop but not removing in single command it is identifying but not removing


Dear all,

I am using Centos and with below command i am finding special character in file but i need to go line by line and remove character and then save file and again need to run same command i have arround 100+ files it is very much difficult to do activity for each file one by one so i try to write script with same command but not working .



==================================================================
#!/bin/bash

find ./ -name "*.csv" -o -name "*.html" -o -name "*.css" -o -name "*.js" -type f |
while read file
do
echo " $file"
mv $file $file.icv
iconv -f UTF-8 -t ISO-8859-15 $file.icv > $file
rm -f $file.icv
done



Command :
iconv -f UTF-8 -t ISO-8859-15 test.csv > tempfile
Above file not able to remove bunch of special character from all files
 
Old 09-01-2017, 04:30 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Are you sure it is finding the files correctly? Boolean logic dictates a specific precedence for logical AND and OR. So grouping is necessary to get the effect it looks like you are aiming for:

Code:
find ./ \( -name "*.csv" -o -name "*.html" -o -name "*.css" -o -name "*.js" \) -type f
About the iconv use, adding -c should remove any characters that cannot be converted.
 
1 members found this post helpful.
Old 09-01-2017, 04:53 AM   #3
dhirendrs
Member
 
Registered: Nov 2009
Posts: 45

Original Poster
Rep: Reputation: 15
not working
 
Old 09-01-2017, 04:56 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Then you'll have to define more clearly what you mean by special characters and which ones you want removed. iconv converts and if the characters are still there after the conversion, then they were successfully converted which appears to be something other than what you want.

Have you looked at tr instead? See especially the -c and -d options. I'm not sure how well it handles UTF-8 though.
 
1 members found this post helpful.
Old 09-01-2017, 06:17 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
kindly use some punctuation in your typing so that is would look like a piece of text not just a sequence of words also instead of not working please quote the exact error message
Code:
find ./ \( -name "*.csv" -o -name "*.html" -o -name "*.css" -o -name "*.js" \) -type f |
while read file
do
    if iconv -f UTF-8 -t ISO-8859-15 "$file" >"$file.ocv"; then
        printf "Converted '%s'\n" "$file"
        mv "$file.ocv" "$file"
    else
        rm -f "$file.ocv"
        printf "*** Couldn't convert '%s'\n" "$file"
    fi
done

Last edited by NevemTeve; 09-01-2017 at 06:25 AM.
 
1 members found this post helpful.
Old 09-01-2017, 06:30 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
What I would recommend is finding the files which prove to be the exception to the iconv call and focusing on only those.
 
Old 09-01-2017, 02:38 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

"not working" is not a useful description of the problem. Please explain how it is not working and include any error messages.

If some files seem to produce bad results while others do not, it woul dbe helpful to pick a specific example and try to identify why it fails. If possible reduce the file to a single line or a few lines which cause the failure and post that example here, along with the exact filename and the command you are using.
 
  


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 script help - removing certain rows from .csv file MrTuxor Programming 10 04-25-2013 11:47 AM
[SOLVED] while read loop is not looping. Removing a line with an awk command corrects this. amshone Programming 4 10-31-2012 02:54 PM
[SOLVED] Removing odd characters from CSV files using a script? carbo Programming 7 04-28-2010 02:42 AM
bash command for removing special characters from string kkpal Linux - Newbie 5 05-26-2008 08:14 AM
removing a bunch of packages.... Basslord1124 Fedora 5 03-03-2005 03:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:49 AM.

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