LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-10-2014, 09:47 AM   #1
CSVvenu
LQ Newbie
 
Registered: Feb 2014
Posts: 5

Rep: Reputation: Disabled
Remove a word after a specified word


Hi All,

I have a file with words like this.

M0 FFB OBRSS 7225 w=1u l=1u
M1 FFC OBRSS 9253 w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
...etc

I want to remove number after OBRSS. i.e

M0 FFB OBRSS w=1u l=1u
M1 FFC OBRSS w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
..etc

Could you please help me?

Thanks,
Venu.
 
Old 02-10-2014, 09:47 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried and where are you stuck ?

awk and sed seem like likely solutions.

Last edited by schneidz; 02-10-2014 at 09:49 AM.
 
2 members found this post helpful.
Old 02-10-2014, 09:52 AM   #3
CSVvenu
LQ Newbie
 
Registered: Feb 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
I don't know much about linux. I just searched in web but haven't found any query related to this. So I just raised query in this forum.
 
Old 02-10-2014, 10:00 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i think you would want to reserch the sed command and its substitute functionality (especially how it operates with regular expressions).
 
2 members found this post helpful.
Old 02-11-2014, 10:36 PM   #5
CSVvenu
LQ Newbie
 
Registered: Feb 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
I tried sed command. But haven't get it. Please let me if any one knows how to get it..
 
Old 02-12-2014, 12:13 AM   #6
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 275Reputation: 275Reputation: 275
Quote:
Originally Posted by CSVvenu View Post
I tried sed command. But haven't get it. Please let me if any one knows how to get it..
Could you post what you tried?
Anyway if you search into this forum, you will find several posts about your same issue.
 
2 members found this post helpful.
Old 02-12-2014, 12:43 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
here is a tip:
Code:
sed 's         # stands for substitute
/OBRSS digits  # look for the given string and the numbers
/OBRSS         # replace it with OBRSS
/g             # flags if required
' inputfile > outfile
see man sed to complete it
 
3 members found this post helpful.
Old 02-12-2014, 07:15 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by CSVvenu View Post
Hi All,

I have a file with words like this.

M0 FFB OBRSS 7225 w=1u l=1u
M1 FFC OBRSS 9253 w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
...etc

I want to remove number after OBRSS. i.e

M0 FFB OBRSS w=1u l=1u
M1 FFC OBRSS w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
..etc

Could you please help me?

Thanks,
Venu.
If OBRSS does appear in a line, will it always be the third word?

Daniel B. Martin
 
Old 02-12-2014, 11:23 PM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
M0 FFB OBRSS 7225 w=1u l=1u
M1 FFC OBRSS 9253 w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
... this awk ...
Code:
awk '{for (j=1;j<=NF-1;j++) {if ($j=="OBRSS") $(j+1)=""}; print}' $InFile >$OutFile
... produced this OutFile ...
Code:
M0 FFB OBRSS  w=1u l=1u
M1 FFC OBRSS  w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
Note the (possibly insignificant) flaw: a double blank after OBRSS.

Daniel B. Martin
 
Old 02-12-2014, 11:28 PM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
M0 FFB OBRSS 7225 w=1u l=1u
M1 FFC OBRSS 9253 w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
... this awk ...
Code:
awk '{p1=index($0,"OBRSS ")+5;
      if (p1>5) 
        {str1=substr($0,1,p1-1)
         str3=substr($0,p1+index(substr($0,p1+1)," "))
         $0=str1 str3}
      print}' $InFile >$OutFile
... produced this OutFile ...
Code:
M0 FFB OBRSS w=1u l=1u
M1 FFC OBRSS w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
Daniel B. Martin
 
1 members found this post helpful.
Old 02-14-2014, 06:55 AM   #11
CSVvenu
LQ Newbie
 
Registered: Feb 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks very much Daniel B. Martin, Your code is working. And thanks kooru,pan and schneidz for your useful suggitions.
 
1 members found this post helpful.
Old 02-14-2014, 08:33 AM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by schneidz View Post
what have you tried and where are you stuck ?

awk and sed seem like likely solutions.
i was waiting for a little effort from the op but this is what i came up with:
Code:
[schneidz@hyper ~]$ cat csvvenu.txt 
M0 FFB OBRSS 7225 w=1u l=1u
M1 FFC OBRSS 9253 w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
[schneidz@hyper ~]$ sed s/"OBRSS [0-9]* "/"OBRSS "/g csvvenu.txt 
M0 FFB OBRSS w=1u l=1u
M1 FFC OBRSS w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
 
1 members found this post helpful.
Old 02-14-2014, 09:26 AM   #13
CSVvenu
LQ Newbie
 
Registered: Feb 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks schneidz, I followed your hint and then I was able to replace OBRSS but not numbers. Now I understand how to replace it. Previously if I spent much time on this then I can able to get this. But i don't have much time because I am working on a project which is tight scheduled. Sorry for this and thanks very much for your hint and answer.
 
Old 02-15-2014, 01:20 PM   #14
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by schneidz View Post
Code:
 sed s/"OBRSS [0-9]* "/"OBRSS "/g csvvenu.txt
Now, just for fun, let's generalize this problem. Instead of saying we want to delete the word following OBRSS, let's say we want to delete the word following any word contained in a second input file.

With this InFile1 ...
Code:
M0 FFB OBRSS 7225 w=1u l=1u
M1 FFC OBRSS 9253 w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
M2 FFB OBRSS 8336 w=1u l=1u
M3 FFC OBRXX 5454 w=1u l=1u
M4 FFC MOSFP 42424 w=1u l=1u
... and this InFile2 ...
Code:
OBRSS
OBRXX
OBRYY
... this code ...
Code:
sed 's|\(.*\)|s/\1 [0-9]* /\1 /g|' $InFile2  \
|sed -f - $InFile1 >$OutFile
... produced this OutFile ...
Code:
M0 FFB OBRSS w=1u l=1u
M1 FFC OBRSS w=1u l=1u
M1 FFC MOSFP 92365 w=1u l=1u
M2 FFB OBRSS w=1u l=1u
M3 FFC OBRXX w=1u l=1u
M4 FFC MOSFP 42424 w=1u l=1u
Daniel B. Martin
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux Script required for word by word comaprison from 2 carat separated files. rajeevdvedi2006 Linux - Newbie 1 05-29-2013 06:24 AM
[SOLVED] Find/Replace shell script that replaces word with other word in text, filenames yanom Programming 8 09-12-2012 12:29 AM
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 01:19 PM
How can i read two files word by word at a time using any loop by shell script? vaibhavs17 Programming 16 03-19-2010 03:48 AM
Problems Copying & Pasting In Word When Word Closes - Ubuntu davidx Linux - Software 3 10-22-2008 08:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:31 AM.

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