Results 1 to 3 of 3

Thread: Deleting Fields

  1. #1
    Join Date
    Feb 2006

    Unanswered: Deleting Fields

    File Layout:

    rtg1ad00620041230tom 12 ^m
    rtg1bd00620041231mari10 ^m
    rtg2bd00620041231sam 12^m

    1) what command can i use to strip the spaces from position 23 - ^m in line record "rtg1" (sed???)

    2) after that, delete the first 17 chars in record "rtg2" and than concat line "rtg1" with "rtg2" to make one record

    rtg1ad00620041230tom 12mark15^m
    rtg1bd00620041231mari10 12^m

    Appreciate the help.

  2. #2
    Join Date
    Jan 2006
    You can do all that in sed or awk.

    Are all the lines labeled rtg1 or rtg2 and nothing else? Are the rtg1 and rtg2 lines always in that order or can they be in any order?

    I'll make the following assumptions:
    1) this is a large file with repeated rtg1 and rtg2 lines.
    2) the file only contains lines starting with rtg1 or rtg2.
    3) the lines always appear in the order rtg1, rtg2. Although not necessarily consecutively.
    4) '^m' means end of line
    5) the key following rtg1 is unique for each set of lines and is always 13 characters long.

    create a file called script.awk containing:
    /rtg1/ { part1 = substr($0, 1,23);
               part2 = substr($0, 24); 
               gsub(/ /, "", part2);
               line = sprintf("%s%s", part1, part2);
               key = substr($0, 5,13);
               save[key] = line;
    /rtg2/ { key = substr($0, 5,13);
               if  (length(save[key]) == 0)
                  printf("Error: no rtg1 line found for %s\n", $0);
                  printf("%s%s\n", save[key], substr($0, 18));
    then run the following from the command line:
    # awk -f script.awk file-you-want-to-process > result-file


  3. #3
    Join Date
    Feb 2006
    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts