Changing Line Number of a File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing Line Number of a File
# 8  
Old 05-18-2007
Try that:
Code:
awk '/^\[/,/^]/ { if ($3 == "X") $1=++line } 1' filename

Jean-Pierre.
# 9  
Old 05-19-2007
Hello Anbu23!

The code you gave worked fine.

I tried to use similar nawk command to the file below but nothing happened.

nawk -v no=1 ' /M_I : \[/,/\]/ { if ( $1 ~ "^[0-9]+ " && $7 == "HI" ) { sub("^[0-9]+ ",no" ");no++}}; 1 ' filename

Contents of "filename":

D : [
# ===========



]


S : [
# ============

Nb_S_I : 5
S_I : [

# R_L "S_D"
56 "1-2x"
48 "1-2x"
40 "1-2x"
32 "1-2x"
24 "1-2x"

]

]


M_I : [
# ===========


# M_I B_T S_Nb C_Nb L_N P_Nb O M_I A_F D_F
4 X 9 1 2 4 HI 1 F F
5 F 2 1 4 0 HI 1 F F
6 F 4 1 6 0 HI 1 F F
10 L 1 1 8 4 HI 1 F F
]


The output should be as follows:

D : [
# ===========



]


S : [
# ============

Nb_S_I : 5
S_I : [

# R_L "S_D"
56 "1-2x"
48 "1-2x"
40 "1-2x"
32 "1-2x"
24 "1-2x"

]

]


M_I : [
# ===========


# M_I B_T S_Nb C_Nb L_N P_Nb O M_I A_F D_F
1 X 9 1 2 4 HI 1 F F
2 F 2 1 4 0 HI 1 F F
3 F 4 1 6 0 HI 1 F F
4 L 1 1 8 4 HI 1 F F

]
# 10  
Old 05-19-2007
The field $1 can't contain a space that is a separator.
Change your test :
Code:
nawk -v no=1 ' /M_I : \[/,/\]/ { if ( $0 ~ "^[0-9]+ " && $7 == "HI" ) { sub("^[0-9]+ ",no" ");no++}}; 1 ' filename

The nawk program can be simplified:
Code:
nawk '/M_I : \[/,/\]/ { if ( $0 ~ /^[0-9]\+ / && $7 == "HI" ) $1 = ++no }; 1 ' filename

Jean-Pierre.
# 11  
Old 05-20-2007
I have attached a sample file about the actual format of the lines I want its line numbers changed.

I used the following script to change the line numbers:

#! /bin/ksh

cd $HOME/lib
nawk -v no=1 ' /Marker_Item : \[/,/\]/ { if ( $5 == "HIGH" ) {sub($1,no);++no}}; 1 ' filename

As you noticed at the results, the alignment for lines 7, 8 and 9 has changed. Any ideas on how to realign it?

Thanks!
# 12  
Old 05-20-2007
if you have Python, here's an alternative;
Code:
flag = False
num=1 # renaming counter
for line in open("file"):
    line = line.strip()
    if line.startswith("M_I : ["): flag = True
    if flag:        
        l = line.split()    
        try:
            if l[0].isdigit():
                l[0]=str(num)
                num+=1                 
        except:pass
        print " ".join(l)
    else:print line
    if line.startswith("]"):
        flag = False

output:
Code:
# ./test.py

D : [
# ===========



]


S : [
# ============

Nb_S_I : 5
S_I : [

# R_L "S_D"
56 "1-2x"
48 "1-2x"
40 "1-2x"
32 "1-2x"
24 "1-2x"

]

]


M_I : [
# ===========


# M_I B_T S_Nb C_Nb L_N P_Nb O M_I A_F D_F
1 X 9 1 2 4 HI 1 F F
2 F 2 1 4 0 HI 1 F F
3 F 4 1 6 0 HI 1 F F
4 L 1 1 8 4 HI 1 F F
]

# 13  
Old 05-20-2007
Quote:
Originally Posted by ghostdog74
if you have Python, here's an alternative;
[/code]

I'm sorry ghostdog74 'coz I just use ksh shell.

Thanks for your help anyway.
# 14  
Old 05-21-2007
Quote:
Originally Posted by ilak1008
I have attached a sample file about the actual format of the lines I want its line numbers changed.

I used the following script to change the line numbers:

#! /bin/ksh

cd $HOME/lib
nawk -v no=1 ' /Marker_Item : \[/,/\]/ { if ( $5 == "HIGH" ) {sub($1,no);++no}}; 1 ' filename

As you noticed at the results, the alignment for lines 7, 8 and 9 has changed. Any ideas on how to realign it?

Thanks!
Code:
nawk ' /Marker_Item : \[/,/\]/ { 
	if ( $5 == "HIGH" ) {
		str=$0;
		sub($1".*",$1,str);
		sub(" *"$1,sprintf("%*d",length(str),++no))
	}
}; 1 ' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

2. Shell Programming and Scripting

Shell script for changing the accession number of DNA sequences in a FASTA file

Hi, I am having a file of dna sequences in fasta format which look like this: >admin_1_45 atatagcaga >admin_1_46 atatagcagaatatatat with many such thousands of sequences in a single file. I want to the replace the accession Id "admin_1_45" similarly in following sequences to... (5 Replies)
Discussion started by: margarita
5 Replies

3. Shell Programming and Scripting

Changing Line in Txt File

So I have a python program that I run, which runs accordingly to options I have listed in a text file (ie user_prefs). Now there are many options listed in this user_prefs.txt, but the one of most interest to me is that of the file path of the time series. I have over a hundred of these time... (8 Replies)
Discussion started by: Jimmyd24
8 Replies

4. Shell Programming and Scripting

Replace first number of each line in a file with another number

Hi, I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@, for file in... (13 Replies)
Discussion started by: scorpioraghu
13 Replies

5. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

6. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

7. Shell Programming and Scripting

Modifying a file without changing inode number

Hi all, I am struggling to change the content of a file without changing the inode number. The exact issue is as below. I have a file name test.bak which has 100 lines of text. I am trying to to delete the first 90 lines of the text in the file. I know that using sed/awk/head/tail I can... (3 Replies)
Discussion started by: sathishkmrv
3 Replies

8. 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

9. Shell Programming and Scripting

changing line on a file

Hi, I want to change on a file this line: vif = into: vif = But there are some conditionals: 1. last part '] of the original line after 00:16:3E:CE:23:14' ] may vary to 00:16:3E:CE:23:14' ] or 00:16:3E:CE:23:14 ' ] 2. The second mac adrress of the line I need,... (4 Replies)
Discussion started by: iga3725
4 Replies

10. 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
Login or Register to Ask a Question