LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-10-2014, 08:17 AM   #1
vinothini
LQ Newbie
 
Registered: Jan 2011
Posts: 5

Rep: Reputation: 0
Replacing string (Check1, Check2, Check12, Chec123) using sed


Hi,

In one of my File, I have data similar to this.
Service1Check1 Data100
Service1Check2 Data101
Service2Check3 Data103
..
..
Service3Check11 Data104
Service4Check12 Data90
..
...
Service41Check100 Data89
Service42Check101 Data79
Now my requirement is to replace all the Check<Numeral> (check1, Check2,Check123, Check12)
I opened the file using vim and tried the following,

Code:
[ESC]:%s/Check[0-9][0-9][0-9]//g
But it removes only Check followed by three digit numeral. Is there a simple solution using sed or vim commands. please provide your inputs
 
Old 06-10-2014, 08:21 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,105

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
is this what you are after ?:
Code:
[schneidz@hyper ~]$ cat vinothini.txt 
Service1Check1 Data100
Service1Check2 Data101
Service2Check3 Data103
..
..
Service3Check11 Data104
Service4Check12 Data90
..
...
Service41Check100 Data89
Service42Check101 Data79
[schneidz@hyper ~]$ sed s/Check[0-9].*" "//g vinothini.txt 
Service1Data100
Service1Data101
Service2Data103
..
..
Service3Data104
Service4Data90
..
...
Service41Data89
Service42Data79
 
Old 06-10-2014, 08:29 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,522

Rep: Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039
More strictly maybe "[0-9]+" as the requirement was for digits only, and apparently the whitespace is still wanted.
 
Old 06-10-2014, 09:00 AM   #4
vinothini
LQ Newbie
 
Registered: Jan 2011
Posts: 5

Original Poster
Rep: Reputation: 0
It has to remove only the digits after Check.

schneidz,

It removed the words after Check123.

input File:
# cat hello.txt
Service1Check1 Data100
Service1Check2 Data101
Service2Check3 Data103
..
..
Service3Check11 Data104
Service4Check12 Data90
..
...
Service41Check100 Data89
Service42Check101 Data79

Expected Output:
# cat hello.txt
Service1 Data100
Service1 Data101
Service2 Data103
..
..
Service3 Data104
Service4 Data90
..
...
Service41 Data89
Service42 Data79

[0-9]+ Also did not work,
[root@vsx2 DataMigrationscripts]# sed s/Check[0-9]+" "//g hello.txt
Service1Check1 Data100
Service1Check2 Data101
Service2Check3 Data103
..
..
Service3Check11 Data104
Service4Check12 Data90
..
...
Service41Check100 Data89
Service42Check101 Data79
 
Old 06-10-2014, 09:23 AM   #5
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
Have you tried this?
Code:
sed "s/Check[0-9].* / /g" hello.txt
Click image for larger version

Name:	Screenshot.png
Views:	16
Size:	60.8 KB
ID:	15703

Last edited by eklavya; 06-10-2014 at 09:25 AM.
 
Old 06-10-2014, 09:51 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,105

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
this keeps the following space for some reason eventhough i expect it not to:
Code:
[schneidz@hyper ~]$ cat vinothini.txt 
Service1Check1 Data100
Service1Check2 Data101
Service2Check3 Data103
..
..
Service3Check11 Data104
Service4Check12 Data90
..
...
Service41Check100 Data89
Service42Check101 Data79
[schneidz@hyper ~]$ sed s/Check[0-9]*//g vinothini.txt 
Service1 Data100
Service1 Data101
Service2 Data103
..
..
Service3 Data104
Service4 Data90
..
...
Service41 Data89
Service42 Data79
 
1 members found this post helpful.
Old 06-10-2014, 10:01 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Quote:
eventhough i expect it not to
Why not? Your regex says to remove zero or more numbers, it mentions nothing about spaces
Although unquoted in the shell would not have been my choice with an asterisk and the 'g' is superfluous.
 
1 members found this post helpful.
Old 06-11-2014, 12:38 AM   #8
vinothini
LQ Newbie
 
Registered: Jan 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Hi schneidz,

It worked. Thank you for the Solution.

Regards,
Vinothini
 
  


Reply

Tags
sed bash, vim


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Searching/replacing string for string Jalcock501 Programming 13 08-06-2013 02:20 AM
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 3 03-21-2012 01:47 PM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
sed -- replacing a string in a file with the contents of another file? CGP314 Linux - Newbie 10 06-17-2010 04:29 PM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 12:54 AM


All times are GMT -5. The time now is 12:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration