regarding about printing line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regarding about printing line number
# 1  
Old 07-13-2009
regarding about printing line number

Hello,
I am testing some data to get line number at cursor position 9 and found some problem, the code is below.Assume we got 3 attribute. At second attribute, there are some data(eg.A41/A6) missing like at the fourth and six line


Code:
11006                        A41              1888
11006                        A6               2456
11006                        A43              4532
11006                                         3333
11006                        A33              5412
11006                                         8987
11006                        A34              4999

if there is no data missing at second attribute, when i use the code
Code:

awk '{ printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3,$4); }' myfile

it is working perfect and printing all line number at cursor postion 9.


But if there is missing data at second or third attribute,line number print ok ,but the output data mess up like that



Code:
11006  1                     A41              1888
11006  2                     A6               2456
11006  3                     A43              4532
11006  4                     3333
11006  5                     A33              5412
11006  6                     8987
11006  7                     A34              4999

Any idea how to fix this problem?



Many thanks
# 2  
Old 07-13-2009
I hope this is enough :

Code:
awk -F"!" '{  printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3,$4); }' input_file.txt

( i mean take other than space as a delimiter )

But Line is the problem in this case to print

Last edited by panyam; 07-13-2009 at 05:48 AM..
# 3  
Old 07-13-2009
Try this(Assuming 2nd column in original file satrts with A):

Code:
awk '{ if($2 !~ /^A/) { $3=$2; $2=""; } printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3); }' filename

# 4  
Old 07-13-2009
Looks your input is fixed width...
Code:
awk '{
FIRST=substr($0,1,7);
SECOND=substr($0,30,17);
THIRD=substr($0,47,4);
$1=FIRST
$2=SECOND
$3=THIRD
printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3);
}' input

# 5  
Old 07-13-2009
Hello,
Many thanks for your reply, i have test both of them
for this code,
Code:
awk -F"!" '{  printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3,$4); }' input_file.txt

i got this error "bash: !: event not found"



this code is working fine

Code:
awk '{ if($2 !~ /^A/) { $3=$2; $2=""; } printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3); }' filename

But dennis, second attribute can start with any character, A-Z and 0-9
so tried /^[A-Z]/ and ok , but when i write /^[A-Z][0-9]/ ,it is not working.

Any idea,




Many thanks
# 6  
Old 07-13-2009
Hey, you can simply do this...
Code:
awk '{ if($3=="") { $3=$2; $2=""; } printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3); }' input

# 7  
Old 07-13-2009
Try:

It will be difficult if the number of columns in the record is not fixed. However, assuming that the last column will always have a value, the below one should work.

Code:
awk '{ if (NF==2) { $3=$2; $2=""; } printf("%-7s%-22s%-17s%-4s\n",$1,FNR,$2,$3); }' filname

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

2. Shell Programming and Scripting

Printing Number of Fields with the line number

Hi, How to print the number of fields in each record with the line number? Lets saw I have 3212|shipped|received| 3213|shipped|undelivered| 3214|shipped|received|delivered I tried the code awk -F '|' '{print NF}' This gives me ouput as 3 3 4 (5 Replies)
Discussion started by: machomaddy
5 Replies

3. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

4. Shell Programming and Scripting

Printing the line number in bash script

Hi, I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened. Thanks, (6 Replies)
Discussion started by: suryaemlinux
6 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

7. Shell Programming and Scripting

printing line number

hi, i have a file, i need to search for a string , if the line contains i need to print that line number and line , please help thanks in advance Satya (5 Replies)
Discussion started by: Satyak
5 Replies

8. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

10. Shell Programming and Scripting

printing a line number using awk

Hi Chaps, I'm trying to print the line number of a comma delimited file where the second field in the line is blank using AWK. Here is the code I have so far where am I going wrong. It is the last column in the file. nawk -v x==0 'BEGIN {FS=",";OFS=","} x++ if ($2 == " ") print $x' bob.tst ... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question