Changes of text in a file

Please advise i need to change a text in a file, what should i do....

original text

18011324      ; serial number YYMMDDNN

m.abcd.com.             600     IN      A       10.10.128.154

text need to replaced

18011325      ; serial number YYMMDDNN

m.abcd.com.             600     IN      A       10.20.124.100
awk '/^[0-9]/{$1=new}1' new=18011325 file

What have you tried so far?

1 Like

Longhand OSX 10.7.5, default bash terminal:-

Last login: Thu Jan 30 07:38:52 on ttys000
AMIGA:barrywalker~> yourtext="18011324      ; serial number YYMMDDNN
> 
> m.abcd.com.             600     IN      A       10.10.128.154"
AMIGA:barrywalker~> echo "$yourtext"
18011324      ; serial number YYMMDDNN

m.abcd.com.             600     IN      A       10.10.128.154
AMIGA:barrywalker~> yourtext="${yourtext:0:$[ ( ${#yourtext} - 2 ) ]}00"
AMIGA:barrywalker~> echo "$yourtext"
18011324      ; serial number YYMMDDNN

m.abcd.com.             600     IN      A       10.10.128.100
AMIGA:barrywalker~> _

EDIT:
Drat, just seen the 5 and 20 in there too, but using this method is just as easy....

Hi Akshay and wisecracker:
I modified the CODE tags in the 1st message in this thread to make the input and output differences easier to see, but even with that fix, it seems that none of you noticed that four fields changed.

With four unrelated changes and no description of how the desired output could be derived from the input, it seems to me that the easiest thing for learnbash to do is to manually edit the input file. In case you didn't notice, the requested output (with the differences marked in red) are:

18011325      ; serial number YYMMDDNN

m.abcd.com.             600     IN      A       10.20.124.100

Why write a script to make four unrelated changes once?

1 Like

For domain replacement, i am able to do change with sed. But i want to automate it that's why i need to change serial number with some calculation.

current number : - 18011324

new number : - 18011325

It will check the last two digits and add the increment one number in it. If it is 23 it will change to 24, and if it is 24 then it will change to 25.

First of all I would like to thank you for moderator comment you posted. Don I really didn't notice it as it was merged like single file, and there was some unclear description some "flat file", I did not remember all...he must have edited. I think you might get in post history.

---------- Post updated at 03:14 PM ---------- Previous update was at 03:10 PM ----------

Spend some time and come with your script showing your effort towards it.. if we find that you really tried something..but could not solve it.. then we will answer... here we just want to see what you have learned from Forum after 300+ posts.

Hope other members also will agree with my words.

Regards,
Akshay

I am using sed to change the ip address, but i don't understand the logic how to increment last two numbers, only that part is left.

So, if all that you wanted was to have the serial number incremented, why didn't you say that? What the first message in this thread said was that you had some "original text" and "text need to be replaced" with no explanation for the logic behind incrementing one field, doubling another field, subtracting 4 from another field, and subtracting 54 from another field.

If you change the serial number in this file, how do you know that it won't match another serial number that has already been assigned in another file? And according to the comments in you data, these are serial numbers for January 13th in the year xx18???

From what you posted, how did you expect people in this forum (who want to help you) to be able to figure out what you wanted to be done?

Why not tell us how you know what the new IP address should be so both the serial number and the IP address can be changed in one invocation of awk instead of trying to coordinate changes by awk and sed for a single input file?

In the original posting, the 1st line was untagged. The remainder of the post was surrounded by a single set of CODE tags.

I made the only edit to that posting. My edit surrounded the input and output in separate CODE tags leaving the other "descriptive" text untagged. I also added the moderator's comment.

  • Don

Okay.. Thanks Don .. I don't remember exactly