The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing last character from each line of file cjhancock Shell Programming and Scripting 17 08-29-2008 01:05 AM
removing particular lines ending with a .cnt extension in a text file ramky79 SUN Solaris 2 03-03-2008 01:17 AM
Removing a particular line from a text file sendhilmani123 Shell Programming and Scripting 5 05-31-2006 05:32 AM
Removing lines in a text file. WABonnett Shell Programming and Scripting 4 11-25-2003 07:27 AM
removing commas from text file hcclnoodles UNIX for Dummies Questions & Answers 6 03-26-2003 12:43 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 9
Stumble this Post!
Exclamation Removing text from a line in a file

Hi All,

I would like to know how to remove text from a line in a file.

eg

[na *oparea* check 0 nul 6376000 nlr 6374000 eul 318000 elr 320000]

to

[na *oparea* check 0]

The 4 sets of numbers are not static ie they change on each line in each different file so if anyone can help that would be great.

Jeremy
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 18
Stumble this Post!
awk '{print $1,$2,$3,$4"]"}' inputfilename > outputfilename

Last edited by H2OBoodle; 02-04-2008 at 08:36 PM. Reason: Removed the "," after the 4 to avoid the space being added.
Reply With Quote
  #3 (permalink)  
Old 02-04-2008
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 767
Stumble this Post!
try this Perl one liner:
Code:
perl -pi -e 's/(\[na\s+\*oparea\*\s+check\s+\d+)\s+nul\s+\d+\s+nlr\s+\d+\s+eul\s+\d+\s+elr\s+\d+\]/\1]/' newfile
Reply With Quote
  #4 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 306
Stumble this Post!
Hopefully the entire line does not have to matched and a shorter regexp will suffice:

perl -pi -e 's/^(\Q[na *oparea* check \E\d+).*/$1]/' file

The only advantage is a more readable code. The disadvantage is false matches are possible if the entire line needs to be matched
Reply With Quote
  #5 (permalink)  
Old 02-12-2008
Registered User
 

Join Date: Jan 2008
Posts: 9
Stumble this Post!
removing text

perl -pi -e 's/^(\Q[na *oparea* check \E\d+).*/$1]/' file

perl -pi -e 's/(\[na\s+\*oparea\*\s+check\s+\d+)\s+nul\s+\d+\s+nlr\s+\d+\s+eul\s+\d+\s+elr\s+\d+\]/\1]/' newfile

I have tried both of these lines, but they just make the output file empty
Reply With Quote
  #6 (permalink)  
Old 02-12-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,540
Stumble this Post!
don't have to use regular expression. Unless i read your requirement wrong,
Code:
open(F, "<file") or die "cannot open file:$!\n";
while ( <F> ) {
 if ( /nul/ ) {
  @a = split(/nul/);
  print $a[0] . "]\n";
 } 
}
close(F);
output:
Code:
# ./test.pl
[na *oparea* check 0 ]
Reply With Quote
  #7 (permalink)  
Old 02-12-2008
Registered User
 

Join Date: Jan 2008
Posts: 9
Stumble this Post!
reply

open(F, "<file") or die "cannot open file:$!\n";
while ( <F> ) {
if ( /nul/ ) {
@a = split(/nul/);
print $a[0] . "]\n";
}
}
close(F);

can you please explain this line by line for me.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
awk, perl script

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0