The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to append spaces(say 10 spaces) at the end of each line based on the length of th prathima UNIX for Dummies Questions & Answers 17 01-28-2009 04:10 PM
Replacing leading zeros with leading spaces deepakwins UNIX for Dummies Questions & Answers 5 06-18-2008 03:06 AM
replacing tabs to spaces from files rishir Shell Programming and Scripting 6 12-15-2005 01:12 PM
finding & replacing blank rows/spaces in a file Gerry405 SUN Solaris 2 07-21-2005 05:49 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-03-2008
sais sais is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 36
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 (permalink)  
Old 10-03-2008
Bijayant Kumar Bijayant Kumar is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 19
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 (permalink)  
Old 10-04-2008
Bijayant Kumar Bijayant Kumar is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 19
Quote:
Originally Posted by Bijayant Kumar View Post
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
  #4 (permalink)  
Old 10-03-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
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.
  #5 (permalink)  
Old 10-03-2008
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,175
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}'
  #6 (permalink)  
Old 10-04-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
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 (permalink)  
Old 10-06-2008
sais sais is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 36
thanks to everyone it worked
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0