LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2012, 10:58 AM   #1
andycol500
LQ Newbie
 
Registered: Mar 2012
Posts: 15

Rep: Reputation: Disabled
delete all text before the /


Hi Guys

So I have a text file with over 25000 lines similar to this.

BRI005/0116253000
sedi's/CorporateServicesadivisionofBridgeFax/0116253090

And I need to change it so it comes at as follows
sed -i 's/0116253000/CorporateServicesadivisionoofBridge/g' /tmp/*.txt

Is that possible with sed or anything?

Thanks
 
Old 10-23-2012, 11:26 AM   #2
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 andycol500 View Post
Is that possible with sed or anything?
Help us to help you. Give us a sample input file (at least 10-12 lines). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

It is almost certain that sed or grep will do the job.

Daniel B. Martin
 
Old 10-23-2012, 11:29 AM   #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
What? I think I perhaps understand the question, but the examples you posted are confusing me. Except for the first line they look like they are already sed commands (at least sorta). Could you please explain more clearly?

And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


A basic sed command that would remove everything up to the first slash would look like this:

Code:
sed -i 's_[^/]*/_/_' infile
Notice that you can use any basic ascii character as the s delimiter, so just choose one that's not found in the expression itself. I used "_" in the above.
 
Old 10-23-2012, 11:32 AM   #4
andycol500
LQ Newbie
 
Registered: Mar 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
here is how it looks currently

BEV/010/0114441030
BEV/02/0114404855
BHE0/01/0114945/020
BIB0/01118/022955
BIG003/0118/020560
sedi's/BigenAfricaFax/0118/020565
BIL003/0116562/024
BIZ0/01/0114472757
ATTENTION/0117040/026NormalTerms2
BLA004/0116568044
BLI0/01112622299
BLU003/011/0218216
BLU009/0118353807
BMG0/01/0117935400
BOA121/0117927663
sedi's/BoardroomSolutions30dayfrmInvFax/0117936209
BOL0/01/0119827671
sedi's/BolelaVoiceanddataCablingFax/0119829700
BOL003/0118947748
BOR0/01/0118043252
BOT005/0117912529/30
sedi's/BotarProcurement&LogisticsccFax/0117928227
BSC0/02/0114634779



I need it to look like

sed -i's/0114404855/BigenAfrica/g' /tmp/*.txt

sed -i 's/0114472757/ATTENTION/g' /tmp/*.txt
sed -i 's/0117927663/BoardroomSolutions/g' /tmp/*.txt
sed -i 's/0117912529/BotarProcurement&Logisticscc/g' /tmp/*.txt
 
Old 10-23-2012, 11:59 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
I need it to look like

sed -i's/0114404855/BigenAfrica/g' /tmp/*.txt

sed -i 's/0114472757/ATTENTION/g' /tmp/*.txt
sed -i 's/0117927663/BoardroomSolutions/g' /tmp/*.txt
sed -i 's/0117912529/BotarProcurement&Logisticscc/g' /tmp/*.txt
This is not what you need it to look like. This is a list of sed commands, which may or may not produce the desired output.

So firstly, please use [code][/code] tags around your code and data.

Secondly, show the actual desired output required.
 
Old 10-23-2012, 12:09 PM   #6
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
How did you get from here...
Code:
BEV/010/0114441030
BEV/02/0114404855
BHE0/01/0114945/020
BIB0/01118/022955
BIG003/0118/020560
sedi's/BigenAfricaFax/0118/020565
... to here?
Code:
sed -i's/0114404855/BigenAfrica/g' /tmp/*.txt
What algorithm guided you to choose 0114404855 from the five lines preceding BigenAfrica?

Daniel B. Martin
 
Old 10-23-2012, 12:24 PM   #7
andycol500
LQ Newbie
 
Registered: Mar 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Ok maybe let me explain a bit better.

So I have an excel spreadsheet with over 45000 lines and it has a lot of junk data.
I need to filter out all the data except company name and no
then I need that to be put into a sed command.
I want to copy all the sed commands into a bash script that will convert a text file on a weekly basis
an eg of the script is below

friday=`date +%F`
monday=`date +%F --date="+4 days ago"`
SUBJECT="Weekly calls report"
EMAIL="xxx@xxx.co.za"
CC="xxx@xxx.co.za"
ATTACHMENT="/tmp/*.txt"
BODY="/home/body.txt"

mysql xxxx -u root -pxxxx<<EOFMYSQL
select calldate as 'DATE',src as 'DIALED-NO',dst as 'DESTINATION', (billsec /* = an int */ / 60) as 'DURATION(seconds)' from cdr where calldate > '$monday 00:00:00' and calldate < '$friday 23:59:00' and src ='30' and dst LIKE '0%' and disposition ='ANSWERED' INTO OUTFILE '/tmp/cedwin.txt'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'LINES TERMINATED BY '\n';
EOFMYSQL
sleep 5
mysql xxxx -u root -pxxxx<<EOFMYSQL
select calldate as 'DATE',src as 'DIALED-NO',dst as 'DESTINATION', (billsec /* = an int */ / 60) as 'DURATION(seconds)' from cdr where calldate > '$monday 00:00:00' and calldate < '$friday 23:59:00' and src ='22' and dst LIKE '0%' and disposition ='ANSWERED' INTO OUTFILE '/tmp/megan.txt'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'LINES TERMINATED BY '\n';
EOFMYSQL
sleep 5
mysql xxxx -u root -pxxxx<<EOFMYSQL
select calldate as 'DATE',src as 'DIALED-NO',dst as 'DESTINATION', (billsec /* = an int */ / 60) as 'DURATION(seconds)' from cdr where calldate > '$monday 00:00:00' and calldate < '$friday 23:59:00' and src ='14' and dst LIKE '0%' and disposition ='ANSWERED' INTO OUTFILE '/tmp/olwethu.txt'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'LINES TERMINATED BY '\n';
EOFMYSQL
sleep 5
mysql xxx -u root -pxxxx<<EOFMYSQL
select calldate as 'DATE',src as 'DIALED-NO',dst as 'DESTINATION', (billsec /* = an int */ / 60) as 'DURATION(seconds)' from cdr where calldate > '$monday 00:00:00' and calldate < '$friday 23:59:00' and src ='23' and dst LIKE '0%' and disposition ='ANSWERED' INTO OUTFILE '/tmp/jean.txt'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'LINES TERMINATED BY '\n';
EOFMYSQL
sleep 5
mysql xxxxx -u root -pxxxxx<<EOFMYSQL
select calldate as 'DATE',src as 'DIALED-NO',dst as 'DESTINATION', (billsec /* = an int */ / 60) as 'DURATION(seconds)' from cdr where calldate > '$monday 00:00:00' and calldate < '$friday 23:59:00' and src ='19' and dst LIKE '0%' and disposition ='ANSWERED' INTO OUTFILE '/tmp/hayley.txt'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'LINES TERMINATED BY '\n';
EOFMYSQL
sleep 5

