Replacing multiple spaces in flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing multiple spaces in flat file
# 8  
Old 06-27-2014
Hello Corona

Your command does not seem to work. I admit I'm pretty new to UNIX so I copied it as it is only substituting input file and output file.

Is there anything else that needs to be substituted?

Thanks again

Tony
# 9  
Old 06-27-2014
You need to provide details - like the error message and/or output you're getting.
# 10  
Old 06-27-2014
Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 06-30-2014
Hello Corona

Thank you for your reply. Here are more details:

Command used:

Code:
awk -F '{ for(N=1; N<=NF; N++) sub(/^[ \r\n\t]*$/, "", $N); } 1' OFS=";" file1 > file2

Output:
Code:
[admdecdev@IPLSID02 QUOT]$ awk -F '{ for(N=1; N<=NF; N++) sub(/^[ \r\n\t]*$/, "", $N); } 1' OFS=";" file1 > file2
awk: OFS=;
awk:     ^ syntax error
awk: cmd. ligne:1: Each rule must have a pattern or action component

# 12  
Old 06-30-2014
There is a huge difference between what Corona688 suggested:
Code:
awk -F; '{ for(N=1; N<=NF; N++) sub(/^[ \r\n\t]*$/, "", $N); } 1' OFS=";" inputfile > outputfile

and what you showed above:
Code:
 awk -F '{ for(N=1; N<=NF; N++) sub(/^[ \r\n\t]*$/, "", $N); } 1' OFS=";" file1 > file2

Corona688's code uses semicolon as a field separator and specifies an awk script to read input from inputfile and write output to outputfile. Your version of his script uses his entire awk script as a field separator.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 13  
Old 06-30-2014
I think the semicolon must be escaped -F";"
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 14  
Old 07-02-2014
That was exactly it. With just the semicolon the command failed and I thus though maybe Corona had left the semi colon after awk inadvertently.

Now that I've put the semicolon within quotes, it works like a charm.

Thanks Corona, Don & MadeInGermany.

I do have a last question though - is it possible to maintain the same filename with a single command? That is erase the file with the blanks trimmed and name it as the original file without having to go through multiple steps like delete original file and rename new file as original?
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