|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Original post from this group in early 2007 1 ) Have a file where each line is numbered currently set a variable to the line numbers that i would like to remove from the file . then remove the line from the file by for a in $variable do grep -v'^'$a' :' file.txt > temp.txt cp temp.txt file.txt done this has been working fine ; however now that the file is growing this method is not very efficent ( slooooow ); file is 40 to 50 thousand lines in length and often there are 15 to 20 thousand lines that i would like to remove . using solaris 10 intel version . ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++ + 2) from this group John provided an excellent solution perl -ne'BEGIN { ( $a = shift ) =~ s/\s+/|/g } /^($a) :/ || print' "$variable" file.txt > temp.txt ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++ New issue now with the $variable set to the lines numbers I'd like to keep from a large file is it possible to to use something similar to the above ? this time to move the lines of interest to the temp.txt file tried to alter the perl command above without success . ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++ the perl script in 2 above is very fast , and would like something similar if possible (similar in that it could be placed in a script ); is there possibly a means to do this with awk that would be quicker than the grep loop indicated in 1 thks.....ab |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
In article
<3ac96394-47be-43b2-8e13-03ad81f370b8@r60g2000hsc.googlegroups.com>, ab <spencedr@gmail.com> wrote: > Original post from this group in early 2007 > > 1 ) Have a file where each line is numbered > > currently set a variable to the line numbers that i would > like to remove from the file . > > then remove the line from the file by > for a in $variable > do > grep -v'^'$a' :' file.txt > temp.txt > cp temp.txt file.txt > done > this has been working fine ; however now that the file is > growing this method is not very efficent ( slooooow ); file is 40 > to 50 thousand lines in length and often there are 15 to 20 > thousand lines that i would like to remove . Use egrep, and set $variable to line1|line2|line3. Then do: egrep -v "^($variable) :" file.txt > temp.txt > > using solaris 10 intel version . > ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++ > + > 2) from this group John provided an excellent solution > > perl -ne'BEGIN { ( $a = shift ) =~ s/\s+/|/g } /^($a) :/ || print' > "$variable" > file.txt > temp.txt > > ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++ > New issue > > now with the $variable set to the lines numbers I'd like to keep > from a large file > is it possible to to use something similar to the above ? this time > to move the > lines of interest to the temp.txt file > > tried to alter the perl command above without success . Change || to &&. > > ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++ > > the perl script in 2 above is very fast , and would like > something similar if possible > (similar in that it could be placed in a script ); is there possibly > a means to do this with awk > that would be quicker than the grep loop indicated in 1 Use egrep, like I said above. It supports alternation in regexps, just like perl and awk. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group *** |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
ab wrote:
> Original post from this group in early 2007 > > 1 ) Have a file where each line is numbered > > currently set a variable to the line numbers that i would > like to remove from the file . > > then remove the line from the file by > for a in $variable > do > grep -v'^'$a' :' file.txt > temp.txt > cp temp.txt file.txt > done > this has been working fine ; however now that the file is > growing this method is not very efficent ( slooooow ); file is 40 > to 50 thousand lines in length and often there are 15 to 20 > thousand lines that i would like to remove . awk -v s="$variable" 'BEGIN{c=split(s,t);for(i=1;i<=c;i++)a[t[i]]} !($1 in a)' file.txt > temp.txt && mv temp.txt file.text > using solaris 10 intel version . > ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++ > + > 2) from this group John provided an excellent solution > > perl -ne'BEGIN { ( $a = shift ) =~ s/\s+/|/g } /^($a) :/ || print' > "$variable" > file.txt > temp.txt > > ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++ > New issue > > now with the $variable set to the lines numbers I'd like to keep > from a large file > is it possible to to use something similar to the above ? this time > to move the > lines of interest to the temp.txt file awk -v s="$variable" 'BEGIN{c=split(s,t);for(i=1;i<=c;i++)a[t[i]]} ($1 in a)' file.txt > temp.txt > tried to alter the perl command above without success . > > ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++ > > the perl script in 2 above is very fast , and would like > something similar if possible > (similar in that it could be placed in a script ); is there possibly > a means to do this with awk > that would be quicker than the grep loop indicated in 1 > > thks.....ab Regards, Ed. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
thks egrep solution worked fine once variable up correctly the && instead of || was great and simple to implement . tried the awk but resulted in core dump tried tried default and /usr/xpg4/bin/awk Barry / Ed thank you for the feed back ....ab |
|
![]() |
| Outils de la discussion | |
|
|