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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace first number of each line in a file with another number
# 8  
Old 06-28-2012
alister, actually the first field in all the files is always a number.
# 9  
Old 06-28-2012
Quote:
Originally Posted by alister
I've become so predictable. Smilie
[...]
Code:
printf '%s\n' ',s/^[^,]*/999/' w q | ed -s file

Regards,
Alister
Yup ! I was waiting for that one ! Smilie
This User Gave Thanks to ctsgnb For This Post:
# 10  
Old 06-28-2012
Can you guys tell me how do I make the changes go into the files?
# 11  
Old 06-28-2012
Use alister's solution (but carrefully), no need for a temp file and your files won't have their inode changed (contrary to the proposed solution with the mv command).
# 12  
Old 06-28-2012
Got it with this..
Code:
awk -F",@," '{sub($1,999); print}' $file >> $file

Thanks guys, you rock !!!, I am amazed by the quick replies unlike the java forums I usually visit...

Last edited by Franklin52; 06-29-2012 at 04:06 AM.. Reason: Please use code tags for data and code samples
# 13  
Old 06-28-2012
Quote:
Originally Posted by scorpioraghu
Got it with this..

awk -F",@," '{sub($1,999); print}' $file >> $file

Thanks guys, you rock !!!, I am amazed by the quick replies unlike the java forums I usually visit...
You sure you want to append to the same file you're reading from? Even if you don't find yourself in a loop where AWK reads lines it wrote itself, you're not changing the original lines.

Regards,
Alister
# 14  
Old 06-28-2012
Yeah I realized that now. This seems to be taking care of it.
Code:
sed 's/[0-9]\{1,\}/999/1' $file > $$ 
mv $$ $file

The script you gave me
Code:
printf '%s\n' ',s/^[^,]*/999/' w q | ed -s $file

is errroring out with new line found

Last edited by Franklin52; 06-29-2012 at 04:06 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a number in the last line of a delimited file.

Hi all, I am fairly new to UNIX and I was wondering if you could provide me with some help! Lets say i have a file as below : Line 1 Line 2 Line 3 ABC|12|4|2 Now the number 4 in bold, this number will represent the number of row there is in the file excluding the header and footer... (10 Replies)
Discussion started by: Stinza
10 Replies

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

3. Shell Programming and Scripting

Replace a field with line number in file

I am working on a script to convert bank data to a csv file. I have the format done - columns etc. The final piece of the puzzle is to change the second field (after the R) of every line to reflect its' line number in the file. I am stumped. I can use awk on each line but need help looping through... (9 Replies)
Discussion started by: Melah Gindi
9 Replies

4. Shell Programming and Scripting

Dynamic number to replace with line

Hi, I have file as below COMMIT # at 34572 # at 23432 COMMIT # at 5674 # at 7856 I want to replace with as below (12 Replies)
Discussion started by: kaushik02018
12 Replies

5. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

replace line starting with not a number

Dear users, I have a file like this: geometry,geometry_vertex_count,Id,strnum,platecode,datatype,dtnum,refnum,appearance,disappeara,color,geogdesc,datatype_ft_style,import_notes "<LineString><coordinates>-130.6539,51.5103,0 -130.7708,51.6287,0 -130.8356,51.6832,0 -130.9211,51.7772,0... (5 Replies)
Discussion started by: Gery
5 Replies

7. Shell Programming and Scripting

replace blank line number

hlow all i need help how can i replace blank number with awk input.txt 300::|355264313178490 301::|358814003239510 302::|358316038113400 303::|357954002633660 304::|354072040694090 305::|356956015214190 306::|352943020525180 307::|359574033836610 308::|381810990023580 so will be like... (4 Replies)
Discussion started by: zvtral
4 Replies

8. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

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

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