awk script: print line number n of another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script: print line number n of another file
# 8  
Old 03-10-2010
what is your desired o/p? what do you want the code to present to you on screen?

ex:- error on line# 12 dataline12.


Smilie
# 9  
Old 03-10-2010
exactly as you wrote.

a sample output of my old awk script (calling sed via system, which is sloow) you see below.

The output consists of 2*n lines.
- odd lines are from fileA. Each odd ouput line has three columns; these are calculated from contents in a line (e.g. substring, array access). so the first column is line_num as int( substr($0,1,9) )
- even lines are indented with " -> " and show the line number line_num from fileb.

Code:
errors on lines
line       result code              record_id
000000011  111 DEC Format Error     000 Create account
 -> 00012345678D42104310|L 5912345678|L|DEFAULT 12345678D42104310 ACT 20100306031545 00000000000000000000000010020100306031545 +0000000000000000020100306031545978
000000329  100 DEC Internal Error   001 updateAccount
 -> 00012345678D5406073820|L 5912345678|L|DEFAULT 12345678D5406073820 ACT 20100306031555 00000000000000000000010000020100306031555 +0000000000000000020100306031555978


Thank you!
# 10  
Old 03-10-2010
test below:-

Code:
gawk 'BEGIN{} NR==FNR{line_num=int( substr($0,1,9) ) ; a[line_num] ; next}(FNR in a){printf  "Error on line number %09s  %s\n",FNR,$0}' fileA fileB

SmilieSmilieSmilie

Last edited by ahmad.diab; 03-10-2010 at 12:22 PM..
# 11  
Old 03-12-2010
Hi,

thank you! You helped me so much.Smilie This code works as wanted.

But when I tested and added some line breaks here and there to improve readability, the script didnt stop when fileA was read.

Question:
Why do line breaks make a difference?
Where exactely is the content of fileB printed, i.e where is the print statement? And can it be made explicit and modified.
# 12  
Old 03-12-2010
line break do not make a difference when writing in correct format..
what did you do on the code? post it and I will see where is your problem.
# 13  
Old 04-22-2010
hi ahmad.diab,

as you wrote, "line break do not make a difference when writing in correct format". Not wasting too much time I escaped _all_ the linebreaks I've added for readability in "my" awk script with a backslash. This works.


Thank you very much for your help and sorry for answering that late. you helped me a lot.
# 14  
Old 04-22-2010
you are welcome. SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

3. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

4. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

5. Shell Programming and Scripting

awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this: 1;0;177;170;Guadeloupe;x 2;127;171;179;Antigua and Barbuda;x 3;170;144;2;Umpqua;x 4;170;126;162;Coos Bay;x ... 1205;46;2;244;Unmak Island;x 1206;47;2;248;Yunaska Island;x 1207;0;2;240;north sea;x... (5 Replies)
Discussion started by: kalelovil
5 Replies

6. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

7. Shell Programming and Scripting

awk script to return the middle line number

I need an awk script that returns the 1st field of the line in the middle of a file. For example, if a file I have has 6 lines, I want it to return the 1st field on line number 3. If a file has 7 lines, I want the script to return the 1st field on line number 4. File1: 3 214 4 219 5 226 2... (8 Replies)
Discussion started by: jontjioe
8 Replies

8. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

9. Shell Programming and Scripting

awk/shell script to print each line to a file

Dear People, My query is: have a file, which looks likes this: 10 20 30 40 50 1 2 3 4 5 100 200 300 400 500 what i need is: "PRINT EACH LINE TO AN UNIQUE FILE" desired output: file 1 10 20 30 40 50 file 2 1 2 3 4 5 (3 Replies)
Discussion started by: saint2006
3 Replies

10. Shell Programming and Scripting

shell script(Preferably awk or sed) to print selected number of columns from each row

Hi Experts, The question may look very silly by seeing the title, but please have a look at it clearly. I have a text file where the first 5 columns in each row were supposed to be attributes of a sample(like sample name, number, status etc) and the next 25 columns are parameters on which... (3 Replies)
Discussion started by: ks_reddy
3 Replies
Login or Register to Ask a Question