Remove matching pattern on each line with number variations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove matching pattern on each line with number variations
# 1  
Old 02-16-2017
Question Remove matching pattern on each line with number variations

Hello folks!

I have a file containing lines like this
Code:
Something text 18:37Remove This: 1,111"Keep this text"
Some more text 19:37Remove This: 222"Keep this text"
More text 20:50Remove This: 3,333Keep this text
And more text 25:50Remove This: 44,444Keep this text

I would like to replace everything from ( Remove This: 1,111 ) or whatever number pattern follows with a tab space so the output would be:

Code:
Something text 18:37	"Keep this text"
Some more text 19:37	"Keep this text"
More text 20:50	Keep this text
And more text 25:50	Keep this text

Code:
Something text 18:37[TABSPACE]"Keep this text"
Some more text 19:37[TABSPACE]"Keep this text"
More text 20:50[TABSPACE]Keep this text
And more text 25:50[TABSPACE]Keep this text

Thank you for your help!

Last edited by Scrutinizer; 02-16-2017 at 03:14 AM.. Reason: quote tags -> code tags
# 2  
Old 02-16-2017
Hello martinsmith,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 02-17-2017
Facebook

Hi Robin,

Thank you!

It's not a homework assignment. I'm just trying to clean up some data which i had extracted.

Basically i tried some commands with sed and awk but could not figure this out. I'm not a programmer so i don't know much about working my way around this Smilie I thought maybe someone here might have a solution for this hopefully.

Basically what i'm trying to accomplish is the following.

I have a data file which is about 40K lines long so it would take me forever to clean it up manually.

In my file i have data structured like this:
Code:
Random text 18:37Same text: 1,111"Some random text"
More random text 19:37Same text: 222"Some more random text"

After "Same text:" the numbers are randomized from 1 to 50,000.

I would like to replace everything from "Same text: RANDOMIZED NUMBER" with a tab space so the output can be:

Code:
Random text 18:37	"Some random text"
More random text 19:37	"Some more random text"

I'd appreciate any help for this if there is a way.

Thanks so much again
# 4  
Old 02-17-2017
With GNU sed, try:
Code:
sed 's/Remove This: [0-9,]*/\t/' file

with regular sed you need to use a real TAB character, which can be entered with CTRL-V TAB
Code:
sed 's/Remove This: [0-9,]*/    /' file

its any awk try:
Code:
awk '{sub(/Remove This: [0-9,]*/, "\t")}1'  file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-17-2017
Quote:
Originally Posted by Scrutinizer
With GNU sed, try:
Code:
sed 's/Remove This: [0-9,]*/\t/' file

with regular sed you need to use a real TAB character, which can be entered with CTRL-V TAB
Code:
sed 's/Remove This: [0-9,]*/    /' file

its any awk try:
Code:
awk '{sub(/Remove This: [0-9,]*/, "\t")}1'  file

Thank you Scrutinizer!

All of your solutions worked perfectly and solved the problem.

Many Thanks for your help! It is so much appreciated Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetching a line matching a pattern

Hi Gurus, I have a file as follows (Sample shown below but the list is very huge) SCHEDULE WS1#JS1 RUNCYCLE1 : WS1#JOB1 WS1#JOB2 FOLLOWS JOB1 END SCHEDULE WS2#JS1 RUNCYCLE2 : WS1#JOB3 WS1#JOB1 FOLLOWS JOB3 WS2#JOB1 (10 Replies)
Discussion started by: jayadanabalan
10 Replies

2. Shell Programming and Scripting

Removing spaces from line matching a pattern

Hi, I want to remove the spaces from all the lines matching a particular pattern from my file. For instance in file abc.txt I have following data. Header,This is the header 111,this is 1st record 222, this is 2nd record 333, this is 3rd record Footer,3 records found Footer,111222333 ... (5 Replies)
Discussion started by: decci_7
5 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

sed Command new Line not working Tried many variations

Hi I have assigned an output of a command to $I. I try to print the input and put a new line after occurrence of the hostname which is assigned to $HOST1 ( Example: pwrm16 ) . First of all I need to get rid of the Colon after the host name pwrm16: and make it pwrm16 then I want to print the... (10 Replies)
Discussion started by: mnassiri
10 Replies

5. Shell Programming and Scripting

Matching pattern in two files and print the line

Hi, I want to match the pattern in file1 with file2 and print the value in file2 and paste in file1 file1: ISHO RT SR Major 96.46778 Drop Call Rate CS Critical 0.5072662 ISHO RT SR Major 97.754364... (3 Replies)
Discussion started by: khalil
3 Replies

6. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

7. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

8. Shell Programming and Scripting

matching pattern to end of line

I have the following code and want to get only the comments. Ideally I would like to replace the characters to the left of the first '!' in the line with blanks. real, dimension(:), allocatable :: ft ! stores a single trace real, dimension(:), allocatable :: tr ! stores a single... (8 Replies)
Discussion started by: kristinu
8 Replies

9. Shell Programming and Scripting

Creating number by pattern matching

I have a certain mnemonic string from which I want to calculate a number The pattern follows three letters s, v and d. If a letter is by its own, the number assigned to the letter is assumed to be one. Else it takes the value preceeding it. I then need to add the numbers together. Example ... (5 Replies)
Discussion started by: kristinu
5 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question