views:

156

answers:

3

Hello,

I have a text file which has hex values, one value on one separate line. A file has many such values one below another. I need to do some analysis of the values for which i need to but some kind of delimiter/marker say a '#' in this file before line numbers 32,47,62,77... difference between two line numbers in this patterin is 15 always.

I am trying to do it using awk. I tried few things but didnt work.

What is the command in awk to do it?

Any other solution involving some other language/script/tool is also welcome.

Thank you.

-AD

A: 

Python:

f_in = open("file.txt")
f_out = open("file_out.txt","w")
offset = 4 # 0 <= offset < 15 ; first marker after fourth line in this example

for num,line in enumerate(f_in):
   if not (num-offset) % 15:
       f_out.write("#\n")
   f_out.write(line)
balpha
+2  A: 

This is how you can use AWK for it,

awk 'BEGIN{ i=0; } \
    {if (FNR<31) {print $0} \
     else {i++; if (i%15) {print $0} else {printf "#%s\n",$0}}\
    }' inputfile.txt > outputfile.txt

How it works,

  • BEGIN sets an iterator for counting from your starting line 32
  • FNR<31 starts counting from the 31st record (the next record needs a #)
    • input lines are called records and FNR is an AWK variable that counts them
  • Once we start counting, the i%15 prefixes a # on every 15th line
  • $0 prints the record (the line) as is

You can type all the text with white spaces skipping the trailing '\' on a single command line.
Or, you can use it as an AWK file,

# File: comment.awk
BEGIN{ i=0; }
$0 ~ {\
    if (FNR<31) {print $0} \
    else {\
        i++; \
        if (i%15) {\
            print $0
        }\
        else {\
            printf "#%s\n",$0
        }\
    }\
 }

And run it as,

awk -f comment.awk inputfile.txt > outputfile.txt

Hope this will help you to use more AWK.

nik
@Nik: thanks for the script. It worked for me. I just had to make a small change FNR <18 so that first # would be put at 32 (17+15) and then subsequently after every 15 lines. Although i did not understand the script completely, i sure will learn awk more seriously as its damn powerful.. Thanks!
goldenmean
@goldenmean, You can point out the parts you did not get in this script, will try to explain them more elaborately.
nik
A: 

Haskell:

offset = 31;
chunk_size = 15;

main = do
{
  (h, t) <- fmap (splitAt offset . lines) getContents;
  mapM_ putStrLn h;
  mapM_ ((putStrLn "#" >>) . mapM_ putStrLn) $
  map (take chunk_size) $
  takeWhile (not . null) $
  iterate (drop chunk_size) t;
}
finnw