1

Below is the format of some table data contained within a single file:

;NULL;ABCD;ABHJARS;;ABCD;ABCD;Y;;;;;;;;;A;
;NULL;XEU-ANKD;XEU-AJKD;;ABCD;ABCD;Y;;;;;;;;;A;
.
.
;11744;AMKDIONSKH;AMKDJ AN DJ JAHF AS CPFVH MTM;;QWERDF;QWERDF;Y;;;;;;;;;A;

(5436rowsaffected)
(returnstatus=0)

Returnparameters:

;;
;5436;


(1rowaffected)
;;;
;-------;-----------;
;grepkey;5436;

(1rowaffected)

NOTE: Above grepkey=5436 (Count of the records present in table).

Below is the expected output:

1;NULL;ABCD;ABHJARS;;ABCD;ABCD;Y;;;;;;;;;A;
2;NULL;XEU-ANKD;XEU-AJKD;;ABCD;ABCD;Y;;;;;;;;;A;
.
.
5436;11744;AMKDIONSKH;AMKDJ AN DJ JAHF AS CPFVH MTM;;QWERDF;QWERDF;Y;;;;;;;;;A;

I need the data in the above format. I'd like to prefix the row number and exclude the additional data that is present at the end of the file, like count of records in the table etc.

Additionally I'd like to accomplish the above using awk.

4
  • is that the . in the middle is really a dot or the lines just like ;NULL;ABCD;ABHJARS;;ABCD;ABCD;Y;;;;;;;;;A; – Avinash Raj May 8 '14 at 12:05
  • its the lines just like ;NULL;ABCD;ABHJARS;;ABCD;ABCD;Y;;;;;;;;;A; – Rajiv May 8 '14 at 12:15
  • try @terdon's answer or you may try this awk '/.*A;$/ {print NR$0}' file if all the records you want to extract are ended with A; – Avinash Raj May 8 '14 at 12:25
  • not all records are ending with A ..some times its 'I' – Rajiv May 8 '14 at 12:26
1

I'm not sure I've understood correctly. I think that you want the line number added to each row and to remove everything after the first blank line. If so, this will do it:

awk '{if($1){print NR$0}else{exit}}' file 

Explanation

  • if($1){print NR$0} : if this line has a 1st field (a simple way of checking whether the line is empty), print the current line number (NR) and the line $0.
  • else{exit} : stop the script at the first empty line.
1
awk -v n=$( grep -oP '(?<=grepkey;)\d+' file ) 'NR <= n {print NR ";" $0}' file

Here I use a GNU grep command to extract the "grepkey" value from the file, then an awk script to print the first "n" lines of the file, with the line number prepended.

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.