Changing Line Number of a File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing Line Number of a File
# 1  
Old 05-15-2007
Changing Line Number of a File

Example:
O o x
[

2 A X
3 B X
4 C X
5 D X
6 E Z
7 F Z
a b c 12d
]

What I would like to do is to rename the first column of the above file without affecting the format. The output should look like the following:

Output:
O o x
[

1 A X
2 B X
3 C X
4 D X
5 E Z
6 F Z

a b c 12d
]


#! /bin/ksh

cd $HOME/lib/.Lee

#nl = no. of lines.
nl=`grep 'X' ex | wc -l`

#ln = line no.
ln=1
$1=NR
until [[ "$ln" = "$nl" ]]; do
awk '{$1 = NR; print}' ex
ln=ln+1
done

The above script affects the format and the results keep on and on and on.
# 2  
Old 05-16-2007
Code:
awk -v no=1 ' /\[/,/\]/ { if ( $0 ~ "^[0-9]+ " ) { sub("^[0-9]+ ",no" ");no++}}; 1 ' filename

# 3  
Old 05-16-2007
anbu can we have some awk code explanation..I am confused..but I like this topic

thanks in advance
# 4  
Old 05-16-2007
Quote:
Originally Posted by amon
anbu can we have some awk code explanation..I am confused..but I like this topic

thanks in advance
/\[/,/\]/ { ... } This block is executed for lines between [ and ]

$0 ~ "^[0-9]+ " Checks whether first field is a number

sub("^[0-9]+ ",no" ") If above condition is true replace first field by the value in variable "no" and increment the variable "no"

1 which means true and prints the line
# 5  
Old 05-16-2007
thanks for nice explanation..Smilie
# 6  
Old 05-18-2007
Thanks for the reply. I tried using the script you gave but showed the following messages:

awk: syntax error at source line 1
awk: bailing out at source line 1


So, I tried few other things but gave the same results. What I want to do exactly is to renumber only the first column where X are found.

Regards.
# 7  
Old 05-18-2007
Quote:
Originally Posted by ilak1008
Thanks for the reply. I tried using the script you gave but showed the following messages:

awk: syntax error at source line 1
awk: bailing out at source line 1


So, I tried few other things but gave the same results. What I want to do exactly is to renumber only the first column where X are found.

Regards.
Use nawk to avoid this error.

Do you mean the X in third field? Then try this
Code:
nawk -v no=1 ' /\[/,/\]/ { if ( $0 ~ "^[0-9]+ " && $3 == "X"  ) { sub("^[0-9]+ ",no" ");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