Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newb looking for awk help.

Status
Not open for further replies.

kttan84

Vendor
Jan 19, 2016
10
MY
Hi ,

i'm not a programmer , just want to eddit abit old script that left up.

i need a function can filter up those unwanted part in a text file with a input.

it will delete those section that i'm giving in input file.


Exp:
file i need to modify:
-------------------------------------------------------
Start1A
contents
end

Start2B
contents
end

Start3C
contents
end
-------------------------------------------------------

input file:
1A
3C
-------------------------------------------------------
output file:
Start2
contents
end
-------------------------------------------------------


 
Hi

Something like this ?
Code:
master # awk '[navy]FNR[/navy][teal]==[/teal][navy]NR[/navy][teal]{[/teal][navy]s[/navy][teal]=[/teal][navy]s[/navy] [teal]([/teal][navy]s[/navy][teal]?[/teal][i][green]"|"[/green][/i][teal]:[/teal][i][green]""[/green][/i][teal])[/teal][navy]$0[/navy][teal];[/teal][b]next[/b][teal]}[/teal][navy]$0[/navy][teal]~[/teal][i][green]"^Start("[/green][/i]s[i][green]")$"[/green][/i][teal],[/teal][fuchsia]/^end$/[/fuchsia][teal]{[/teal][b]next[/b][teal]}[/teal][purple]1[/purple]' input-file.txt file-to-modify.txt 

Start2B
contents
end
Tested with [tt]gawk[/tt] and [tt]mawk[/tt], but should work with any implementation.


Feherke.
feherke.ga
 
Hi ,

Thank for answer, i'm try, but can't .
if not mistake I just got normal awk.

the output come out all what from file-to-modify
 
Hi

I used exactly the samples you posted earlier. Are you sure they are correct ? ( Note that by default Awk matches case sensitively. )

You are using it on a Unix or Unix-like operating system, right ?


Feherke.
feherke.ga
 
The file names are transposed,

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
thank for reply.
i'm using window base .

i try modify the file name to single name to reduce mistake, but still same.
 
i'm using window base" means what??


By the way, "transposed" means 'wrong way around' or 'swapped places' nothing to do with length but IF your file names have spaces in them you need to wrap them in quotes.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Hi

kttan84 said:
i'm using window base .
I met Windows users in the past who experienced problems with Awk. Try to put the Awk code in a file and execute it like this :
Code:
[blue]master #[/blue] cat file-to-modify.txt 
Start1A
contents
end

Start2B
contents
end

Start3C
contents
end

[blue]master #[/blue] cat input-file.txt 
1A
3C

[blue]master #[/blue] cat kttan84.awk 
FNR==NR{s=s (s?"|":"")$0;next}$0~"^Start("s")$",/^end$/{next}1

[blue]master #[/blue] awk -f kttan84.awk input-file.txt file-to-modify.txt

Start2B
contents
end

Feherke.
feherke.ga
 
Hi,

Thank for response.

i'm not familiar with awk.
i'm using kornshell for run it.

i try it but the error as below:
$ kttan84.awk
C:/NuTCROOT/mksnt/awk.exe: Syntax error Context is:
>>> D: <<<
 
Hi

Sadly I am not familiar with MKS Toolkit, but if it provides a genuine KSh in a fully set up POSIX environment ( just like CygWin does ), then the command as I posted earlier should work as is.

What you get if you run [tt]awk -W version[/tt] from the command line ? What you put in the kttan84.awk file ?


Feherke.
feherke.ga
 
Hi

Well, not a big problem, the [tt]-W[/tt] option is not standard, but some implementations have it.

So far I would still expect the command I posted on 20 Jan 16 12:20 should work.

When answering my other question, "What you put in the kttan84.awk file ?", please also post the output of the [tt]which awk[/tt] command.



Feherke.
feherke.ga
 

Hi ,

what i put is in the file are as below, i try remove the master too, it still can't.
file name as (kttan84.awk)

