How to remove everything after a word containing string?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 01-25-2019
How to remove everything after a word containing string?

Hello,
I wish to remove any word coming after searched string found in a word.

source*.txt
Code:
#!bin/bash
#test1
http://www.aa.bb.cc http://www.xx.yy http://www.11.22.44
#test2
http://www.11.rr.cd http://www.01.yy http://www.yy.22.tt
#test3
http://www.22.qq.fc http://www.0x.yy http://www.t1.22.pk


readfile
Code:
aa.bb
11.rr.cd
qq.fc

Expected output:

Code:
#!bin/bash
#test1
http://www.aa.bb.cc
#test2
http://www.11.rr.cd
#test3
http://www.22.qq.fc

I only know how to delete anything coming after a string like below way:

Code:
while read COL1
do
sed 's/$COL1.*//' source
done <readfile

I'd appreciate if you could help me

Thank You
Boris

Last edited by baris35; 01-25-2019 at 01:49 PM..
# 2  
Old 01-25-2019
I remember telling you a few hours ago that variable expansion doesn't work within single quotes, so $COL1 will never be expanded. On top, even IF $COL1 were expanded, there still is a logical error in your sed command: it would remove $COL1 together with the rest of the line. And, that sed command is executed on the entire source file for every single line in readfile.


I think a better approach is awk. Try


Code:
awk 'NR==FNR {T[$1]; next} {for (t in T) if ($1 ~ t) $0 = $1} 1' file2 file1
#!bin/bash
#test1
http://www.aa.bb.cc
#test2
http://www.11.rr.cd
#test3
http://www.22.qq.fc


Last edited by RudiC; 01-25-2019 at 01:36 PM..
This User Gave Thanks to RudiC For This Post:
nezabudka (01-25-2019)
# 3  
Old 01-25-2019
Just in case the matches are not in the first field.
Code:
awk 'NR==FNR {T[$0]; next} {for(i=1;i<=NF;i++){for(t in T) if ($i ~ t) $0=$i}} 1' readfile source

This User Gave Thanks to nezabudka For This Post:
RudiC (01-25-2019)
# 4  
Old 01-25-2019
I am sorry, as I have multiple files to be processed, I tried like this but could not get the output:

Code:
for file in source*.txt
do
awk 'NR==FNR {T[$0]; next} {for(i=1;i<=NF;i++){for(t in T){if ($i ~ t) $0=$i}}} 1' readfile $file \
> report_$(basename "${file/.txt}").txt
done

Thank you
Boris
# 5  
Old 01-25-2019
With 250 posts in these fora, you may have learned that without detailed error information, analysis and debugging is close to impossible. So please provide:
- Any error messages?

- Are the output files created?
- What be their contents?
- Did you run the script with the -x (xtrace) option set?
# 6  
Old 01-25-2019
@nezabudka: good approach, but you'll lose the leading fields if the pattern is found in later fields... does this come closer?
Code:
awk 'NR==FNR {T[$1]; next} {for (t in T) if ($0 ~ t) sub (t".*", t)} 1' file2 file1

EDIT: Indeed, you can drop the if:
Code:
awk 'NR==FNR {T[$1]; next} {for (t in T) sub (t".*", t)} 1' file2 file1


Last edited by RudiC; 01-25-2019 at 02:33 PM..
This User Gave Thanks to RudiC For This Post:
nezabudka (01-25-2019)
# 7  
Old 01-25-2019
Boris
Why do you delete the file extension and immediately add it?
Code:
> report_$(basename "${file/.txt}").txt

And why do you run it in a cycle, only you lose time. Try it first
Code:
awk 'NR==FNR {T[$0]; next} {for(i=1;i<=NF;i++){for(t in T) if ($i ~ t) $0=$i}} 1' readfile source*.txt

To localize the error, try adding a filter, for example
Code:
awk 'NR==FNR {T[$0]; next} NF > 1 && NF < 4 {for(i=1;i<=NF;i++){for(t in T) if ($i ~ t) $0=$i}} 1' readfile source*.txt


Last edited by nezabudka; 01-25-2019 at 02:37 PM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Please remove the word from posts ptappeta Post Here to Contact Site Administrators and Moderators 4 11-06-2015 12:01 AM
Remove string perl with first or last word is in a list cyrine Shell Programming and Scripting 2 01-10-2015 12:25 PM
Remove word before a character wahi80 Shell Programming and Scripting 2 10-13-2014 01:30 PM
Remove not only the duplicate string but also the keyword of the string in Perl askari Shell Programming and Scripting 2 03-12-2014 08:09 PM
Remove word with sed aydj Shell Programming and Scripting 5 03-11-2014 07:43 AM
Remove last word of a string? sea Shell Programming and Scripting 1 02-24-2014 10:51 AM
How to remove first word? pokhraj_d Shell Programming and Scripting 6 12-25-2013 02:14 AM
Remove 1st word and _ from string vedanta Shell Programming and Scripting 1 05-21-2013 09:16 AM
sed command to remove a word from string anand.shah Shell Programming and Scripting 7 12-11-2012 07:25 AM
want to remove last word. javeedkaleem Shell Programming and Scripting 7 09-22-2011 04:40 AM
Replace a word in a string starting with another word mukeshbaranwal Shell Programming and Scripting 2 07-26-2011 03:01 AM
grep part of word or Another word from a string linuxadmin Shell Programming and Scripting 2 05-30-2011 01:25 AM
remove characters from string based on occurrence of a string victor369 Shell Programming and Scripting 5 02-03-2011 08:37 PM
Remove particular word from file darshakraut Shell Programming and Scripting 4 09-10-2009 08:45 AM
how to remove first word malaysoul Shell Programming and Scripting 1 05-29-2008 03:33 AM