Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


How to remove everything after a word containing string?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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 02:49 PM..
# 2  
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 02:36 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
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:
# 4  
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  
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  
@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 03:33 PM..
This User Gave Thanks to RudiC For This Post:
# 7  
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 03:37 PM..
Login or Register to Reply

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

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Please remove the word from posts

Hi Sir, Need your help in removing the following words from the posts . These are confidential info posted by mistake and our organization is doing an audit on this. Could you kindly remove as per below. https://www.unix.com/shell-programming-and-scripting/197017-perl-help.html ... (4 Replies)
Discussion started by: ptappeta
4 Replies

2. Shell Programming and Scripting

Remove string perl with first or last word is in a list

Hello, I try to delete all strings if their first or last word is one of this list of words : "the", "i", "in", "there", "this", "with", "on", "we", "that", "of" For example if i have this string in an input file "with me" this string will be removed, Example: input "the european... (2 Replies)
Discussion started by: cyrine
2 Replies

3. Shell Programming and Scripting

Remove last word of a string?

Hello I have a string that may or may not have 4 dots. The string is actualy a file within a folder, where multiple files are located. Files may look like: # ls * creds: 192.168.10.110 someserver shares: 192.168.10.110.Public someserver.sharefolder # I want to fill 2 variables,... (1 Reply)
Discussion started by: sea
1 Replies

4. Shell Programming and Scripting

How to remove first word?

Hi All, I want to remove the first word "cn=" from the below details. Only I want the number like 171345,174144... cn=171345 cn=174144 How can I achieve this. Please suggest. Thanks- P (6 Replies)
Discussion started by: pokhraj_d
6 Replies

5. Shell Programming and Scripting

Remove 1st word and _ from string

var=abc_cde_def_ghi_678.txt Expected output: cde_def_ghi_678.txt Is there a better way to achive this other than cut command? Basically, I need to remove the 1st word and _ from the string. Thanks. (1 Reply)
Discussion started by: vedanta
1 Replies

6. Shell Programming and Scripting

sed command to remove a word from string

Hello All, I am running a command find . -name amp.cfg | cut -c 3- which gives me output something like below rel/prod/amp.cfg rel/fld/amp.cfg deb/detail/amp.cfg deb/err/amp.cfg I want to remove trailing "/amp.cfg" so that i should get output something like... (7 Replies)
Discussion started by: anand.shah
7 Replies

7. Shell Programming and Scripting

want to remove last word.

Hi, I have a file which has the following /u12/data/oracle/abc.dbf /u12/data/oracle/def.dbf /u12/data/oracle/daf.dbf /u12/data/oracledb/fgh.dbf /u12/data/oracledb/fkh.dbf /u12/data/oracledb/kdq.dbf I want to do something like this /u12/data/oracle /u12/data/oracle... (7 Replies)
Discussion started by: javeedkaleem
7 Replies

8. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

9. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

10. Shell Programming and Scripting

how to remove first word

Hi, i have a question about to remove first word from a sentence. my script; #!/usr/bin/perl $msgtxt = "this is a test script"; my @ap_txtMsg = split(/ +/, trim_data($msgtxt)); $ap_msgtxt = splice (@ap_txtMsg, 0, 1); print $ap_msgtxt; but the output is first word that i... (1 Reply)
Discussion started by: malaysoul
1 Replies

Featured Tech Videos