Replace/Remove not specific text in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace/Remove not specific text in perl
# 1  
Old 03-23-2011
Replace/Remove not specific text in perl

Hello,

Consider that i have many files that have the below format:

file1
Code:
900    7777
1000   5   6   23     nnnnnnnnnnnnnnnnnn
1100 kkkkkkk

file2
Code:
900    1989
1000   5   3   10  kkkdfdfdffd
1100 kkkkkkk


What i would like to do is on every file to search the line that starts with '1000' and remove everything that exist after the 3rd token e.g for file1 remove everything that exists after '23', and for file2 remove everything that exist after '10'

I know how to search for the line that starts with '1000' but i do not know how to remove the string that exist after the third token.

Best Regards,
Christos
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 03-23-2011 at 10:08 AM.. Reason: code tags, please!
# 2  
Old 03-23-2011
Try:
Code:
perl -pe 's/(^1000(\s+\d+){3}).*/\1/' file

# 3  
Old 03-23-2011
Code:
nawk '/^1000 /{NF=4;$1=$1}1' infile

Code:
nawk '/^1000 /{sub($4".*",$4,$0)}1' infile


Last edited by ctsgnb; 03-23-2011 at 10:49 AM..
# 4  
Old 03-23-2011
You could use a buffer, chomp until you find the line with the 1000, then delimit by spaces or tabs (whatever the character is):
Code:
while($buffer = <MYFILE>)
{
  chomp($buffer);
  if('1000' eq (substr $buffer,0,4))
  {
     my @array = split /\s/,$buffer;
     print($array[0] . " " . $array[1] . " " . $array[2] . "\n";
  }
}

# 5  
Old 03-24-2011
Code:
perl -pe 's/^1000(\s+\d+){3}\K.*//' your_file

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to replace and remove few junk characters from a specific field?

I would like to remove all characters starting with "%" and ending with ")" in the 4th field - please help!! 1412007819.864 /device/services/heartbeatxx 204 0.547%!i(int=0) 0.434 0.112 1412007819.866 /device/services/heartbeatxx 204 0.547%!i(int=1) 0.423 0.123... (10 Replies)
Discussion started by: snemuk14
10 Replies

2. Shell Programming and Scripting

find from file and replace with specific text

Dear All, I do not have any knowledge of scripting. I want to replace specific lines of a text file with a specific text. Like I have one file which is "original file" and one file "changes file" which has list of lines which I want to replace in original file with a specific string. I want the... (5 Replies)
Discussion started by: libras
5 Replies

3. Shell Programming and Scripting

Help with remove last text of a file that have specific pattern

Input file matrix-remodelling_associated_8_ aurora_interacting_1_ L20 von_factor_A_domain_1 ATP_containing_3B_ . . Output file matrix-remodelling_associated_8 aurora_interacting_1 L20 von_factor_A_domain_1 ATP_containing_3B . . (3 Replies)
Discussion started by: perl_beginner
3 Replies

4. Shell Programming and Scripting

Replace text in SPECIFIC multiple files

I have a list of files with different file names and ext that i need replace(saved as file_list.txt.) i just want to replace from a specific file list. i obtain an error saying lint not found. Plz help. Thx perl -e "s/$NAME/$T_NAME/gi;" -pi $(find . -type f | xargs -0 lint -e < file_list.txt) (0 Replies)
Discussion started by: yvmy
0 Replies

5. Shell Programming and Scripting

Perl use split and remove specific character

i want to split the input by a space and remove specific characters like full stop, comma...... etc. and then save each word in an array. i got something below, but it didn't work. can anyone please help me? Thank you #!/usr/bin/perl -w while (<>) { $line = <>; @word = split(' ',... (6 Replies)
Discussion started by: mingming88
6 Replies

6. Shell Programming and Scripting

remove and replace text in a file

Hello all, How would I go to a particular line in a file and remove certain text from it and replace with something that I want it to be there. like: file /etc/abc now look for line HOME="/export/xyz" in /etc/abc and then replace with HOME=/"export/xyz1" thanks in advance guys. (1 Reply)
Discussion started by: solaix14
1 Replies

7. UNIX for Dummies Questions & Answers

how to use vi to replace specific line of text only

hi all, i am new bee to Unix. i know how to replace text for range of lines in vi for e.g. replace | with |||| for line 4 through 7 using vi : 4,7s/|/||||/g but i have to replace | with |||| only for line no 4, 7 and 10 using vi only!!! your help will be appreciated! thanks,... (4 Replies)
Discussion started by: pranav.pandya
4 Replies

8. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

9. Shell Programming and Scripting

How to remove the specific lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some "seed" information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. Please provide the script code... (4 Replies)
Discussion started by: dipakg
4 Replies

10. Shell Programming and Scripting

remove specific lines from flat file using perl

Hi, Here is wat im looking for.. i have a flat file which looks like this.. 00 * * * * .. .. * * text text text COL1 COL2 ----- ----- 1 a (12 Replies)
Discussion started by: meghana
12 Replies
Login or Register to Ask a Question