Replacing multiple spaces in flat file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing multiple spaces in flat file
# 1  
Old 06-25-2014
Replacing multiple spaces in flat file

Greetings all

I have a delimited text file (the delimiter is ';') where certain fields consist of many blanks e.g. ; ; and ; ;

Before I separate the data I need to eliminate these blanks altogether.

I tried the sed command using the following syntax:
sed -i 's/; *;/;;/g' <filename>

The * indicates that however many spaces are found between two semi-colons should be replaced by nothing so the string to replace with is an empty string surrounded by two semi-colons.

However I see that this only works on ASCII files whereas my files are ISO-8859. In these files this command doesn't detect multiple spaces with the * character.

Would anyone have some ideas?

Thanks & regards


Last edited by Don Cragun; 06-25-2014 at 03:43 PM.. Reason: Please use code and icode tags when spaces are important.
# 2  
Old 06-25-2014
Try ;[[:space:]]*;
# 3  
Old 06-25-2014
Did you mean the command?
sed -i 's/;[[:space:]]*;/;;/g' <filename>

If so it didn't work? FYI. originally i was depicting the space with literally a space or ' ' and so the command took the form:
sed -i 's/; *;/;;/g' <filename>

Neither work at the moment.Smilie

Last edited by Franklin52; 06-25-2014 at 10:02 AM.. Reason: Please use code tags
# 4  
Old 06-25-2014
Please post a hexdump / od of your file.
# 5  
Old 06-25-2014
awk -F; '{ for(N=1; N<=NF; N++) sub(/^[ \r\n\t]*$/, "", $N); } 1' OFS=";" inputfile > outputfile

# 6  
Old 06-25-2014
Originally Posted by RudiC
Please post a hexdump / od of your file.
Here is the content of the file:

AAA;1; ; ;      ;  ;XXX
  BBB;2; ; ;             ;  ;YYY

Moderator's Comments:
Mod Comment Code tags for code, please.
# 7  
Old 06-25-2014
That is neither a hexdump nor an od... it's not even in code tags.

Could you try my example? It should handle most whitespace, I hope.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the... (2 Replies)
Discussion started by: Hari Prasanth
2 Replies

2. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

3. Shell Programming and Scripting

Remove white spaces from flat file generated from Oracle table...

I have to export data from table into flat file with | delimited. In the ksh file, I am adding below to do this activity. $DBSTRING contains the sqlplus command and $SQL_STRING contains the SQL query. File is created properly with the data as per SQL command. I am getting white spaces in the... (1 Reply)
Discussion started by: mgpatil31
1 Replies

4. Shell Programming and Scripting

Read from Multiple character delimited flat file

Hello Guys I have a multiple character delimited flat file "|~|". when I tried to read the data the "|" character also coming Example I/P file 9882590|~|20111207|~|K03501000063005574033961|~|K|~| Command to get the second column I used awk -F"|~|" ' {print $2}' ... (2 Replies)
Discussion started by: Pratik4891
2 Replies

5. Shell Programming and Scripting

replacing spaces with null or 0 in the input file

hi i have records in my input file like this aaa|1234||2bc||rahul|tamilnadu bba|2234||b4c||bajaj|tamilnadu what i am expecting is in between two pipes if there is no character it should be replaced with null or 0 so my file will look like this aaa|1234|null|2bc|0|rahul|tamilnadu... (4 Replies)
Discussion started by: trichyselva
4 Replies

6. Shell Programming and Scripting

need help in replacing spaces in a file

hi all this is the part i am facing a problem eg data: filename : tr1 + T 40 this is a sample record in that file ... the value of T can be anything, but will be a single character. i need to cut from field two, and i am using this command cut -d " " -f2 tr1 >tr3 and the o/p is ... (7 Replies)
Discussion started by: sais
7 Replies

7. Shell Programming and Scripting

Help Replacing Characters in Flat File

I was wondering if somebody could help me with something on UNIX. I have a file that looks like this - "nelson,bill","bill","123 Main St","Mpls","MN",55444,8877,william I want to replace all comma with pipes (|), except if the comma is within double quotes. (The first field is an example of... (8 Replies)
Discussion started by: nelson553011
8 Replies

8. Shell Programming and Scripting

merge multiple lines from flat file

Hi, I have a tab delimited flat file like this: 189 Guide de lutilisateur sur lappel conférence à trois au moyen d'adaptateurs téléphoniques <TABLE><TBODY><TR><TD><DIV class=subheader>La fonction Appel conférence à trois </DIV></TD> \ <TD><?php print $navTree;?> vous permet de tenir un appel... (4 Replies)
Discussion started by: hnhegde
4 Replies

9. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies

10. Solaris

finding & replacing blank rows/spaces in a file

Can anyone help me find and replace blank rows in a file with a numeric value (ie blankrow=someTxtOrNumValue), the file is over 500,000 rows long so it would need to be the quickest way as I'll need to do this for multiple files...I would be greatfull for any suggestions....thanks sample file:... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question

Featured Tech Videos