I have a big file with some record which will be having different values than others.

Actual data file:


$ grep -n "9999999999" BADINS0000001065_000000000020140301.dat

I wanted to append this record into bottom of the file, simply if I have ~100 records in file if my record position is somewhere in the file, I have to find, remove from that location (completely) and need to be appended to end of the file (Bottom of the file).

There are many files in directory I want to make it automate this process for every run. File name is BADINS0000001065_0000000000* always same.

Would you please help me to achieve this?

  • 1
    How many "bad" records do you expect in each file? More than one? – glenn jackman Apr 23 '15 at 15:53
  • 1
    Also, show your desired output. Then everyone will be clear about what you want. – glenn jackman Apr 23 '15 at 15:54

With awk you can do:

awk '
  /PATTERN/ { save = $0 ; next }
  { print }
  END { print save }
' infile > outfile

where you have to replace PATTERN by the actual pattern and infile is your data file; rename those appropriately.

The code works as follows:

/PATTERN/ { save = $0 ; next } - if the pattern is found save this line for later use and skip printing (so it's removed from this place)

{ print } - prints those lines in the file that have not been skipped by the previous command

END { print save } - finally, when all lines are processed, append the saved line, the one where the pattern matched

If you want to overwrite the original file by the new file add a mv command after the awk script:

mv outfile infile

again with appropriately chosen filenames.

  • Thank you for quick response. I have to keep the row at the bottom of the file(There are n number of records in file) – Mahesh Kothapalli Apr 23 '15 at 14:57
  • @Mahesh; I don't understand your comment. - I'll add an explanation in my answer, in case it's unclear. – Janis Apr 23 '15 at 14:59
  • Note that this will add a blank line to the bottom of the file, if no PATTERN line is found. – Peter.O Apr 23 '15 at 22:38
  • @Peter.O; if you want to cover that case do the final print conditionally: END { if(save) print save }. – Janis Apr 23 '15 at 23:00

Other sed solution

sed -i '
        /9999999999/{h;d};           # move match string in hold space
        $G;                          # append string from hold space to end
        s/\n$//                      # avoid empty line if pattern have not met 
       ' BADINS0000001065_0000000000*

The question is not clear on whether or not the 9999999999 is always present, or whether there can be more than one such instance in the input file. So here is a sed version which caters for all of these situations.
See man sed (option -i) for in-place update of the input file.

sed -n '/9999999999/{H;b n}; p; :n; ${g;s/\n//p}' file
  • Sorry guys for very late response. 9999999999 would be there always – Mahesh Kothapalli May 18 '15 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.