2

I have a text file which contains in each line a sql query.

For each line, I need to remove some special characters.

Expl: if I have:

select * from Users;
insert into Users values ('UR01','Kim','Director');

the result file must be match:

select * from Users
insert into Users values UR01 Kim Director
1
  • FWIW, all the solutions here will add a trailing space on each line ending in one of those chars (one can't see the space in terminal but it's there...) Commented Apr 8, 2015 at 13:15

3 Answers 3

3

You can use sed:

sed -e 's/[;,()'\'']/ /g;s/  */ /g' input.sql > output.txt

or, if you rather want to specify what characters to keep:

sed -e 's/[^a-zA-Z*0-9]/ /g;s/  */ /g' input.sql > output.txt
3
  • this delete * ... Commented Apr 8, 2015 at 8:56
  • @choroba this command delete some characters but not all Commented Apr 8, 2015 at 9:02
  • @AomineDaiki: Check the update. Commented Apr 8, 2015 at 9:03
2

Using tr:

$ tr -s "()',;" " " < data
select * from Users 
insert into Users values UR01 Kim Director 
0

Using awk:

awk '{gsub(/[;(),'\'']/," "); gsub(/  */," "); print}' your_file
4
  • -1 When I run this in a terminal, nothing happens. Please expand your answer so that it can be used without much prior knowledge. Commented Apr 8, 2015 at 9:07
  • you need to provide file at last, i edited it, check the update Commented Apr 8, 2015 at 9:09
  • @A.B i tried it in a terminal and it's ok Commented Apr 8, 2015 at 9:10
  • ok, it works now. Commented Apr 8, 2015 at 9:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.