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
# 1  
Old 06-28-2012
Error 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 ,@,
Code:
for file in *alpha* *beat* *kappa*
 do
   awk -F",@," '{ print $1 }' $file > sed -i 's/${1}/999/g' $file
 done

Here in the example I am trying to replace the first number in each row of the file with a dummy number 999.

Here is a sample file contents
Code:
123,@,x,@,y,@,45
453,@,x,@,y,@,23

Any help is appreciated. Thanks in advance !

Last edited by Franklin52; 06-29-2012 at 04:06 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-28-2012
@alister
... just another "ed" challenge for you Smilie
# 3  
Old 06-28-2012
Code:
awk -F",@," '{sub($1,999); print}' file

This User Gave Thanks to shamrock For This Post:
# 4  
Old 06-28-2012
Are you willing to change the first number into the first row only or in every row ?
# 5  
Old 06-28-2012
Code:
cd directory
for file in a b c
do
 sed 's/[0-9]\{1,\}/999/1' $file > $$
 mv $$ $file
done

---------- Post updated at 12:47 PM ---------- Previous update was at 12:45 PM ----------

Quote:
Originally Posted by shamrock
Code:
awk -F",@," '{sub($1,999); print}' file

This will replace a non-numeric string too...
This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 06-28-2012
Hi Shamrock,

Thanks for the help. The script is working but is not able to change the contents in the file.
I am not sure if it has to do with any permissions, but when I ran the script and directed output to a file, I see the changed values, not in the original files for some reason.

How do I effect the changes in the files?

---------- Post updated at 12:50 PM ---------- Previous update was at 12:49 PM ----------

Hi ctsgnb,

First number of each row in each of the files

Thanks!
# 7  
Old 06-28-2012
Quote:
Originally Posted by ctsgnb
@alister
... just another "ed" challenge for you Smilie
I've become so predictable. Smilie

I'm not quite sure what the OP wants. The problem statement states that the desire is to change the first number, but the AWK is extracting the first field without regard to what it contains. Following the AWK lead, the following changes everything preceding the first comma.

The following modifies the file in-place. Proceed with caution.

Code:
printf '%s\n' ',s/^[^,]*/999/' w q | ed -s file

Regards,
Alister

Last edited by alister; 06-28-2012 at 03:01 PM.. Reason: Woops. Included the wrong ctsgnb quote.
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