Home > Archive > AWK > March 2006 > Help with line extraction
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help with line extraction
|
|
| rhuston 2006-03-24, 6:59 pm |
| Hi folks. I have files containing hundreds of lines. Using AWK, I want
to extract only lines containing a specific instance of "ISS." It is
possible that "ISS" will occur several times in a line or not, but the
only instance of "ISS" I want considered will always be located at the
87th character position if you're counting from left to right on the
line.
So if "ISS" is located at position 87, remove that instance of "ISS"
from the line and replace it with three empty spaces. Then append the
entire line to a new file.
I got a Windows version of awk from here:
http://cm.bell-labs.com/cm/cs/awkbook/index.html
Someone else gave me the following command, which I have tested and
doesn't work for me:
awk 'substr($0,p,3)=="ISS" {
print substr($0,1,p-1) " " substr($0,p+3)}' p=87 oldfile >new
The error I get from this is:
awk: syntax error at source line
context is[color=darkred]
awk: bailing out at source line 1
If anyone can help I'd be very grateful. Thanks!
| |
| Janis Papanagnou 2006-03-24, 6:59 pm |
| rhuston wrote:
> Hi folks. I have files containing hundreds of lines. Using AWK, I want
> to extract only lines containing a specific instance of "ISS." It is
> possible that "ISS" will occur several times in a line or not, but the
> only instance of "ISS" I want considered will always be located at the
> 87th character position if you're counting from left to right on the
> line.
>
> So if "ISS" is located at position 87, remove that instance of "ISS"
> from the line and replace it with three empty spaces. Then append the
> entire line to a new file.
>
> I got a Windows version of awk from here:
> http://cm.bell-labs.com/cm/cs/awkbook/index.html
>
> Someone else gave me the following command, which I have tested and
> doesn't work for me:
>
> awk 'substr($0,p,3)=="ISS" {
> print substr($0,1,p-1) " " substr($0,p+3)}' p=87 oldfile >new
Something like that is what I would have also suggested.
> The error I get from this is:
>
> awk: syntax error at source line
> context is
>
>
> awk: bailing out at source line 1
>
> If anyone can help I'd be very grateful. Thanks!
>
You are using awk on Windows, so you need different quoting and have
to escape the quotes within the awk program if you write it explicit
on the WinDOS command line. Better put everything within the single
quotes in a file 'yourfile' and call it as: awk -f yourfile
Janis
| |
| Patrick TJ McPhee 2006-03-27, 3:56 am |
| In article <1143236614.236143.238420@u72g2000cwu.googlegroups.com>,
rhuston <richard.a.huston@lmco.com> wrote:
% Someone else gave me the following command, which I have tested and
% doesn't work for me:
%
% awk 'substr($0,p,3)=="ISS" {
% print substr($0,1,p-1) " " substr($0,p+3)}' p=87 oldfile >new
This is the awk program from the above:
substr($0,p,3)=="ISS" {
print substr($0,1,p-1) " " substr($0,p+3)
}
Put it in a file, then run it like this:
awk -f subst.awk p=87 oldfile > newfile
The example you give uses Unix shell-quoting conventions to put the
script on the command-line.
--
Patrick TJ McPhee
North York Canada
ptjm@interlog.com
| |
| rhuston 2006-03-28, 6:56 pm |
| Thanks so much guys. I will test it out today.
| |
| rhuston 2006-03-28, 6:56 pm |
| That worked wonderfully except for one small detail: In my test files,
when the very last line has ISS in the 87th charcacter position (and
nothing after it), that last line doesn't get appended to the new file.
All other ISS lines preceding it make it to the new file. Any
thoughts here?
| |
| Janis Papanagnou 2006-03-28, 6:56 pm |
| rhuston wrote:
> That worked wonderfully
What worked wonderful? Please quote context.
> except for one small detail: In my test files,
> when the very last line has ISS in the 87th charcacter position (and
> nothing after it), that last line doesn't get appended to the new file.
> All other ISS lines preceding it make it to the new file. Any
> thoughts here?
Some broken applications don't create line terminators (CR and/or LF)
on the last line.
Load the file with vim and see whether there is a [no eol] message.
If so, then just save the file with vim (use command :wq). Or use
the command od -c.
Janis
| |
| rhuston 2006-03-28, 6:56 pm |
| Not sure what "vim" is...a unix text editor? If the file lacks a line
terminator on the last line, could awk be used to put one there before
the ISS lines are extracted?
Thanks,
Tony
| |
| Janis Papanagnou 2006-03-28, 6:56 pm |
| Please quote context. If you don't know what I am talking about you may
like to read http://cfaj.freeshell.org/google/. Thank you!
rhuston wrote:
> Not sure what "vim" is...a unix text editor?
Ah, yes, that's true. Sorry for assuming vim to be commonly known and
available.
> If the file lacks a line
> terminator on the last line, could awk be used to put one there before
> the ISS lines are extracted?
If you want an extra awk call (instead of fixing the source file in the
first place) you may do that using...
awk 1
Here you see how it works...
$ print -n "Hello\nWorld" | od -c
0000000 H e l l o \n W o r l d
0000013
$ print -n "Hello\nWorld" | awk 1 | od -c
0000000 H e l l o \n W o r l d \n
0000014
> Thanks,
> Tony
>
Janis
| |
| rhuston 2006-03-28, 6:56 pm |
| Thanks Janis and Patrick. I have it working now. You guys rock!
|
|
|
|
|