sed -i 's/074318520/Dorcas/g' /tmp/*.txt
sed -i 's/0712472533/Hayley/g' /tmp/*.txt
sed -i 's/0724021236/Isaac-SNR/g' /tmp/*.txt
sed -i 's/0724021236/Isaac-SNR/g' /tmp/*.txt
sed -i 's/0730092023/Isaac-JNR/g' /tmp/*.txt
sed -i 's/0730448205/Jaysen/g' /tmp/*.txt
sed -i 's/0824874059/Jean/g' /tmp/*.txt
sed -i 's/0827796158/Jerry/g' /tmp/*.txt
sed -i 's/0743115570/Jimmy/g' /tmp/*.txt
sed -i 's/0844541646/Jono/g' /tmp/*.txt
sed -i 's/0823918640/Judy/g' /tmp/*.txt
sed -i 's/0834585171/Lita/g' /tmp/*.txt
sed -i 's/0726912960/Megan/g' /tmp/*.txt
sed -i 's/0768511320/Sharon/g' /tmp/*.txt
sed -i 's/0836687892/Solomon/g' /tmp/*.txt
sed -i 's/0724340725/Sune/g' /tmp/*.txt
sed -i 's/0782673693/Tiny/g' /tmp/*.txt


sed -i 's/0114833051/10-2nd-Avenue-Houghton-Estate-(Pty)-Ltd/g' /tmp/*.txt
sed -i 's/0116168348/123-Montessori/g' /tmp/*.txt
sed -i 's/0114401561/180-DEGREES-PTY-LTD/g' /tmp/*.txt
sed -i 's/0116567015/20-20-Insight/g' /tmp/*.txt
sed -i 's/0114778718/321go-Travel-Consultants-CC/g' /tmp/*.txt
sed -i 's/0114544439/7th-Element-Production/g' /tmp/*.txt
sed -i 's/0113057242/@home/g' /tmp/*.txt
sed -i 's/0123261978/A-&-A-Creations/g' /tmp/*.txt
sed -i 's/0114554555/AA-Diamonds/g' /tmp/*.txt
sed -i 's/0834694430/Abdul/g' /tmp/*.txt
sed -i 's/0118131890/aBEErate-Verification-Agency/g' /tmp/*.txt
sed -i 's/0119791885/Aboard-Refrigeration/g' /tmp/*.txt
sed -i 's/0114543130/Above-&-Beyond-Concepts/g' /tmp/*.txt
sed -i 's/0128038348/ABSOLUTE-DOORS/g' /tmp/*.txt
sed -i 's/0114441019/Abu-Daya/g' /tmp/*.txt
sed -i 's/0116800368/A Calisto-Portuguese-Restuarant/g' /tmp/*.txt
sed -i 's/0119555200/ACAirConsupport/g' /tmp/*.txt
sed -i 's/0117873571/ACCVANTAGESOLUTIONS/g' /tmp/*.txt
sed -i 's/0110242178/ACRClothingManufacturers/g' /tmp/*.txt
sed -i 's/0126536032/ActiveMotors/g' /tmp/*.txt

an EG of the xls file


Acc BRI005 Telephone: /0116253000
sed-i's/ CorporateServicesadivisionofBridge Fax: /0116253090
DeliveryAddress PostalAddress Contact: Feranah
1BridgeClose MobilePhone:
CityDeep Email:
Ext19 Type: O/I
Category: 1
NormalTerms: 2

Last edited by andycol500; 10-23-2012 at 12:25 PM.
 
Old 10-23-2012, 01:11 PM   #8
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
For the third time this thread...

*** USE CODE TAGS!! ***

http://www.linuxquestions.org/questi...do=bbcode#code

I'm not about to go parsing all of that to understand what you want at this point.

But I will point out that you don't need a bunch of individual commands. All you need is a file that contains the expressions, one per line. Then you can run it all in a single command using the -f option.

sedfile.txt
Code:
s/074318520/Dorcas/g
s/0712472533/Hayley/g
s/0724021236/Isaac-SNR/g
s/0730092023/Isaac-JNR/g
...etc...
Code:
sed -i -f sedfile.txt /tmp/*.txt
 
  


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
Delete line of text from text file via shell? zizou86 Programming 3 01-13-2010 11:25 AM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 09:40 PM
delete all files with certain text dougp23 Linux - General 3 04-21-2008 01:17 PM
Delete ^O from a text file pwc101 Programming 3 12-05-2007 11:02 AM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 01:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:17 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