INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Newb looking for awk help.

Newb looking for awk help.

(OP)
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
-------------------------------------------------------


RE: Newb looking for awk help.

Hi

Something like this ?

CODE --> command line

master # awk 'FNR==NR{s=s (s?"|":"")$0;next}$0~"^Start("s")$",/^end$/{next}1' input-file.txt file-to-modify.txt 

Start2B
contents
end 
Tested with gawk and mawk, but should work with any implementation.

Feherke.
feherke.ga

RE: Newb looking for awk help.

(OP)
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

RE: Newb looking for awk help.

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

RE: Newb looking for awk help.

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

RE: Newb looking for awk help.

(OP)
thank for reply.
i'm using window base .

i try modify the file name to single name to reduce mistake, but still same.

RE: Newb looking for awk help.

"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

RE: Newb looking for awk help.

Hi

Quote (kttan84)

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 --> command line

master # cat file-to-modify.txt 
Start1A
contents
end

Start2B
contents
end

Start3C
contents
end

master # cat input-file.txt 
1A
3C

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

Start2B
contents
end 

Feherke.
feherke.ga

RE: Newb looking for awk help.

(OP)
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: <<<

RE: Newb looking for awk help.

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 awk -W version from the command line ? What you put in the kttan84.awk file ?

Feherke.
feherke.ga

RE: Newb looking for awk help.

Hi

Well, not a big problem, the -W 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 which awk command.

Feherke.
feherke.ga

RE: Newb looking for awk help.

(OP)

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
-------------------------------------------------------------------------

RE: Newb looking for awk help.

Hi

To be able to execute kttan84.awk like that(*), it must include a shebang :

CODE --> kttan84.awk

#!/usr/bin/awk

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

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

Feherke.
feherke.ga

RE: Newb looking for awk help.

Quote (kttan84)

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

RE: Newb looking for awk help.

(OP)
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

RE: Newb looking for awk help.

Hi

CODE --> Awk

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

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

1                                # always perform the default action (3)


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

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close