-------------------------------------------------------------------------
master # cat kttan84.awk
FNR==NR{s=s (s?"|":"")$0;next}$0~"^Start("s")$",/^end$/{next}1

master # awk -f kttan84.awk input-file.txt file-to-modify.txt
-------------------------------------------------------------------------

 
 http://files.engineering.com/getfile.aspx?folder=32512a78-272c-422a-9bcd-8e78fa1d3013&file=Untitled.png
Hi

To be able to execute kttan84.awk like that[sup][blue](*)[/blue][/sup], it must include a shebang :
Code:
[gray]#!/usr/bin/awk[/gray]

[navy]FNR[/navy][teal]==[/teal]NR[teal]{[/teal][navy]s[/navy][teal]=[/teal][navy]s[/navy] [teal]([/teal][navy]s[/navy][teal]?[/teal][i][green]"|"[/green][/i][teal]:[/teal][i][green]""[/green][/i][teal])[/teal][navy]$0[/navy][teal];[/teal][b]next[/b][teal]}[/teal][navy]$0[/navy][teal]~[/teal][i][green]"^Start("[/green][/i][navy]s[/navy][i][green]")$"[/green][/i][teal],[/teal][fuchsia]/^end$/[/fuchsia][teal]{[/teal][b]next[/b][teal]}[/teal][purple]1[/purple]
Where "/usr/bin/awk" is the path to the Awk interpreter. May be different on your system. [tt]which awk[/tt] displays the actual path you need to put there.

[blue](*)[/blue] Of course, the above applies to Unix and Unix-like systems, including CygWin. Not sure about MKS Toolkit.


Feherke.
feherke.ga
 
kttan84 said:
i try remove the master too, it still can't.

You are aware that "master #" is not part of the command line, but is the 'command prompt' on feherke's terminal window.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Hi ,


cool it work . thank you.

may i know how it function ? coz i need modify it for my program, those i give a an simple example , in actual file have include spacebar and symbol, i want learn some, it look fun, although im not programmer.


FNR==NR{s=s (s?"|":"")$0;next}$0~"^Start("s")$",/^end$/{next}1
 
Hi

Code:
[navy]FNR[/navy] [teal]==[/teal] [navy]NR[/navy] [teal]{[/teal]                      [gray]# if processing first input file (1)[/gray]
    [navy]s[/navy] [teal]=[/teal] [navy]s[/navy] [teal]([/teal][navy]s[/navy] [teal]?[/teal] [i][green]"|"[/green][/i] [teal]:[/teal] [i][green]""[/green][/i][teal])[/teal] [navy]$0[/navy]      [gray]# append the currently processed record to variable s, (2)[/gray]
                                 [gray]# using "|" as separator if s already contains anything[/gray]
    [b]next[/b]                         [gray]# continue with next input record[/gray]
                                 [gray]# ( skip all following code )[/gray]
[teal]}[/teal]

[navy]$0[/navy] [teal]~[/teal] [i][green]"^Start("[/green][/i] [navy]s[/navy] [i][green]")$"[/green][/i][teal],[/teal] [fuchsia]/^end$/[/fuchsia] [teal]{[/teal] [gray]# if current record is between records matching the two patterns[/gray]
                                 [gray]# ( first regex specified as string because was composed dynamically )[/gray]
    [b]next[/b]                         [gray]# continue with next input record[/gray]
[teal]}[/teal]

[purple]1[/purple]                                [gray]# always perform the default action (3)[/gray]


From man awk :
(1) [b]FNR[/b]         The input record number in the current input file.
    [b]NR[/b]          The total number of input records seen so far.
(2) [b]$0[/b] is the whole record.
(3) An AWK program consists of a sequence of pattern-action statements
        [u]pattern[/u]   [b]{[/b] [u]action statements[/u] [b]}[/b]
    [gray](...)[/gray]
    A missing action is equivalent to
        [b]{ print }[/b]
    which prints the entire record.

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top