LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2013, 07:47 AM   #1
palani.ec
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Rep: Reputation: Disabled
Remove Particular character in file


How i can remove the character.
a/aj/ajay.
a/aj/ajay.ya
a/aj/ajay.yadav
i want to remove a/aj/
pls help.
 
Old 01-09-2013, 08:35 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by palani.ec View Post
How i can remove the character.
a/aj/ajay.
a/aj/ajay.ya
a/aj/ajay.yadav
i want to remove a/aj/
pls help.
Spell out your words, please. And you don't really say much about your requirements (doing this in bash? Perl? Ruby? Python?), but a simple sed command can do it. There are many tutorials you can find on Google, and there is MUCH to learn about sed, but a basic character delete is trivial...bear in mind that you can remove the "/" by escaping it with backslashes, which will make sed treat it as a normal character, rather than as part of the interpreter command.
Code:
cat <filename> | sed 's/a\/aj\///g' > output.filename
Other ways to do it, but you can research those on your own.
 
Old 01-09-2013, 04:42 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Even better, just use a different delimiting character. And we can remove the Useless Use Of Cat.

BTW, if all the strings are at the beginning of the line, as they seem to be, it might also be wise to bind the regex to the start too.

Code:
sed 's|^a/aj/|/|' infile >outfile
Use the -i option (gnu sed) to edit the input file in place, instead of redirecting it to a new file.
 
1 members found this post helpful.
Old 01-09-2013, 09:55 PM   #4
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Hi palani.ec,
Lets assume file1 is having your content,
i.e $cat file1
a/aj/ajay.
a/aj/ajay.ya
a/aj/ajay.yadav

The following perl command will do the required work simply:
Quote:
perl -pi -w -e 's/a\/aj\///g' file1
You can even replace the file1 with the file's path.
Good luck
 
Old 01-10-2013, 06:19 AM   #5
palani.ec
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for reply,

i want to remove before /(special character)
Input is
a/am/amare
b/bb/pals

Output should be:
amare
pals
 
Old 01-10-2013, 08:47 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by palani.ec View Post
Thanks for reply,
i want to remove before /(special character)
Input is
a/am/amare
b/bb/pals

Output should be:
amare
pals
You have changed the requirement from your first post, and you still haven't said how you want to do this (language? ANYTHING?). Again, a sed would be:
Code:
sed -re 's/^.+\///' infile >outfile
Quote:
Originally Posted by David the H.
And we can remove the Useless Use Of Cat.
Hehe...I was leaving that as an exercise for the user.
 
Old 01-10-2013, 11:32 AM   #7
palani.ec
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
i want remove

Hi Friends,
Input is
a/am/amare
b/bb/pals

I want remove
a/am/
and
b/bb/

i need output like
amare
pals
 
Old 01-10-2013, 11:49 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by palani.ec View Post
Hi Friends,
Input is
a/am/amare
b/bb/pals

I want remove
a/am/
and
b/bb/

i need output like
amare
pals
Right..that's what you posted before, several times. You have not added any new information, or answered the questions you were asked. Asking the same question multiple times doesn't get you different answers.

And did you not read/understand post #6, where I told you how to do it???
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a file character by character with a bash builtin command (script). stf92 Linux - Newbie 4 06-30-2012 08:41 PM
[SOLVED] awk: remove the last character in the file cristalp Programming 5 11-03-2011 10:19 AM
[SOLVED] Script to remove lines in a file with more than "x" instances of any character ? pissed_budgie Programming 12 10-08-2010 08:16 PM
Remove last character from file/string linuxchump Programming 34 06-08-2009 04:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration