need help in replacing spaces in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help in replacing spaces in a file
# 1  
Old 10-03-2008
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
40

if i try to replace the spaces between + and T using sed

sed 's/\t/ /g' tr1>tr3

o/p :
+ T 40

it is not a tab.....

so i tried to replace it like this .....

tr -s " " "" tr1>tr3

o/p :

+ T 40 ...

it is not reading it as a space ....
more wierd is this ..
i tried to cut the file column wise ...


> cut -c 1 test1
+
> cut -c 2 test1

> cut -c 3 test1
T

i copied the record and pasted it in a notepad and i found that it is tab.

i cannot find out whether it is a space or a tab ....

can any one give me a solution ...

i am trying to do the following but not sure how to do ...
i want to replace everything that is there before tht "T" or "F" with a space... is it possible...?
# 2  
Old 10-03-2008
First of all, i am sorry to say that i didnt understood ur problem , but whatever i understood

cat test
+ T 40

cat test | tr -d " " | sed -e 's/^./ /g;s/\( \)\(.\)/\1\2 /'
T 40

Last edited by Bijayant Kumar; 10-04-2008 at 02:15 AM..
# 3  
Old 10-03-2008
Find out what the data is

You can use something like the following

Code:
>cat test | od -An -t dC -w10

This will display the ASCII codes for your input file test.
That will tell you what is in your file. A space would be code 32, while a tab would be a 9. Carriage return and line feed are 13 and 10 respectively.

Take a look at this:
Code:
> echo "+ T 40" | od -An -t dC -w10
   43   32   84   32   52   48   10

This shows the two 32 characters and that they are spaces.
# 4  
Old 10-03-2008
Aside: Both of your commands to remove spaces or tabs do not work. This is why you were misled.
Try:
To remove spaces:
tr -d " "
To remove tabs:
TAB=`echo "\0011"`
tr -d "${TAB}"

Notwithstanding "joeyg" good advice. To get a feel for whether white space is spaces or tabs you can substitute them both to make them visible.
TAB=`echo "\0011"`
cat filename|sed -e "s/${TAB}/TAB/g"|sed -e "s/ /SPACE/g"|pg
(Shell purists may cringe at this inefficient pipeline but it is only an example).

In your case the original problem is with using "cut" which requires a fixed delimiter.
If your use "awk" to get the second field it won't matter whether its a space or a tab.
cat filename|awk '{print $2}'
# 5  
Old 10-04-2008
Quote:
Originally Posted by Bijayant Kumar
First of all, i am sorry to say that i didnt understood ur problem , but whatever i understood

cat test
+ T 40

cat test | tr -d " " | sed -e 's/^./ /g;s/\( \)\(.\)/\1\2 /'
T 40

This code should be like this

cat test |sed -e 's/^.*+//g'|tr -d "\t"

It will take care any number of "tabs" or "spaces" present before the "+" sign. No matter its a "space or tabs". It will give the desired output. First give this a try at least Smilie
# 6  
Old 10-04-2008
guys please don't use cat command when you are going to use commands like awk,grep and sed because it can read the file
regards
# 7  
Old 10-06-2008
thanks to everyone it worked Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with replacing characters without moving the spaces.

Could you please advise what is the best way to edit a file without disrupting the spaces? For example: I have within my file the value below, wherein I wanted to change VALUE2 to VALUETEST. The total characters on the field of VALUE2 is 15 characters. VALUE1|VALUE2<9 spaces>|VALUE3 but... (3 Replies)
Discussion started by: asdfghjkl
3 Replies

2. 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

3. Shell Programming and Scripting

Replacing certain positions in lines with spaces

Hello, I have a file with hundreds of lines. Now I need to replace positions 750-766 in each line (whatever there is there) with spaces... how can I do that? Which command to use? The result will be all the lines in the file will have spaces in positions 750-766. Thanks! (3 Replies)
Discussion started by: netrom
3 Replies

4. Shell Programming and Scripting

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> ... (15 Replies)
Discussion started by: S. BASU
15 Replies

5. Shell Programming and Scripting

Replacing tabs with spaces

I want my program to replace tabs with spaces.1tab=4spaces.When i write aa(tab)aaa(tab)(tab)a(tab) it must show me aaxxaaaxxxxxaxxx. I think that my program works corectly but when a write aaa(tab)a it must show aaaxa but it is aaaxxxxxa.Please for help!!! That is my code: #include <stdio.h> ... (3 Replies)
Discussion started by: marto1914
3 Replies

6. Shell Programming and Scripting

Replacing the new character with spaces

Hi Experts, We are facing some while loading the "csv" file to target table.Some of the records are having values as : Account number,Name,Address "123","XYZ","302 Street,Washington,US" "456","PQR"," 3233 Some Street, Washington,US" In the above file instead reading only two records it... (11 Replies)
Discussion started by: Amey Joshi
11 Replies

7. Shell Programming and Scripting

trim spaces and replacing value

Hi, I have a file origFile.txt with values: origFile.txt .00~ 145416.02~ xyz~ ram kishor ~? ~ ~783.9 .35~ 765.76~ anh reid~ kelly woodburg ~nancy ~ ~? Now each row in the file has value for 7 columns with "~" as delimiter. The requirement was i)I need to erase the blank spaces between... (2 Replies)
Discussion started by: badrimohanty
2 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Replacing the Spaces

Hi, i have a tab dilimeted file.The records are :header is having column names.I am facing the following issue : I want to convert the spaces only for header file into "_" in the unix shell but the problem is that if i use sed command all the blank spaces are getting replaced by "_". For... (3 Replies)
Discussion started by: Amey Joshi
3 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