Solved

Character replace in UNIX file

Posted on 2011-03-11
14
424 Views
Last Modified: 2012-05-11
I have a .txt file in Solaris10 server that contans "\t" characters in a few lines. How can I get rid of those characters via any command line automated command.

Here is a sample line of the test file:
 
IKOTEST-11690 \tNeed an Album art icon for each video item \t for Letter "H"     15      51208      NULL

I need the output file(same name) that will have the above line replaced by:
IKOTEST-11690 \tNeed an Album art icon for each video item  for Letter "H"      15      51208      NULL

There are tabs in the file (multiple spaces) that I cannot lose.
0
Comment
Question by:toooki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35112574
replacing ALL tabs is easy.

I believe this question is related to your other sql*loader question.

Are you sure you now want to replace ALL tabs?
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35112577
open the file with vi and issue the following command:

:%s/\\t//g

Open in new window

0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35112586
Sorry, ignore mine comment...that will erase all \t 's
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35112605
>>Sorry, ignore mine comment...that will erase all \t 's

That might be OK.  

The other question is here:
http://www.experts-exchange.com/Database/Oracle/Q_26876555.html

They are trying to load a text file with Oralce's sql loader utility.  This allows you to define field delimiters.  They set a TAB as a field delimiter but some of the text has a tab in it.

Instead of vi, I might go with sed so it can be more easily scripted.
0
 

Author Comment

by:toooki
ID: 35112919
Yes I am trying to remove all "\t" entries in the file...
0
 
LVL 4

Accepted Solution

by:
Alex Matzinger earned 238 total points
ID: 35112935
Oh, then yes, my command will remove every \t in the file...as slightvw said, you can also use sed which would look like this(a new file will be created leaving the original intact):

sed 's/\\t//g' < yourfile > newFile

Open in new window


i prefer using vi or vim, but both will work.
0
 

Author Comment

by:toooki
ID: 35112987
Yes that seems to work. But is there any way to write single command like above to save in the source itself:
sed 's/\\t//g' < myfile > myfile
The above creates empty file.
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35113017
if you have perl on your machine you can use this

perl -e "s/\\t//g" -pi $(find yourFile -type f)

That should do what you are looking for
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35113077
made a minor error in that script it should be single quotes:

perl -e 's/\\t//g' -pi $(find yourFile -type f)

Open in new window

0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35113196
tooki,

Is this related to your sql loader question?

http://www.experts-exchange.com/Database/Oracle/Q_26876555.html
0
 
LVL 6

Assisted Solution

by:Tomunique
Tomunique earned 237 total points
ID: 35115925
You get an empty file because you redirect to it

Command < Input  > Output

The shell prepares the environment to execute  the command, by setting up the file handles, environment variables etc, and THEN executing the program.
In this setup prep, the output is destroyed. (thus, the input is destroyed.)

Try this to "update in place" (not truly update in place, you will get a new inode)

Command < Input > TEMP && mv TEMP Input
 
&& says, execute the next command only if the previous command completed with a return code zero.
&& is a CYA, if Command fails, you save your original, (low on disk space, you don't end up with 1/2  a file -- generally better to not run the cmd, than trunk your file)

0
 

Author Comment

by:toooki
ID: 35117574
Thank you all.
amatzinger, I could not test the Perl command as I could not find any Perl installed on the server.
And slightwv, it is related to the same SQL Loader problem.
Tomunique' sed syntax worked for me. But the TEMP file I created does not seem to get deleted. That I could delete before the main script but It seems this delete of the TEMP file does not work:
# pwd
/usr2/mydir
#ls -ltr TEMP
-rw-r--r--   1 root     root           8 Mar 12 15:49 TEMP
#cat myscript
#!/bin/sh
D_DIR = /usr2/mydir
/bin/rm -rf "${D_DIR}/TEMP" >/dev/null 2>&1
#chmod 755 myscript
#./myscript
./myscript: D_DIR: not found
#

Why cannot the script find/delete the TEMP file?
0
 
LVL 6

Expert Comment

by:Tomunique
ID: 35118926
don't put spaces around the equal sign...
it things D_DIR is a command

D_DIR=/usr2/mydir   all run together
0
 

Author Comment

by:toooki
ID: 35119255
Thank you Tomunique! That worked.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question