editing line in text file adding number to value in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting editing line in text file adding number to value in file
# 1  
Old 01-19-2012
Question editing line in text file adding number to value in file

I have a text file that has data like:

Code:
Data    "12345#22"
Fred
ID 12345
Age 45
Wilma
Dino
Data  "123#22"
Tarzan
ID 123
Age 33
Jane

I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes:

Code:
Data    "1012345#22"
Fred
ID 1012345
Age 45
Wilma
Dino
Data  "1000123#22"
Tarzan
ID 1000123
Age 33
Jane

Of course this data is completely made up, but the principal is the same. One number is "VVVV#22" or "VVVVV#22" (ie, number is always after a single quote and before a # on a line called Data), and the other number is always the second word on an ID line. (both number the same in each stanza)

I can prepend the number, but can't figure out how to treat it as a number and add a value to it and replace...

Thanks in advance for any help...
# 2  
Old 01-19-2012
Hi,

You can use the below script.

Code:
y=1
while read line
do
fld1=`echo $line |cut -d' ' -f1|tr -d ' '`
if [ "$fld1" = 'Data' ]; then
   fld2=`echo $line|cut -d'"' -f2|cut -d'#' -f1|tr -d ' '`
   fld3=`expr $fld2 + 1000000`
   sed -i "$y s/$fld2/$fld3/" test1
elif [ "$fld1" = 'ID' ]; then
   fld2=`echo $line|cut -d' ' -f2|tr -d ' '`
   fld3=`expr $fld2 + 1000000`
   sed -i "$y s/$fld2/$fld3/" test1
fi
y=`expr $y + 1`
done < inputfile
exit 0

Code:
Input file :
 
Data "12345#22"
Fred
ID 12345
Age 45
Wilma
Dino
Data "123#22"
Tarzan
ID 123
Age 33
Jane

Code:
Output file : 
 
Data "1012345#22" 
Fred 
ID 1012345 
Age 45 
Wilma 
Dino 
Data "1000123#22" 
Tarzan 
ID 1000123 
Age 33 
Jane

If the final number has to be < 2000000 then you will need to put a check before using 'sed' so that the original value is not updated.

Regards,
RM

Last edited by rmohanty; 01-19-2012 at 10:53 PM..
# 3  
Old 01-20-2012
Code:
awk  '/^Data/{split($2,a,"[#|\"]");$2="\"" a[2]+1000000 "#" a[3] "\""} 
      /^ID/{$2+=1000000}1' infile

# 4  
Old 01-20-2012
Thanks. The AWK line works - as I can put it as a one liner. The only downside is it destroys the 'look' or of the original file. It was:

Code:
Data        "1#15"
  CreateTime           ""
  Id                   1

and is now
Code:
Data "1001#15"
{
  CreateTime           ""
Id 1001

I had to add an extra space, ie, ' Id' as I had Id and xxxId and yyyId in the file and it was changing all 3

Code:
awk '/^Data/{split($2,a,"[#|\"]");$2="\"" a[2]+1000 "#" a[3] "\""}
/ Id/{$2+=1000}1' infile

as SED would just replace, I don't think it would destroy the formatting.

I can do it by making two changes. The first one is easy...add spaces before the \ :
Code:
....{split($2,a,"[#|\"]");$2="       \""....

I can change the second one by piping through SED
Code:
.... | sed -e 's/^Id /  Id                   /g'

but this seems an ugly way of doing it

Last edited by say170; 01-20-2012 at 07:19 AM..
# 5  
Old 01-20-2012
You can preserve formatting like so:

Code:
awk -F '[ \t"#]*' '/ID|Data/{sub($2,$2+1000000)}1' infile


Last edited by Scrutinizer; 01-20-2012 at 08:42 AM..
# 6  
Old 01-20-2012
That doesn't add to the ID field for me, it does to the data line though. The ID line doesn't have quotes around the value - I think that's why it doesn't work.

This is my data file:

Code:
Data        "1#15"
{
  CreateTime           ""
  Id                   1

# 7  
Old 01-20-2012
No, it is because - different from your sample -, here Id is written with a lowercase d.
Try:
Code:
awk -F '[ \t"#]*' '/Id|Data/{sub($2,$2+1000000)}1' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding user name to file, and then displaying new line number

Hi all - I'm completely stumped by a script I'm working on... The short version is I have a file called 'lookup' and in it are hundreds of names (first and last). I have a script that basically allows the user to enter a name, and what I need to have happen is something like this: Record... (8 Replies)
Discussion started by: sabster
8 Replies

2. UNIX for Dummies Questions & Answers

[Help] Adding text to a variable line in a file

Hey guys, I need to write a script that will add a specific text at the end of a specific line (of a text file). but the line is a variable this is my text file : device_2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=30000 ansible_ssh_user='root' device_2 ansible_ssh_host=127.0.0.1... (1 Reply)
Discussion started by: OdedOvdat
1 Replies

3. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

4. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

5. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

6. Shell Programming and Scripting

adding a line to a text file

I have a tab delimited text file, id name distance 1 3325167 0.334561754018 2 3290488 0.389444269458 3 3288794 0.392312701782 4 3347602 0.392532202097 5 3295355 0.394394169485 I need to add a line after the header line. The first and third field of... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

7. Linux

Get a specific line number from a text file

Hello! All, Could you please tell me how to get a specific line number from a text file? For example below, ABC DEF ---> Get this line number, return to an variable GHI My OS is Linux. Thank you so much for your help in advance! (3 Replies)
Discussion started by: barryxian
3 Replies

8. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

9. Shell Programming and Scripting

adding text to end of each line in a file

I'm needing to add a "hour:min" to the end of each line in a document. The document in this case is only going to be one line. if this inserts it at the end, what needs to be changed to add something at the end... /bin/echo "%s/^/$filler/g\nwq!" | ex -s $oFile Thank you... (2 Replies)
Discussion started by: cubs0729
2 Replies

10. Shell Programming and Scripting

Adding Text To each line of a file

How would I add text to the beginning of each line in a text file in a script right after the file is created from another text file. (4 Replies)
Discussion started by: cubs0729
4 Replies
Login or Register to Ask a Question